From f38b307d9e0c87a53e89fde7a3fa326e40f5430a Mon Sep 17 00:00:00 2001 From: Jari Kolehmainen Date: Thu, 21 Jan 2021 14:57:29 +0200 Subject: [PATCH] introduce openCommandDialog & closeCommandDialog Signed-off-by: Jari Kolehmainen --- src/extensions/registries/command-registry.ts | 1 + .../+preferences/preferences.route.ts | 3 +- .../components/+workspaces/add-workspace.tsx | 56 ++++ src/renderer/components/+workspaces/index.ts | 1 - .../+workspaces/remove-workspace.tsx | 71 ++++++ .../+workspaces/workspace-menu.scss | 7 - .../components/+workspaces/workspace-menu.tsx | 66 ----- .../+workspaces/workspaces.route.ts | 8 - .../components/+workspaces/workspaces.tsx | 241 ++++-------------- src/renderer/components/app.tsx | 2 + .../components/cluster-manager/bottom-bar.tsx | 8 +- .../cluster-manager/cluster-manager.tsx | 2 - .../command-palette/command-container.scss | 11 + .../command-palette/command-container.tsx | 64 +++++ .../command-palette/command-dialog.scss | 28 -- .../command-palette/command-dialog.tsx | 59 +---- src/renderer/components/input/input.tsx | 2 +- src/renderer/lens-app.tsx | 5 +- 18 files changed, 272 insertions(+), 363 deletions(-) create mode 100644 src/renderer/components/+workspaces/add-workspace.tsx create mode 100644 src/renderer/components/+workspaces/remove-workspace.tsx delete mode 100644 src/renderer/components/+workspaces/workspace-menu.scss delete mode 100644 src/renderer/components/+workspaces/workspace-menu.tsx delete mode 100644 src/renderer/components/+workspaces/workspaces.route.ts create mode 100644 src/renderer/components/command-palette/command-container.scss create mode 100644 src/renderer/components/command-palette/command-container.tsx delete mode 100644 src/renderer/components/command-palette/command-dialog.scss diff --git a/src/extensions/registries/command-registry.ts b/src/extensions/registries/command-registry.ts index 359702c29f..258f856b9c 100644 --- a/src/extensions/registries/command-registry.ts +++ b/src/extensions/registries/command-registry.ts @@ -12,6 +12,7 @@ export type CommandContext = { export interface CommandRegistration { id: string; title: string; + scope: "cluster" | "global"; action: (context: CommandContext) => void; isActive?: (context: CommandContext) => boolean; } diff --git a/src/renderer/components/+preferences/preferences.route.ts b/src/renderer/components/+preferences/preferences.route.ts index 6937ecb0dd..2915b68a28 100644 --- a/src/renderer/components/+preferences/preferences.route.ts +++ b/src/renderer/components/+preferences/preferences.route.ts @@ -12,5 +12,6 @@ export const preferencesURL = buildURL(preferencesRoute.path); commandRegistry.add({ id: "app.showPreferences", title: "Preferences: Open", - action: () => navigate(preferencesURL.toString()) + scope: "global", + action: () => navigate(preferencesURL()) }); diff --git a/src/renderer/components/+workspaces/add-workspace.tsx b/src/renderer/components/+workspaces/add-workspace.tsx new file mode 100644 index 0000000000..a7b24804e4 --- /dev/null +++ b/src/renderer/components/+workspaces/add-workspace.tsx @@ -0,0 +1,56 @@ +import React from "react"; +import { observer } from "mobx-react"; +import { Workspace, workspaceStore } from "../../../common/workspace-store"; +import { v4 as uuid } from "uuid"; +import { commandRegistry } from "../../../extensions/registries/command-registry"; +import { Input, InputValidator } from "../input"; +import { navigate } from "../../navigation"; +import { closeCommandDialog, openCommandDialog } from "../command-palette/command-container"; + +const uniqueWorkspaceName: InputValidator = { + condition: ({ required }) => required, + message: () => `Workspace with this name already exists`, + validate: value => !workspaceStore.enabledWorkspacesList.find((workspace) => workspace.name === value), +}; + +@observer +export class AddWorkspace extends React.Component { + handleKeyDown(name: string) { + if (name.trim() === "") { + return; + } + const workspace = workspaceStore.addWorkspace(new Workspace({ + id: uuid(), + name + })); + + workspaceStore.setActive(workspace.id); + navigate("/"); + closeCommandDialog(); + } + + render() { + return ( + <> + this.handleKeyDown(v)} + dirty={true} + showValidationLine={true} /> + + Please provide a new workspace name (Press "Enter" to confirm or "Escape" to cancel) + + + ); + } +} + +commandRegistry.add({ + id: "workspace.addWorkspace", + title: "Workspace: Add workspace ...", + scope: "global", + action: () => openCommandDialog() +}); diff --git a/src/renderer/components/+workspaces/index.ts b/src/renderer/components/+workspaces/index.ts index db23faa3be..5b84fc9b00 100644 --- a/src/renderer/components/+workspaces/index.ts +++ b/src/renderer/components/+workspaces/index.ts @@ -1,2 +1 @@ -export * from "./workspaces.route"; export * from "./workspaces"; diff --git a/src/renderer/components/+workspaces/remove-workspace.tsx b/src/renderer/components/+workspaces/remove-workspace.tsx new file mode 100644 index 0000000000..17119b4a82 --- /dev/null +++ b/src/renderer/components/+workspaces/remove-workspace.tsx @@ -0,0 +1,71 @@ +import React from "react"; +import { observer } from "mobx-react"; +import { computed} from "mobx"; +import { WorkspaceStore, workspaceStore } from "../../../common/workspace-store"; +import { ConfirmDialog } from "../confirm-dialog"; +import { commandRegistry } from "../../../extensions/registries/command-registry"; +import { Select } from "../select"; +import { closeCommandDialog, openCommandDialog } from "../command-palette/command-container"; + +@observer +export class RemoveWorkspace extends React.Component { + @computed get options() { + return workspaceStore.enabledWorkspacesList.filter((workspace) => workspace.id !== WorkspaceStore.defaultId).map((workspace) => { + return { value: workspace.id, label: workspace.name }; + }); + } + + onChange(id: string) { + const workspace = workspaceStore.enabledWorkspacesList.find((workspace) => workspace.id === id); + + if (!workspace ) { + return; + } + + closeCommandDialog(); + ConfirmDialog.open({ + okButtonProps: { + label: `Remove Workspace`, + primary: false, + accent: true, + }, + ok: () => { + workspaceStore.removeWorkspace(workspace); + + if (workspace.id === workspaceStore.currentWorkspaceId) { + workspaceStore.setActive(workspaceStore.enabledWorkspacesList[0].id); + } + }, + message: ( +
+

+ Are you sure you want remove workspace {workspace.name}? +

+

+ All clusters within workspace will be cleared as well +

+
+ ), + }); + } + + render() { + return ( + editingWorkspace.name = v} - onKeyPress={(e) => this.onInputKeypress(e, workspaceId)} - validators={[isRequired, existenceValidator]} - autoFocus - /> - editingWorkspace.description = v} - onKeyPress={(e) => this.onInputKeypress(e, workspaceId)} - /> - this.saveWorkspace(workspaceId)} - /> - this.clearEditing(workspaceId)} - /> - - )} - - ); - })} - -