From 2fabfe1de5eb644bd5da9591d21410e9044deb54 Mon Sep 17 00:00:00 2001 From: Alex Andreev Date: Thu, 19 Aug 2021 16:00:09 +0300 Subject: [PATCH] Custom delete cluster dialog Signed-off-by: Alex Andreev --- src/renderer/components/app.scss | 1 - .../cluster-manager/cluster-manager.tsx | 2 + .../delete-cluster-dialog.module.css | 46 +++++++ .../delete-cluster-dialog.tsx | 129 ++++++++++++++++++ .../components/delete-cluster-dialog/index.ts | 22 +++ 5 files changed, 199 insertions(+), 1 deletion(-) create mode 100644 src/renderer/components/delete-cluster-dialog/delete-cluster-dialog.module.css create mode 100644 src/renderer/components/delete-cluster-dialog/delete-cluster-dialog.tsx create mode 100644 src/renderer/components/delete-cluster-dialog/index.ts diff --git a/src/renderer/components/app.scss b/src/renderer/components/app.scss index e1d002c8f7..8062e38f0b 100755 --- a/src/renderer/components/app.scss +++ b/src/renderer/components/app.scss @@ -172,7 +172,6 @@ code { padding: 0.2em; vertical-align: middle; border-radius: $radius; - font-family: $font-monospace; font-size: calc(var(--font-size) * .9); color: #b4b5b4; diff --git a/src/renderer/components/cluster-manager/cluster-manager.tsx b/src/renderer/components/cluster-manager/cluster-manager.tsx index 4bd14e7805..1903625116 100644 --- a/src/renderer/components/cluster-manager/cluster-manager.tsx +++ b/src/renderer/components/cluster-manager/cluster-manager.tsx @@ -35,6 +35,7 @@ import { HotbarMenu } from "../hotbar/hotbar-menu"; import { EntitySettings } from "../+entity-settings"; import { Welcome } from "../+welcome"; import * as routes from "../../../common/routes"; +import { DeleteClusterDialog } from "../delete-cluster-dialog"; @observer export class ClusterManager extends React.Component { @@ -62,6 +63,7 @@ export class ClusterManager extends React.Component { + ); } diff --git a/src/renderer/components/delete-cluster-dialog/delete-cluster-dialog.module.css b/src/renderer/components/delete-cluster-dialog/delete-cluster-dialog.module.css new file mode 100644 index 0000000000..da7668a676 --- /dev/null +++ b/src/renderer/components/delete-cluster-dialog/delete-cluster-dialog.module.css @@ -0,0 +1,46 @@ +.code { + @apply block overflow-auto whitespace-nowrap; + background: #e0e0e0; + padding: 6px 8px; + color: #4c4c4c; + font-size: 0.9em; +} + +.warning { + @apply mt-4 flex; + padding: 10px 16px; + background: #fad8d7; + color: #797979; + border-radius: 3px; +} + +.warningIcon { + @apply mr-5 mt-2.5; + font-size: 26px; +} + +.dialog { + > div { + max-width: 50vw; + min-width: calc(45 * var(--unit)); + background-color: white; + border-radius: 3px; + } +} + +.dialogContent { + padding: 24px; + line-height: 1.4; +} + +.dialogButtons { + background: #f4f4f4; + padding: 16px; + display: flex; + justify-content: flex-end; + border-radius: 3px; + + > * { + margin-left: var(--margin) + } +} \ No newline at end of file diff --git a/src/renderer/components/delete-cluster-dialog/delete-cluster-dialog.tsx b/src/renderer/components/delete-cluster-dialog/delete-cluster-dialog.tsx new file mode 100644 index 0000000000..ea36ad6617 --- /dev/null +++ b/src/renderer/components/delete-cluster-dialog/delete-cluster-dialog.tsx @@ -0,0 +1,129 @@ +/** + * Copyright (c) 2021 OpenLens Authors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +import styles from "./delete-cluster-dialog.module.css"; + +import { observable } from "mobx"; +import { observer } from "mobx-react"; +import React from "react"; + +import { Button } from "../button"; +import type { KubeConfig } from "@kubernetes/client-node"; +import type { Cluster } from "../../../main/cluster"; +import { saveKubeconfig } from "./save-config"; +import { requestMain } from "../../../common/ipc"; +import { clusterClearDeletingHandler, clusterDeleteHandler, clusterSetDeletingHandler } from "../../../common/cluster-ipc"; +import { Notifications } from "../notifications"; +import { HotbarStore } from "../../../common/hotbar-store"; +import { boundMethod } from "autobind-decorator"; +import { Dialog } from "../dialog"; +import { Icon } from "../icon"; + +type DialogState = { + isOpen: boolean, + config?: KubeConfig, + cluster?: Cluster +}; + +const dialogState: DialogState = observable({ + isOpen: false +}); + +@observer +export class DeleteClusterDialog extends React.Component { + static open({ config, cluster }: Partial) { + dialogState.isOpen = true; + dialogState.config = config; + dialogState.cluster = cluster; + } + + static close() { + dialogState.isOpen = false; + } + + onClose() { + DeleteClusterDialog.close(); + } + + @boundMethod + async onDelete() { + await requestMain(clusterSetDeletingHandler, dialogState.cluster.id); + dialogState.config.contexts = dialogState.config.contexts.filter(item => item.name !== dialogState.cluster.contextName); + + try { + await saveKubeconfig(dialogState.config, dialogState.cluster.kubeConfigPath); + HotbarStore.getInstance().removeAllHotbarItems(dialogState.cluster.id); + await requestMain(clusterDeleteHandler, dialogState.cluster.id); + } catch(error) { + Notifications.error(`Cannot remove cluster, failed to process config file. ${error}`); + await requestMain(clusterClearDeletingHandler, dialogState.cluster.id); + } + + this.onClose(); + } + + render() { + const { cluster, config, isOpen } = dialogState; + + if (!cluster || !config) return null; + + const isCurrentContext = config.currentContext == cluster.contextName; + // const contexts = config.contexts.filter(context => context.name !== cluster.contextName); + + const warning = isCurrentContext ? ( + <> +

The current-context field from the kubeconfig file indicates a minikube context + that will cease to exist after the change. This will affect the operation of kubectl. + {" "}Replace current context

+ + ) : ( +

The contents of your kubeconfig file will be changed!

+ ); + + return ( + +
+
+ Delete the {cluster.getMeta().name} context from {cluster.kubeConfigPath}? +
+
+ + {warning} +
+
+
+
+
+ ); + } +} diff --git a/src/renderer/components/delete-cluster-dialog/index.ts b/src/renderer/components/delete-cluster-dialog/index.ts new file mode 100644 index 0000000000..1966204130 --- /dev/null +++ b/src/renderer/components/delete-cluster-dialog/index.ts @@ -0,0 +1,22 @@ +/** + * Copyright (c) 2021 OpenLens Authors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +export * from "./delete-cluster-dialog";