1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Issue-827: Enhancement to bring back last active cluster view

This enhancement will store the last active cluster used in a workspace. If you then switch to a different
workspace and switch back the cluster view will be restored

Signed-off-by: Steve Richards <srichards@mirantis.com>
This commit is contained in:
Steve Richards 2020-10-08 14:54:41 +01:00
parent abe6a4e0b1
commit d8f972a02e
5 changed files with 57 additions and 9 deletions

View File

@ -3,8 +3,10 @@ import { BaseStore } from "./base-store";
import { clusterStore } from "./cluster-store"
import { landingURL } from "../renderer/components/+landing-page/landing-page.route";
import { navigate } from "../renderer/navigation";
import { clusterViewURL } from "../renderer/components/cluster-manager/cluster-view.route";
export type WorkspaceId = string;
export type ClusterId = string;
export interface WorkspaceStoreModel {
currentWorkspace?: WorkspaceId;
@ -15,6 +17,7 @@ export interface Workspace {
id: WorkspaceId;
name: string;
description?: string;
lastActiveClusterId?: ClusterId;
}
export class WorkspaceStore extends BaseStore<WorkspaceStoreModel> {
@ -31,7 +34,8 @@ export class WorkspaceStore extends BaseStore<WorkspaceStoreModel> {
@observable workspaces = observable.map<WorkspaceId, Workspace>({
[WorkspaceStore.defaultId]: {
id: WorkspaceStore.defaultId,
name: "default"
name: "default",
lastActiveClusterId: ""
}
});
@ -55,18 +59,36 @@ export class WorkspaceStore extends BaseStore<WorkspaceStoreModel> {
return this.workspacesList.find(workspace => workspace.name === name);
}
@action
setLastActiveClusterId(id: WorkspaceId, clusterId: ClusterId) {
if (clusterId != null) {
this.getById(id).lastActiveClusterId = clusterId;
}
}
@action
setActive(id = WorkspaceStore.defaultId, { redirectToLanding = true, resetActiveCluster = true } = {}) {
this.setLastActiveClusterId(this.currentWorkspaceId, clusterStore.activeClusterId)
if (id === this.currentWorkspaceId) return;
if (!this.getById(id)) {
const workspaceToUse = this.getById(id);
if (!workspaceToUse) {
throw new Error(`workspace ${id} doesn't exist`);
}
this.currentWorkspaceId = id;
this.currentWorkspaceId = workspaceToUse.id;
const clusterId = workspaceToUse.lastActiveClusterId;
if (resetActiveCluster) {
clusterStore.setActive(null)
}
if (redirectToLanding) {
navigate(landingURL())
if (clusterId) {
clusterStore.setActive(clusterId)
navigate(clusterViewURL({ params: { clusterId } }));
} else {
clusterStore.setActive(null)
if (redirectToLanding) {
navigate(landingURL())
}
}
}
}

View File

@ -146,7 +146,9 @@ export class AddCluster extends React.Component {
clusterStore.addCluster(...newClusters);
if (newClusters.length === 1) {
const clusterId = newClusters[0].id;
const wsId = workspaceStore.currentWorkspace.id;
clusterStore.setActive(clusterId);
workspaceStore.setLastActiveClusterId(wsId, clusterStore.activeClusterId);
navigate(clusterViewURL({ params: { clusterId } }));
} else {
Notifications.ok(

View File

@ -7,8 +7,9 @@ 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 { workspaceStore, WorkspaceId } from "../../../common/workspace-store";
import { cssNames } from "../../utils";
import { clusterStore } from "../../../common/cluster-store";
interface Props extends Partial<MenuProps> {
}
@ -17,6 +18,13 @@ interface Props extends Partial<MenuProps> {
export class WorkspaceMenu extends React.Component<Props> {
@observable menuVisible = false;
activateWorkspace = (id: WorkspaceId) => {
if (clusterStore.activeClusterId) {
workspaceStore.setLastActiveClusterId(workspaceStore.currentWorkspace.id, clusterStore.activeClusterId);
}
workspaceStore.setActive(id);
}
render() {
const { className, ...menuProps } = this.props;
const { workspacesList, currentWorkspace } = workspaceStore;
@ -38,7 +46,7 @@ export class WorkspaceMenu extends React.Component<Props> {
key={workspaceId}
title={description}
active={workspaceId === currentWorkspace.id}
onClick={() => workspaceStore.setActive(workspaceId)}
onClick={() => this.activateWorkspace(workspaceId)}
>
<Icon small material="layers"/>
<span className="workspace">{name}</span>

View File

@ -54,6 +54,7 @@ export class Workspaces extends React.Component {
id: workspaceId,
name: "",
description: "",
lastActiveClusterId: ""
})
}

View File

@ -29,6 +29,10 @@ interface Props {
@observer
export class ClustersMenu extends React.Component<Props> {
showCluster = (clusterId: ClusterId) => {
const wsId = workspaceStore.currentWorkspace.id;
if(clusterId) {
workspaceStore.setLastActiveClusterId(wsId, clusterId);
}
clusterStore.setActive(clusterId);
navigate(clusterViewURL({ params: { clusterId } }));
}
@ -57,7 +61,12 @@ export class ClustersMenu extends React.Component<Props> {
menu.append(new MenuItem({
label: _i18n._(t`Disconnect`),
click: async () => {
const wsId = workspaceStore.currentWorkspace.id;
const wsLastActiveClusterId = workspaceStore.currentWorkspace.lastActiveClusterId
if (clusterStore.isActive(cluster.id)) {
if (wsLastActiveClusterId === cluster.id) {
workspaceStore.setLastActiveClusterId(wsId, "");
}
navigate(landingURL());
clusterStore.setActive(null);
}
@ -75,6 +84,12 @@ export class ClustersMenu extends React.Component<Props> {
label: _i18n._(t`Remove`),
},
ok: () => {
const wsId = workspaceStore.currentWorkspace.id;
const wsLastActiveClusterId = workspaceStore.currentWorkspace.lastActiveClusterId
if (wsLastActiveClusterId === cluster.id) {
workspaceStore.setLastActiveClusterId(wsId, "");
}
if (clusterStore.activeClusterId === cluster.id) {
navigate(landingURL());
}