UNPKG

@kinvolk/headlamp-plugin

Version:

The needed infrastructure for building Headlamp plugins.

111 lines (110 loc) 4.91 kB
import { jsx as _jsx } 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 { Icon } from '@iconify/react'; 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 { Activity } from '../../activity/Activity'; import ActionButton from '../ActionButton'; import AuthVisible from './AuthVisible'; import EditorDialog from './EditorDialog'; import ViewButton from './ViewButton'; export default function EditButton(props) { const dispatch = useDispatch(); const { item, options = {}, buttonStyle, afterConfirm } = props; const [isReadOnly, setIsReadOnly] = React.useState(false); const [errorMessage, setErrorMessage] = React.useState(''); const location = useLocation(); const { t } = useTranslation(['translation', 'resource']); const dispatchHeadlampEditEvent = useEventCallback(HeadlampEventType.EDIT_RESOURCE); const activityId = 'edit-' + item.metadata.uid; function makeErrorMessage(err) { const status = err.status; switch (status) { case 408: return 'Conflicts when trying to perform operation (code 408).'; default: return `Failed to perform operation: code ${status}`; } } async function updateFunc(newItem) { try { await item.update(newItem); Activity.close(activityId); } catch (err) { Activity.update(activityId, { minimized: false }); setErrorMessage(makeErrorMessage(err)); throw err; } } // eslint-disable-next-line react-hooks/exhaustive-deps const applyFunc = React.useCallback(updateFunc, [item]); function handleSave(items) { const newItemDef = Array.isArray(items) ? items[0] : items; const cancelUrl = location.pathname; const itemName = item.metadata.name; Activity.update(activityId, { minimized: true }); dispatch(clusterAction(() => applyFunc(newItemDef), { startMessage: t('translation|Applying changes to {{ itemName }}…', { itemName }), cancelledMessage: t('translation|Cancelled changes to {{ itemName }}.', { itemName }), successMessage: t('translation|Applied changes to {{ itemName }}.', { itemName }), errorMessage: t('translation|Failed to apply changes to {{ itemName }}.', { itemName }), cancelUrl, errorUrl: cancelUrl, ...options, })); dispatchHeadlampEditEvent({ resource: item, status: EventStatus.CLOSED, }); if (afterConfirm) { afterConfirm(); } } if (!item) { return null; } if (isReadOnly) { return _jsx(ViewButton, { item: item }); } return (_jsx(AuthVisible, { item: item, authVerb: "update", onError: (err) => { console.error(`Error while getting authorization for edit button in ${item}:`, err); setIsReadOnly(true); }, onAuthResult: ({ allowed }) => { setIsReadOnly(!allowed); }, children: _jsx(ActionButton, { description: t('translation|Edit'), buttonStyle: buttonStyle, onClick: () => { if (afterConfirm) { afterConfirm(); } Activity.launch({ id: activityId, title: t('translation|Edit') + ': ' + item.metadata.name, icon: _jsx(Icon, { icon: "mdi:pencil" }), cluster: item.cluster, content: (_jsx(EditorDialog, { noDialog: true, item: item.getEditableObject(), open: true, onClose: () => Activity.close(activityId), onSave: handleSave, allowToHideManagedFields: true, errorMessage: errorMessage, onEditorChanged: () => setErrorMessage('') })), location: 'full', }); dispatchHeadlampEditEvent({ resource: item, status: EventStatus.OPENED, }); }, icon: "mdi:pencil" }) })); }