diff --git a/src/common/workspace-store.ts b/src/common/workspace-store.ts index 70f8d08fd2..33e92e8db0 100644 --- a/src/common/workspace-store.ts +++ b/src/common/workspace-store.ts @@ -7,10 +7,12 @@ import { broadcastIpc } from "../common/ipc"; import logger from "../main/logger"; export type WorkspaceId = string; +export type ClusterId = string; export interface WorkspaceStoreModel { + workspaces: WorkspaceModel[]; currentWorkspace?: WorkspaceId; - workspaces: WorkspaceModel[] + lastActiveClusterId?: ClusterId; } export interface WorkspaceModel { @@ -30,6 +32,7 @@ export class Workspace implements WorkspaceModel, WorkspaceState { @observable description?: string @observable ownerRef?: string @observable enabled: boolean + @observable lastActiveClusterId?: ClusterId constructor(data: WorkspaceModel) { Object.assign(this, data) @@ -141,13 +144,12 @@ export class WorkspaceStore extends BaseStore { } @action - setActive(id = WorkspaceStore.defaultId, reset = true) { + setActive(id = WorkspaceStore.defaultId) { if (id === this.currentWorkspaceId) return; if (!this.getById(id)) { throw new Error(`workspace ${id} doesn't exist`); } this.currentWorkspaceId = id; - clusterStore.activeCluster = null; // fixme: handle previously selected cluster from current workspace } @action @@ -187,6 +189,13 @@ export class WorkspaceStore extends BaseStore { clusterStore.removeByWorkspaceId(id) } + @action + setLastActiveClusterId(clusterId: ClusterId, workspaceId = this.currentWorkspaceId) { + if (clusterId != null) { + this.getById(workspaceId).lastActiveClusterId = clusterId; + } + } + @action protected fromStore({ currentWorkspace, workspaces = [] }: WorkspaceStoreModel) { if (currentWorkspace) { diff --git a/src/renderer/components/+add-cluster/add-cluster.tsx b/src/renderer/components/+add-cluster/add-cluster.tsx index 8acd3a51ea..6bf439930c 100644 --- a/src/renderer/components/+add-cluster/add-cluster.tsx +++ b/src/renderer/components/+add-cluster/add-cluster.tsx @@ -167,6 +167,7 @@ export class AddCluster extends React.Component { if (newClusters.length === 1) { const clusterId = newClusters[0].id; clusterStore.setActive(clusterId); + workspaceStore.setLastActiveClusterId(clusterId); navigate(clusterViewURL({ params: { clusterId } })); } else { if (newClusters.length > 1) { diff --git a/src/renderer/components/+workspaces/workspace-menu.tsx b/src/renderer/components/+workspaces/workspace-menu.tsx index 1979d3a3ad..ccf0706942 100644 --- a/src/renderer/components/+workspaces/workspace-menu.tsx +++ b/src/renderer/components/+workspaces/workspace-menu.tsx @@ -7,8 +7,11 @@ import { Trans } from "@lingui/macro"; import { Menu, MenuItem, MenuProps } from "../menu"; import { Icon } from "../icon"; import { observable } from "mobx"; -import { workspaceStore } from "../../../common/workspace-store"; +import { WorkspaceId, workspaceStore } from "../../../common/workspace-store"; import { cssNames } from "../../utils"; +import { navigate } from "../../navigation"; +import { clusterViewURL } from "../cluster-manager/cluster-view.route"; +import { landingURL } from "../+landing-page"; interface Props extends Partial { } @@ -17,6 +20,16 @@ interface Props extends Partial { export class WorkspaceMenu extends React.Component { @observable menuVisible = false; + activateWorkspace = (id: WorkspaceId) => { + const clusterId = workspaceStore.getById(id).lastActiveClusterId; + workspaceStore.setActive(id); + if (clusterId) { + navigate(clusterViewURL({ params: { clusterId } })); + } else { + navigate(landingURL()); + } + } + render() { const { className, ...menuProps } = this.props; const { enabledWorkspacesList, currentWorkspace } = workspaceStore; @@ -38,7 +51,7 @@ export class WorkspaceMenu extends React.Component { key={workspaceId} title={description} active={workspaceId === currentWorkspace.id} - onClick={() => workspaceStore.setActive(workspaceId)} + onClick={() => this.activateWorkspace(workspaceId)} > {name} diff --git a/src/renderer/components/cluster-manager/clusters-menu.tsx b/src/renderer/components/cluster-manager/clusters-menu.tsx index 1fb818a812..e99333fccb 100644 --- a/src/renderer/components/cluster-manager/clusters-menu.tsx +++ b/src/renderer/components/cluster-manager/clusters-menu.tsx @@ -31,6 +31,7 @@ interface Props { @observer export class ClustersMenu extends React.Component { showCluster = (clusterId: ClusterId) => { + workspaceStore.setLastActiveClusterId(clusterId); navigate(clusterViewURL({ params: { clusterId } })); } @@ -56,7 +57,11 @@ export class ClustersMenu extends React.Component { menu.append(new MenuItem({ label: _i18n._(t`Disconnect`), click: async () => { + const lastActiveClusterId = workspaceStore.currentWorkspace.lastActiveClusterId; if (clusterStore.isActive(cluster.id)) { + if (lastActiveClusterId === cluster.id) { + workspaceStore.setLastActiveClusterId(""); + } navigate(landingURL()); clusterStore.setActive(null); } @@ -74,6 +79,10 @@ export class ClustersMenu extends React.Component { label: _i18n._(t`Remove`), }, ok: () => { + const lastActiveClusterId = workspaceStore.currentWorkspace.lastActiveClusterId; + if (lastActiveClusterId === cluster.id) { + workspaceStore.setLastActiveClusterId(""); + } if (clusterStore.activeClusterId === cluster.id) { navigate(landingURL()); }