1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/common/workspace-store.ts
Roman 334815f71a
Tray icon (#1005)
* Tray icon #833 -- part 1

Signed-off-by: Roman <ixrock@gmail.com>

* Tray icon #833 -- part 2

Signed-off-by: Roman <ixrock@gmail.com>

* Tray icon #833 -- part 3

Signed-off-by: Roman <ixrock@gmail.com>

* Tray icon #833 -- part 4

Signed-off-by: Roman <ixrock@gmail.com>

* fix: lint / linux build failed

Signed-off-by: Roman <ixrock@gmail.com>

* allow to disable tray from preferences

Signed-off-by: Roman <ixrock@gmail.com>

* allow to tweak svg-icon before applying as tray-icon

Signed-off-by: Roman <ixrock@gmail.com>

* add checkbox indication, setActive workspace on cluster select

Signed-off-by: Roman <ixrock@gmail.com>

* fix build version (cannon find module 'react')

Signed-off-by: Roman <ixrock@gmail.com>

* - switching dark/light icon depending on os-x theme settings
- optimization: don't re-create tray icon on menu udpates (avoid blinking)

Signed-off-by: Roman <ixrock@gmail.com>

* fix: refresh icon after turning on/off + switching dark-mode

Signed-off-by: Roman <ixrock@gmail.com>

* allow to close main window and re-open from dock or tray icon

Signed-off-by: Roman <ixrock@gmail.com>

* small fix

Signed-off-by: Roman <ixrock@gmail.com>

* fix: ensure main-window from global menu

Signed-off-by: Roman <ixrock@gmail.com>

* chore

Signed-off-by: Roman <ixrock@gmail.com>

* fix: hide traffic-light buttons for tray window

Signed-off-by: Roman <ixrock@gmail.com>

* removed redundant tray window

Signed-off-by: Roman <ixrock@gmail.com>

* removed delay from base-store

Signed-off-by: Roman <ixrock@gmail.com>

* adding cluster fix (reverted changes from master)

Signed-off-by: Roman <ixrock@gmail.com>

* - hide icon in dock when main-window closed (mac-os only)
- added preferences checkbox to open app at system start-up

Signed-off-by: Roman <ixrock@gmail.com>

* handle quit app action from tray menu

Signed-off-by: Roman <ixrock@gmail.com>

* moved generating tray icons to build step

Signed-off-by: Roman <ixrock@gmail.com>

* Fix integration tests (#1080)

* Fix integration tests

Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com>

* Update integration/helpers/utils.ts

Co-authored-by: Sebastian Malton <sebastian@malton.name>
Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com>

Co-authored-by: Sebastian Malton <sebastian@malton.name>

* fix-build: invisible app icon when there are more files within "build/icons/*.png"

Signed-off-by: Roman <ixrock@gmail.com>

* chore

Signed-off-by: Roman <ixrock@gmail.com>

* yarn i18n.extract

Signed-off-by: Roman <ixrock@gmail.com>

* clean-up

Signed-off-by: Roman <ixrock@gmail.com>

* navigation refactoring, move out `buildUrl` to common/utils so `react` and `react-router` not required as package.json dependecies in runtime (main)

Signed-off-by: Roman <ixrock@gmail.com>

* Ignore namespace query param on integration tests (#1109)

Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com>

* merge-conflicts fixes

Signed-off-by: Roman <ixrock@gmail.com>

* support page fixes

Signed-off-by: Roman <ixrock@gmail.com>

* make eslint happy again

Signed-off-by: Roman <ixrock@gmail.com>

Co-authored-by: Lauri Nevala <lauri.nevala@gmail.com>
Co-authored-by: Sebastian Malton <sebastian@malton.name>
2020-10-27 15:25:29 +02:00

123 lines
3.2 KiB
TypeScript

import { action, computed, observable, toJS } from "mobx";
import { BaseStore } from "./base-store";
import { clusterStore } from "./cluster-store"
import { appEventBus } from "./event-bus";
export type WorkspaceId = string;
export interface WorkspaceStoreModel {
currentWorkspace?: WorkspaceId;
workspaces: Workspace[]
}
export interface Workspace {
id: WorkspaceId;
name: string;
description?: string;
}
export class WorkspaceStore extends BaseStore<WorkspaceStoreModel> {
static readonly defaultId: WorkspaceId = "default"
private constructor() {
super({
configName: "lens-workspace-store",
});
}
@observable currentWorkspaceId = WorkspaceStore.defaultId;
@observable workspaces = observable.map<WorkspaceId, Workspace>({
[WorkspaceStore.defaultId]: {
id: WorkspaceStore.defaultId,
name: "default"
}
});
@computed get currentWorkspace(): Workspace {
return this.getById(this.currentWorkspaceId);
}
@computed get workspacesList() {
return Array.from(this.workspaces.values());
}
isDefault(id: WorkspaceId) {
return id === WorkspaceStore.defaultId;
}
getById(id: WorkspaceId): Workspace {
return this.workspaces.get(id);
}
getByName(name: string): Workspace {
return this.workspacesList.find(workspace => workspace.name === name);
}
@action
setActive(id = WorkspaceStore.defaultId, reset = true) {
if (id === this.currentWorkspaceId) return;
if (!this.getById(id)) {
throw new Error(`workspace ${id} doesn't exist`);
}
this.currentWorkspaceId = id;
clusterStore.activeClusterId = null; // fixme: handle previously selected cluster from current workspace
}
@action
saveWorkspace(workspace: Workspace) {
const { id, name } = workspace;
const existingWorkspace = this.getById(id);
if (!name.trim() || this.getByName(name.trim())) {
return;
}
if (existingWorkspace) {
Object.assign(existingWorkspace, workspace);
appEventBus.emit({name: "workspace", action: "update"})
} else {
appEventBus.emit({name: "workspace", action: "add"})
}
this.workspaces.set(id, workspace);
return workspace;
}
@action
removeWorkspace(id: WorkspaceId) {
const workspace = this.getById(id);
if (!workspace) return;
if (this.isDefault(id)) {
throw new Error("Cannot remove default workspace");
}
if (this.currentWorkspaceId === id) {
this.currentWorkspaceId = WorkspaceStore.defaultId; // reset to default
}
this.workspaces.delete(id);
appEventBus.emit({name: "workspace", action: "remove"})
clusterStore.removeByWorkspaceId(id)
}
@action
protected fromStore({ currentWorkspace, workspaces = [] }: WorkspaceStoreModel) {
if (currentWorkspace) {
this.currentWorkspaceId = currentWorkspace
}
if (workspaces.length) {
this.workspaces.clear();
workspaces.forEach(workspace => {
this.workspaces.set(workspace.id, workspace)
})
}
}
toJSON(): WorkspaceStoreModel {
return toJS({
currentWorkspace: this.currentWorkspaceId,
workspaces: this.workspacesList,
}, {
recurseEverything: true
})
}
}
export const workspaceStore = WorkspaceStore.getInstance<WorkspaceStore>()