diff --git a/src/renderer/components/+landing-page/workspace-cluster.store.ts b/src/renderer/components/+landing-page/workspace-cluster.store.ts index 24834927b3..8fd2966b93 100644 --- a/src/renderer/components/+landing-page/workspace-cluster.store.ts +++ b/src/renderer/components/+landing-page/workspace-cluster.store.ts @@ -3,6 +3,7 @@ import { Cluster } from "../../../main/cluster"; import { clusterStore } from "../../../common/cluster-store"; import { ItemObject, ItemStore } from "../../item.store"; import { autobind } from "../../utils"; +import { computed, reaction } from "mobx"; export class ClusterItem implements ItemObject { constructor(public cluster: Cluster) {} @@ -51,15 +52,21 @@ export class WorkspaceClusterStore extends ItemStore { this.workspaceId = workspaceId; } + @computed get clusters(): ClusterItem[] { + return clusterStore + .getByWorkspaceId(this.workspaceId) + .filter(cluster => cluster.enabled) + .map(cluster => new ClusterItem(cluster)); + } + + watch() { + return reaction(() => this.clusters, () => this.loadAll(), { + fireImmediately: true + }); + } + loadAll() { - return this.loadItems( - () => ( - clusterStore - .getByWorkspaceId(this.workspaceId) - .filter(cluster => cluster.enabled) - .map(cluster => new ClusterItem(cluster)) - ) - ); + return this.loadItems(() => this.clusters); } async remove(clusterItem: ClusterItem) { diff --git a/src/renderer/components/+landing-page/workspace-overview.tsx b/src/renderer/components/+landing-page/workspace-overview.tsx index 3fcb3d6ed3..bb95f5d336 100644 --- a/src/renderer/components/+landing-page/workspace-overview.tsx +++ b/src/renderer/components/+landing-page/workspace-overview.tsx @@ -1,7 +1,7 @@ import "./workspace-overview.scss"; import React, { Component } from "react"; -import { disposeOnUnmount, observer } from "mobx-react"; +import { observer } from "mobx-react"; import { ItemListLayout } from "../item-object-list/item-list-layout"; import { ClusterItem, WorkspaceClusterStore } from "./workspace-cluster.store"; import { navigate } from "../../navigation"; @@ -9,7 +9,7 @@ import { clusterViewURL } from "../cluster-manager/cluster-view.route"; import { WorkspaceClusterMenu } from "./workspace-cluster-menu"; import { kebabCase } from "lodash"; import { addClusterURL } from "../+add-cluster"; -import { observable, reaction } from "mobx"; +import { IReactionDisposer, observable, reaction } from "mobx"; import { workspaceStore } from "../../../common/workspace-store"; enum sortBy { @@ -23,15 +23,22 @@ enum sortBy { export class WorkspaceOverview extends Component { @observable private workspaceClusterStore?: WorkspaceClusterStore; + disposeWorkspaceWatch: IReactionDisposer; + disposeClustersWatch: IReactionDisposer; + componentDidMount() { - disposeOnUnmount(this, [ - reaction(() => workspaceStore.currentWorkspaceId, workspaceId => { - this.workspaceClusterStore = new WorkspaceClusterStore(workspaceId); - this.workspaceClusterStore.loadAll().catch(error => console.log("workspaceClusterStore.loadAll", error)); - }, { - fireImmediately: true, - }) - ]); + this.disposeWorkspaceWatch = reaction(() => workspaceStore.currentWorkspaceId, workspaceId => { + this.workspaceClusterStore = new WorkspaceClusterStore(workspaceId); + this.disposeClustersWatch?.(); + this.disposeClustersWatch = this.workspaceClusterStore.watch(); + }, { + fireImmediately: true, + }); + } + + componentWillUnmount() { + this.disposeWorkspaceWatch?.(); + this.disposeClustersWatch?.(); } showCluster = ({ clusterId }: ClusterItem) => {