/** * Copyright (c) OpenLens Authors. All rights reserved. * Licensed under MIT License. See LICENSE in root directory for more information. */ import "./config-map-details.scss"; import React from "react"; import { autorun, makeObservable, observable } from "mobx"; import { disposeOnUnmount, observer } from "mobx-react"; import { DrawerTitle } from "../drawer"; import type { ShowNotification } from "../notifications"; import { Button } from "../button"; import type { KubeObjectDetailsProps } from "../kube-object-details"; import { ConfigMap } from "../../../common/k8s-api/endpoints"; import type { Logger } from "../../../common/logger"; import type { ConfigMapStore } from "./store"; import { withInjectables } from "@ogre-tools/injectable-react"; import configMapStoreInjectable from "./store.injectable"; import showSuccessNotificationInjectable from "../notifications/show-success-notification.injectable"; import showErrorNotificationInjectable from "../notifications/show-error-notification.injectable"; import loggerInjectable from "../../../common/logger.injectable"; import { MonacoEditor } from "../monaco-editor"; export interface ConfigMapDetailsProps extends KubeObjectDetailsProps { } interface Dependencies { configMapStore: ConfigMapStore; logger: Logger; showSuccessNotification: ShowNotification; showErrorNotification: ShowNotification; } @observer class NonInjectedConfigMapDetails extends React.Component { @observable isSaving = false; @observable data = observable.map(); constructor(props: ConfigMapDetailsProps & Dependencies) { super(props); makeObservable(this); } async componentDidMount() { disposeOnUnmount(this, [ autorun(() => { const { object: configMap } = this.props; if (configMap) { this.data.replace(configMap.data); // refresh } }), ]); } save = async () => { const { object: configMap, configMapStore } = this.props; try { this.isSaving = true; await configMapStore.update(configMap, { ...configMap, data: Object.fromEntries(this.data), }); this.props.showSuccessNotification((

{"ConfigMap "} {configMap.getName()} {" successfully updated."}

)); } catch (error) { this.props.showErrorNotification(`Failed to save config map: ${error}`); } finally { this.isSaving = false; } }; render() { const { object: configMap, logger } = this.props; if (!configMap) { return null; } if (!(configMap instanceof ConfigMap)) { logger.error("[ConfigMapDetails]: passed object that is not an instanceof ConfigMap", configMap); return null; } const data = Array.from(this.data.entries()); return (
{ data.length > 0 && ( <> Data { data.map(([name, value]) => (
{name}
this.data.set(name, v)} setInitialHeight />
)) }
); } } export const ConfigMapDetails = withInjectables(NonInjectedConfigMapDetails, { getProps: (di, props) => ({ ...props, configMapStore: di.inject(configMapStoreInjectable), showSuccessNotification: di.inject(showSuccessNotificationInjectable), showErrorNotification: di.inject(showErrorNotificationInjectable), logger: di.inject(loggerInjectable), }), });