/** * Copyright (c) OpenLens Authors. All rights reserved. * Licensed under MIT License. See LICENSE in root directory for more information. */ import styles from "./view.module.scss"; import type { IObservableValue } from "mobx"; import { action, observable } from "mobx"; import { observer } from "mobx-react"; import React from "react"; import { Button } from "../button"; import { saveKubeconfig } from "./save-config"; import { Notifications } from "../notifications"; import { Dialog } from "../dialog"; import { Icon } from "../icon"; import { Select } from "../select"; import { Checkbox } from "../checkbox"; import { requestClearClusterAsDeleting, requestDeleteCluster, requestSetClusterAsDeleting } from "../../ipc"; import type { HotbarStore } from "../../../common/hotbars/store"; import { withInjectables } from "@ogre-tools/injectable-react"; import hotbarStoreInjectable from "../../../common/hotbars/store.injectable"; import type { DeleteClusterDialogState } from "./state.injectable"; import deleteClusterDialogStateInjectable from "./state.injectable"; export interface Dependencies { state: IObservableValue; hotbarStore: HotbarStore; } @observer class NonInjectedDeleteClusterDialog extends React.Component { private readonly showContextSwitch = observable.box(false); private readonly newCurrentContext = observable.box(); @action onOpen(state: DeleteClusterDialogState) { this.newCurrentContext.set(""); this.showContextSwitch.set(this.isCurrentContext(state)); } onClose = () => { this.showContextSwitch.set(false); }; removeContext(state: DeleteClusterDialogState) { state.config.contexts = state.config.contexts.filter(item => item.name !== state.cluster.contextName, ); } changeCurrentContext(state: DeleteClusterDialogState) { const newCurrentContext = this.newCurrentContext.get(); const showContextSwitch = this.showContextSwitch.get(); if (newCurrentContext && showContextSwitch) { state.config.currentContext = newCurrentContext; } } async onDelete(state: DeleteClusterDialogState) { const { cluster, config } = state; await requestSetClusterAsDeleting(cluster.id); this.removeContext(state); this.changeCurrentContext(state); try { await saveKubeconfig(config, cluster.kubeConfigPath); this.props.hotbarStore.removeAllHotbarItems(cluster.id); await requestDeleteCluster(cluster.id); } catch(error) { Notifications.error(`Cannot remove cluster, failed to process config file. ${error}`); } finally { await requestClearClusterAsDeleting(cluster.id); } this.onClose(); } shouldDeleteBeDisabled({ cluster, config }: DeleteClusterDialogState): boolean { const noContextsAvailable = config.contexts.filter(context => context.name !== cluster.contextName).length == 0; const newContextNotSelected = this.newCurrentContext.get() === ""; const showContextSwitch = this.showContextSwitch.get(); if (noContextsAvailable) { return false; } return showContextSwitch && newContextNotSelected; } isCurrentContext({ cluster, config }: DeleteClusterDialogState) { return config.currentContext == cluster.contextName; } renderCurrentContextSwitch({ cluster, config }: DeleteClusterDialogState) { if (!this.showContextSwitch.get()) { return null; } const selectOptions = config .contexts .filter(context => context.name !== cluster.contextName) .map(context => ({ value: context.name, label: context.name, })); return (