UNPKG

@kinvolk/headlamp-plugin

Version:

The needed infrastructure for building Headlamp plugins.

92 lines (91 loc) 4.8 kB
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 FormControlLabel from '@mui/material/FormControlLabel'; import Grid from '@mui/material/Grid'; import Switch from '@mui/material/Switch'; import React from 'react'; import { useTranslation } from 'react-i18next'; import { useDispatch } from 'react-redux'; import { useLocation } from 'react-router-dom'; import { clusterAction } from '../../../redux/clusterActionSlice'; import { EventStatus, HeadlampEventType, useEventCallback, } from '../../../redux/headlampEventSlice'; import { useSettings } from '../../App/Settings/hook'; import ActionButton from '../ActionButton'; import { ConfirmDialog } from '../Dialog'; import AuthVisible from './AuthVisible'; export default function DeleteButton(props) { const dispatch = useDispatch(); const settingsObj = useSettings(); const { item, options, buttonStyle, afterConfirm } = props; const [openAlert, setOpenAlert] = React.useState(false); const [forceDelete, setForceDelete] = React.useState(false); const location = useLocation(); const { t } = useTranslation(['translation']); const dispatchDeleteEvent = useEventCallback(HeadlampEventType.DELETE_RESOURCE); const deleteFunc = React.useCallback(() => { if (!item) { return; } let callback = item.delete; if (settingsObj.useEvict && item.kind === 'Pod') { const pod = item; callback = pod.evict; } const itemName = item.metadata.name; callback && dispatch(clusterAction(callback.bind(item), { callbackArgs: [forceDelete], startMessage: t('Deleting item {{ itemName }}…', { itemName }), cancelledMessage: t('Cancelled deletion of {{ itemName }}.', { itemName }), successMessage: t('Deleted item {{ itemName }}.', { itemName }), errorMessage: t('Error deleting item {{ itemName }}.', { itemName }), cancelUrl: location.pathname, startUrl: item.getListLink(), errorUrl: item.getListLink(), ...options, })); }, // eslint-disable-next-line [item, forceDelete]); if (!item) { return null; } return (_jsxs(AuthVisible, { item: item, authVerb: "delete", onError: (err) => { console.error(`Error while getting authorization for delete button in ${item}:`, err); }, children: [_jsx(ActionButton, { description: settingsObj.useEvict && item.kind === 'Pod' ? t('translation|Evict') : t('translation|Delete'), buttonStyle: buttonStyle, onClick: () => { setOpenAlert(true); }, icon: "mdi:delete" }), _jsx(ConfirmDialog, { open: openAlert, title: settingsObj.useEvict && item.kind === 'Pod' ? t('translation|Evict Pod') : t('translation|Delete item'), description: _jsxs(Grid, { container: true, direction: "column", children: [_jsx(Grid, { item: true, children: settingsObj.useEvict && item.kind === 'Pod' ? t('translation|Are you sure you want to evict pod {{ itemName }}?', { itemName: item.metadata.name, }) : t('translation|Are you sure you want to delete item {{ itemName }}?', { itemName: item.metadata.name, }) }), (!settingsObj.useEvict || item.kind !== 'Pod') && (_jsx(Grid, { item: true, children: _jsx(FormControlLabel, { control: _jsx(Switch, { checked: forceDelete, onChange: () => setForceDelete(!forceDelete), name: "forceDelete" }), label: t('Force Delete') }) }))] }), handleClose: () => setOpenAlert(false), onConfirm: () => { deleteFunc(); dispatchDeleteEvent({ resource: item, status: EventStatus.CONFIRMED, }); if (afterConfirm) { afterConfirm(); } } })] })); }