UNPKG

@kinvolk/headlamp-plugin

Version:

The needed infrastructure for building Headlamp plugins.

102 lines (101 loc) 4.02 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 { has } from 'lodash'; import React, { isValidElement } from 'react'; import { DefaultHeaderAction, } from '../../../../redux/actionButtonsSlice'; import { useTypedSelector } from '../../../../redux/reducers/reducers'; import ErrorBoundary from '../../ErrorBoundary'; import SectionHeader from '../../SectionHeader'; import DeleteButton from '../DeleteButton'; import EditButton from '../EditButton'; import { RestartButton } from '../RestartButton'; import ScaleButton from '../ScaleButton'; export function MainInfoHeader(props) { const { resource, title, actions = [], headerStyle = 'main', noDefaultActions = false } = props; const headerActions = useTypedSelector(state => state.actionButtons.headerActions); const headerActionsProcessors = useTypedSelector(state => state.actionButtons.headerActionsProcessors); function setupAction(headerAction) { let Action = has(headerAction, 'action') ? headerAction.action : headerAction; if (!noDefaultActions && has(headerAction, 'id')) { switch (headerAction.id) { case DefaultHeaderAction.RESTART: Action = RestartButton; break; case DefaultHeaderAction.SCALE: Action = ScaleButton; break; case DefaultHeaderAction.EDIT: Action = EditButton; break; case DefaultHeaderAction.DELETE: Action = DeleteButton; break; default: break; } } if (!Action || headerAction.action === null) { return null; } if (isValidElement(Action)) { return _jsx(ErrorBoundary, { children: Action }); } else if (Action === null) { return null; } else if (typeof Action === 'function') { return (_jsx(ErrorBoundary, { children: _jsx(Action, { item: resource }) })); } } const defaultActions = [ { id: DefaultHeaderAction.RESTART, }, { id: DefaultHeaderAction.SCALE, }, { id: DefaultHeaderAction.EDIT, }, { id: DefaultHeaderAction.DELETE, }, ]; let hAccs = []; const accs = typeof actions === 'function' ? actions(resource) || [] : actions; if (accs !== null) { hAccs = [...accs].map((action, i) => { if (action?.id !== undefined) { return action; } else { return { id: `gen-${i}`, action: action }; } }); } let actionsProcessed = [...headerActions, ...hAccs, ...defaultActions]; if (headerActionsProcessors.length > 0) { for (const headerProcessor of headerActionsProcessors) { actionsProcessed = headerProcessor.processor(resource, actionsProcessed); } } const allActions = React.Children.toArray((function propsActions() { const pluginAddedActions = actionsProcessed.map(setupAction); return React.Children.toArray(pluginAddedActions); })()); return (_jsx(SectionHeader, { title: title || (resource ? `${resource.kind}: ${resource.getName()}` : ''), headerStyle: headerStyle, actions: allActions })); }