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

Save and restore lastActiveClusterId

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
Alex Andreev 2020-11-18 15:01:36 +03:00
parent d723474684
commit f8841fa0a5
4 changed files with 37 additions and 5 deletions

View File

@ -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<WorkspaceStoreModel> {
}
@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<WorkspaceStoreModel> {
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) {

View File

@ -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) {

View File

@ -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<MenuProps> {
}
@ -17,6 +20,16 @@ interface Props extends Partial<MenuProps> {
export class WorkspaceMenu extends React.Component<Props> {
@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<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

@ -31,6 +31,7 @@ interface Props {
@observer
export class ClustersMenu extends React.Component<Props> {
showCluster = (clusterId: ClusterId) => {
workspaceStore.setLastActiveClusterId(clusterId);
navigate(clusterViewURL({ params: { clusterId } }));
}
@ -56,7 +57,11 @@ export class ClustersMenu extends React.Component<Props> {
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<Props> {
label: _i18n._(t`Remove`),
},
ok: () => {
const lastActiveClusterId = workspaceStore.currentWorkspace.lastActiveClusterId;
if (lastActiveClusterId === cluster.id) {
workspaceStore.setLastActiveClusterId("");
}
if (clusterStore.activeClusterId === cluster.id) {
navigate(landingURL());
}