|
| 1 | +/* |
| 2 | + * Copyright 2025 The Kubernetes Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { |
| 18 | + Button, |
| 19 | + Checkbox, |
| 20 | + Dialog, |
| 21 | + DialogActions, |
| 22 | + DialogContent, |
| 23 | + DialogContentText, |
| 24 | + FormControlLabel, |
| 25 | + Typography, |
| 26 | +} from '@mui/material'; |
| 27 | +import React, { useState } from 'react'; |
| 28 | +import { useTranslation } from 'react-i18next'; |
| 29 | +import { useDispatch } from 'react-redux'; |
| 30 | +import Namespace from '../../lib/k8s/namespace'; |
| 31 | +import { clusterAction } from '../../redux/clusterActionSlice'; |
| 32 | +import { ProjectDefinition } from '../../redux/projectsSlice'; |
| 33 | +import { AppDispatch } from '../../redux/stores/store'; |
| 34 | +import { DialogTitle } from '../common/Dialog'; |
| 35 | +import AuthVisible from '../common/Resource/AuthVisible'; |
| 36 | +import { PROJECT_ID_LABEL } from './projectUtils'; |
| 37 | + |
| 38 | +interface ProjectDeleteDialogProps { |
| 39 | + open: boolean; |
| 40 | + project: ProjectDefinition; |
| 41 | + onClose: () => void; |
| 42 | + namespaces: Namespace[]; |
| 43 | +} |
| 44 | + |
| 45 | +export function ProjectDeleteDialog({ |
| 46 | + open, |
| 47 | + project, |
| 48 | + onClose, |
| 49 | + namespaces, |
| 50 | +}: ProjectDeleteDialogProps) { |
| 51 | + const { t } = useTranslation(); |
| 52 | + const dispatch: AppDispatch = useDispatch(); |
| 53 | + const [deleteNamespaces, setDeleteNamespaces] = useState(false); |
| 54 | + |
| 55 | + const handleDelete = () => { |
| 56 | + const projectNamespaces = namespaces.filter(ns => |
| 57 | + project.namespaces.includes(ns.metadata.name) |
| 58 | + ); |
| 59 | + |
| 60 | + dispatch( |
| 61 | + clusterAction( |
| 62 | + async () => { |
| 63 | + if (deleteNamespaces) { |
| 64 | + // Delete the entire namespaces |
| 65 | + await Promise.all(projectNamespaces.map(namespace => namespace.delete())); |
| 66 | + } else { |
| 67 | + // Just remove the project label from each namespace |
| 68 | + await Promise.all( |
| 69 | + projectNamespaces.map(async namespace => { |
| 70 | + const updatedData = { ...namespace.jsonData }; |
| 71 | + if (updatedData.metadata?.labels) { |
| 72 | + delete updatedData.metadata.labels[PROJECT_ID_LABEL]; |
| 73 | + // If labels object becomes empty, remove it entirely |
| 74 | + if (Object.keys(updatedData.metadata.labels).length === 0) { |
| 75 | + delete updatedData.metadata.labels; |
| 76 | + } |
| 77 | + } |
| 78 | + return namespace.update(updatedData); |
| 79 | + }) |
| 80 | + ); |
| 81 | + } |
| 82 | + }, |
| 83 | + { |
| 84 | + startMessage: t('Deleting project {{ projectName }}…', { projectName: project.id }), |
| 85 | + cancelledMessage: t('Cancelled deletion of project {{ projectName }}.', { |
| 86 | + projectName: project.id, |
| 87 | + }), |
| 88 | + successMessage: t('Deleted project {{ projectName }}.', { projectName: project.id }), |
| 89 | + errorMessage: t('Error deleting project {{ projectName }}.', { projectName: project.id }), |
| 90 | + successUrl: '/', |
| 91 | + } |
| 92 | + ) |
| 93 | + ); |
| 94 | + |
| 95 | + onClose(); |
| 96 | + }; |
| 97 | + |
| 98 | + return ( |
| 99 | + <Dialog |
| 100 | + open={open} |
| 101 | + onClose={onClose} |
| 102 | + aria-labelledby="project-delete-dialog-title" |
| 103 | + aria-describedby="project-delete-dialog-description" |
| 104 | + maxWidth="sm" |
| 105 | + fullWidth |
| 106 | + > |
| 107 | + <DialogTitle id="project-delete-dialog-title">{t('Delete Project')}</DialogTitle> |
| 108 | + <DialogContent> |
| 109 | + <DialogContentText id="project-delete-dialog-description" component="div"> |
| 110 | + <Typography variant="body1" paragraph> |
| 111 | + {t('Are you sure you want to delete project "{{projectName}}"?', { |
| 112 | + projectName: project.id, |
| 113 | + })} |
| 114 | + </Typography> |
| 115 | + |
| 116 | + <Typography variant="body2" paragraph> |
| 117 | + {t( |
| 118 | + 'By default, this will only remove the project label from the following namespaces:' |
| 119 | + )} |
| 120 | + </Typography> |
| 121 | + |
| 122 | + <ul> |
| 123 | + {project.namespaces.map(namespaceName => ( |
| 124 | + <li key={namespaceName}> |
| 125 | + <strong>{namespaceName}</strong> |
| 126 | + </li> |
| 127 | + ))} |
| 128 | + </ul> |
| 129 | + |
| 130 | + <AuthVisible item={namespaces[0]} authVerb="delete"> |
| 131 | + <FormControlLabel |
| 132 | + control={ |
| 133 | + <Checkbox |
| 134 | + checked={deleteNamespaces} |
| 135 | + onChange={e => setDeleteNamespaces(e.target.checked)} |
| 136 | + name="deleteNamespaces" |
| 137 | + color="primary" |
| 138 | + /> |
| 139 | + } |
| 140 | + label={ |
| 141 | + <Typography variant="body2"> |
| 142 | + {t('Also delete the namespaces (this will remove all resources within them)')} |
| 143 | + </Typography> |
| 144 | + } |
| 145 | + /> |
| 146 | + |
| 147 | + {deleteNamespaces && ( |
| 148 | + <Typography variant="body2" color="error" sx={{ mt: 1, fontWeight: 'bold' }}> |
| 149 | + {t( |
| 150 | + 'Warning: This action cannot be undone. All resources in these namespaces will be permanently deleted.' |
| 151 | + )} |
| 152 | + </Typography> |
| 153 | + )} |
| 154 | + </AuthVisible> |
| 155 | + </DialogContentText> |
| 156 | + </DialogContent> |
| 157 | + <DialogActions> |
| 158 | + <Button onClick={onClose} color="secondary" variant="contained"> |
| 159 | + {t('Cancel')} |
| 160 | + </Button> |
| 161 | + <Button |
| 162 | + onClick={handleDelete} |
| 163 | + color="primary" |
| 164 | + variant="contained" |
| 165 | + sx={{ minWidth: '200px' }} // Fixed width to prevent button resize |
| 166 | + > |
| 167 | + {deleteNamespaces ? t('Delete Project & Namespaces') : t('Delete Project')} |
| 168 | + </Button> |
| 169 | + </DialogActions> |
| 170 | + </Dialog> |
| 171 | + ); |
| 172 | +} |
0 commit comments