/** * Copyright (c) OpenLens Authors. All rights reserved. * Licensed under MIT License. See LICENSE in root directory for more information. */ import styles from "./delete-cluster-dialog.module.scss"; import { computed, makeObservable, 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 { boundMethod } from "autobind-decorator"; import { Dialog } from "../dialog"; import { Icon } from "../icon"; import { Select } from "../select"; import { Checkbox } from "../checkbox"; import { requestClearClusterAsDeleting, requestDeleteCluster, requestSetClusterAsDeleting } from "../../ipc"; import { withInjectables } from "@ogre-tools/injectable-react"; import hotbarStoreInjectable from "../../../common/hotbar-store.injectable"; import type { DeleteClusterDialogModel } from "./delete-cluster-dialog-model/delete-cluster-dialog-model"; import deleteClusterDialogModelInjectable from "./delete-cluster-dialog-model/delete-cluster-dialog-model.injectable"; import type { HotbarStore } from "../../../common/hotbar-store"; interface Dependencies { hotbarStore: HotbarStore; model: DeleteClusterDialogModel; } @observer class NonInjectedDeleteClusterDialog extends React.Component { @observable showContextSwitch = false; @observable newCurrentContext = ""; constructor(props: Dependencies) { super(props); makeObservable(this); } @boundMethod onOpen() { this.newCurrentContext = ""; if (this.isCurrentContext()) { this.showContextSwitch = true; } } @boundMethod onClose() { this.props.model.close(); this.showContextSwitch = false; } removeContext() { this.props.model.config.contexts = this.props.model.config.contexts.filter(item => item.name !== this.props.model.cluster.contextName, ); } changeCurrentContext() { if (this.newCurrentContext && this.showContextSwitch) { this.props.model.config.currentContext = this.newCurrentContext; } } @boundMethod async onDelete() { const { cluster, config } = this.props.model; await requestSetClusterAsDeleting(cluster.id); this.removeContext(); this.changeCurrentContext(); 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(); } @computed get disableDelete() { const { cluster, config } = this.props.model; const noContextsAvailable = config.contexts.filter(context => context.name !== cluster.contextName).length == 0; const newContextNotSelected = this.newCurrentContext === ""; if (noContextsAvailable) { return false; } return this.showContextSwitch && newContextNotSelected; } isCurrentContext() { return this.props.model.config.currentContext == this.props.model.cluster.contextName; } renderCurrentContextSwitch() { if (!this.showContextSwitch) return null; const { cluster, config } = this.props.model; const contexts = config.contexts.filter(context => context.name !== cluster.contextName); const options = [ ...contexts.map(context => ({ label: context.name, value: context.name, })), ]; return (