@kinvolk/headlamp-plugin
Version:
The needed infrastructure for building Headlamp plugins.
65 lines (64 loc) • 4.93 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
/*
* Copyright 2025 The Kubernetes Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Button, Checkbox, Dialog, DialogActions, DialogContent, DialogContentText, FormControlLabel, Typography, } from '@mui/material';
import { useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useDispatch } from 'react-redux';
import { clusterAction } from '../../redux/clusterActionSlice';
import { DialogTitle } from '../common/Dialog';
import AuthVisible from '../common/Resource/AuthVisible';
import { PROJECT_ID_LABEL } from './projectUtils';
export function ProjectDeleteDialog({ open, project, onClose, namespaces, }) {
const { t } = useTranslation();
const dispatch = useDispatch();
const [deleteNamespaces, setDeleteNamespaces] = useState(false);
const handleDelete = () => {
const projectNamespaces = namespaces.filter(ns => project.namespaces.includes(ns.metadata.name));
dispatch(clusterAction(async () => {
if (deleteNamespaces) {
// Delete the entire namespaces
await Promise.all(projectNamespaces.map(namespace => namespace.delete()));
}
else {
// Just remove the project label from each namespace
await Promise.all(projectNamespaces.map(async (namespace) => {
const updatedData = { ...namespace.jsonData };
if (updatedData.metadata?.labels) {
delete updatedData.metadata.labels[PROJECT_ID_LABEL];
// If labels object becomes empty, remove it entirely
if (Object.keys(updatedData.metadata.labels).length === 0) {
delete updatedData.metadata.labels;
}
}
return namespace.update(updatedData);
}));
}
}, {
startMessage: t('Deleting project {{ projectName }}…', { projectName: project.id }),
cancelledMessage: t('Cancelled deletion of project {{ projectName }}.', {
projectName: project.id,
}),
successMessage: t('Deleted project {{ projectName }}.', { projectName: project.id }),
errorMessage: t('Error deleting project {{ projectName }}.', { projectName: project.id }),
successUrl: '/',
}));
onClose();
};
return (_jsxs(Dialog, { open: open, onClose: onClose, "aria-labelledby": "project-delete-dialog-title", "aria-describedby": "project-delete-dialog-description", maxWidth: "sm", fullWidth: true, children: [_jsx(DialogTitle, { id: "project-delete-dialog-title", children: t('Delete Project') }), _jsx(DialogContent, { children: _jsxs(DialogContentText, { id: "project-delete-dialog-description", component: "div", children: [_jsx(Typography, { variant: "body1", paragraph: true, children: t('Are you sure you want to delete project "{{projectName}}"?', {
projectName: project.id,
}) }), _jsx(Typography, { variant: "body2", paragraph: true, children: t('By default, this will only remove the project label from the following namespaces:') }), _jsx("ul", { children: project.namespaces.map(namespaceName => (_jsx("li", { children: _jsx("strong", { children: namespaceName }) }, namespaceName))) }), _jsxs(AuthVisible, { item: namespaces[0], authVerb: "delete", children: [_jsx(FormControlLabel, { control: _jsx(Checkbox, { checked: deleteNamespaces, onChange: e => setDeleteNamespaces(e.target.checked), name: "deleteNamespaces", color: "primary" }), label: _jsx(Typography, { variant: "body2", children: t('Also delete the namespaces (this will remove all resources within them)') }) }), deleteNamespaces && (_jsx(Typography, { variant: "body2", color: "error", sx: { mt: 1, fontWeight: 'bold' }, children: t('Warning: This action cannot be undone. All resources in these namespaces will be permanently deleted.') }))] })] }) }), _jsxs(DialogActions, { children: [_jsx(Button, { onClick: onClose, color: "secondary", variant: "contained", children: t('Cancel') }), _jsx(Button, { onClick: handleDelete, color: "primary", variant: "contained", sx: { minWidth: '200px' }, children: deleteNamespaces ? t('Delete Project & Namespaces') : t('Delete Project') })] })] }));
}