UNPKG

@kinvolk/headlamp-plugin

Version:

The needed infrastructure for building Headlamp plugins.

80 lines (79 loc) 3.87 kB
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } 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 _ from 'lodash'; 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'; function DeleteMultipleButtonDescription(props) { const { t } = useTranslation(['translation']); return (_jsxs("p", { children: [t('Are you sure you want to delete the following items?'), _jsx("ul", { children: props.items?.map(item => (_jsx("li", { children: item.metadata.name }, item.metadata.uid))) })] })); } export default function DeleteMultipleButton(props) { const dispatch = useDispatch(); const settingsObj = useSettings(); const { items, options, afterConfirm, buttonStyle } = props; const [openAlert, setOpenAlert] = React.useState(false); const { t } = useTranslation(['translation']); const location = useLocation(); const dispatchDeleteEvent = useEventCallback(HeadlampEventType.DELETE_RESOURCES); const deleteFunc = React.useCallback((items) => { if (!items || items.length === 0) { return; } const clonedItems = _.cloneDeep(items); const itemsLength = clonedItems.length; dispatch(clusterAction(async () => { await Promise.all(items.map(item => { if (settingsObj.useEvict && item.kind === 'Pod') { const pod = item; return pod.evict(); } return item.delete(); })); }, { startMessage: t('Deleting {{ itemsLength }} items…', { itemsLength }), cancelledMessage: t('Cancelled deletion of {{ itemsLength }} items.', { itemsLength }), successMessage: t('Deleted {{ itemsLength }} items.', { itemsLength }), errorMessage: t('Error deleting {{ itemsLength }} items.', { itemsLength }), cancelUrl: location.pathname, startUrl: location.pathname, errorUrl: location.pathname, ...options, })); }, [options]); if (!items || items.length === 0) { return null; } return (_jsxs(_Fragment, { children: [_jsx(ActionButton, { description: t('translation|Delete items'), buttonStyle: buttonStyle, onClick: () => { setOpenAlert(true); }, icon: "mdi:delete" }), _jsx(ConfirmDialog, { open: openAlert, title: t('translation|Delete items'), description: _jsx(DeleteMultipleButtonDescription, { items: items }), handleClose: () => setOpenAlert(false), onConfirm: () => { deleteFunc(items); dispatchDeleteEvent({ resources: items, status: EventStatus.CONFIRMED, }); if (afterConfirm) { afterConfirm(); } } })] })); }