From 89e7cb79acc7ef879db43ae10ff83b5528a00bc8 Mon Sep 17 00:00:00 2001
From: Alex Andreev
Date: Fri, 20 Aug 2021 16:56:05 +0300
Subject: [PATCH] Switching current-context from dialog
Signed-off-by: Alex Andreev
---
.../delete-cluster-dialog.module.css | 2 +-
.../delete-cluster-dialog.tsx | 88 ++++++++++++++++---
2 files changed, 78 insertions(+), 12 deletions(-)
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
index da7668a676..1a5759642e 100644
--- a/src/renderer/components/delete-cluster-dialog/delete-cluster-dialog.module.css
+++ b/src/renderer/components/delete-cluster-dialog/delete-cluster-dialog.module.css
@@ -21,7 +21,7 @@
.dialog {
> div {
- max-width: 50vw;
+ max-width: 600px;
min-width: calc(45 * var(--unit));
background-color: white;
border-radius: 3px;
diff --git a/src/renderer/components/delete-cluster-dialog/delete-cluster-dialog.tsx b/src/renderer/components/delete-cluster-dialog/delete-cluster-dialog.tsx
index ea36ad6617..9285a1c9e7 100644
--- a/src/renderer/components/delete-cluster-dialog/delete-cluster-dialog.tsx
+++ b/src/renderer/components/delete-cluster-dialog/delete-cluster-dialog.tsx
@@ -20,12 +20,12 @@
*/
import styles from "./delete-cluster-dialog.module.css";
-import { observable } from "mobx";
+import { makeObservable, 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 { Context, KubeConfig } from "@kubernetes/client-node";
import type { Cluster } from "../../../main/cluster";
import { saveKubeconfig } from "./save-config";
import { requestMain } from "../../../common/ipc";
@@ -35,6 +35,7 @@ import { HotbarStore } from "../../../common/hotbar-store";
import { boundMethod } from "autobind-decorator";
import { Dialog } from "../dialog";
import { Icon } from "../icon";
+import { Select } from "../select";
type DialogState = {
isOpen: boolean,
@@ -46,8 +47,21 @@ const dialogState: DialogState = observable({
isOpen: false
});
+type Props = {};
+
@observer
export class DeleteClusterDialog extends React.Component {
+ showContextSwitch = false;
+ newCurrentContext = "";
+
+ constructor(props: Props) {
+ super(props);
+ makeObservable(this, {
+ showContextSwitch: observable,
+ newCurrentContext: observable
+ });
+ }
+
static open({ config, cluster }: Partial) {
dialogState.isOpen = true;
dialogState.config = config;
@@ -58,40 +72,90 @@ export class DeleteClusterDialog extends React.Component {
dialogState.isOpen = false;
}
+ @boundMethod
+ onOpen() {
+ this.showContextSwitch = false;
+ this.newCurrentContext = "";
+ }
+
onClose() {
DeleteClusterDialog.close();
}
+ removeContext() {
+ dialogState.config.contexts = dialogState.config.contexts.filter(item =>
+ item.name !== dialogState.cluster.contextName
+ );
+ }
+
+ changeCurrentContext() {
+ if (this.newCurrentContext) {
+ dialogState.config.currentContext = this.newCurrentContext;
+ }
+ }
+
@boundMethod
async onDelete() {
- await requestMain(clusterSetDeletingHandler, dialogState.cluster.id);
- dialogState.config.contexts = dialogState.config.contexts.filter(item => item.name !== dialogState.cluster.contextName);
+ const { cluster, config } = dialogState;
+
+ await requestMain(clusterSetDeletingHandler, cluster.id);
+ this.removeContext();
+ this.changeCurrentContext();
try {
- await saveKubeconfig(dialogState.config, dialogState.cluster.kubeConfigPath);
- HotbarStore.getInstance().removeAllHotbarItems(dialogState.cluster.id);
- await requestMain(clusterDeleteHandler, dialogState.cluster.id);
+ await saveKubeconfig(config, cluster.kubeConfigPath);
+ HotbarStore.getInstance().removeAllHotbarItems(cluster.id);
+ await requestMain(clusterDeleteHandler, cluster.id);
} catch(error) {
Notifications.error(`Cannot remove cluster, failed to process config file. ${error}`);
- await requestMain(clusterClearDeletingHandler, dialogState.cluster.id);
+ await requestMain(clusterClearDeletingHandler, cluster.id);
}
this.onClose();
}
+ renderCurrentContextSwitch(contexts: Context[]) {
+ if (!this.showContextSwitch) return null;
+
+ const options = [
+ ...contexts.map(context => ({
+ label: context.name,
+ value: context.name,
+ })),
+ ];
+
+ return (
+
+
Choose new current-context
+
this.newCurrentContext = value}
+ themeName="light"
+ className="ml-[1px] mr-[1px]"
+ />
+
+ )
+ }
+
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 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
+ that will cease to exist after the change. This will affect the operation of kubectl.{" "}
+ {contexts.length > 0 && (
+ this.showContextSwitch = !this.showContextSwitch}
+ >Replace current context
+ )}
+
>
) : (
The contents of your kubeconfig file will be changed!
@@ -102,6 +166,7 @@ export class DeleteClusterDialog extends React.Component {
className={styles.dialog}
isOpen={isOpen}
close={this.onClose}
+ open={this.onOpen}
>
@@ -111,6 +176,7 @@ export class DeleteClusterDialog extends React.Component {
{warning}
+ {this.renderCurrentContextSwitch(contexts)}