@kinvolk/headlamp-plugin
Version:
The needed infrastructure for building Headlamp plugins.
99 lines (98 loc) • 4.16 kB
JavaScript
/*
* 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 { createSlice } from '@reduxjs/toolkit';
import { get, set } from 'lodash';
export var DefaultHeaderAction;
(function (DefaultHeaderAction) {
DefaultHeaderAction["RESTART"] = "RESTART";
DefaultHeaderAction["DELETE"] = "DELETE";
DefaultHeaderAction["EDIT"] = "EDIT";
DefaultHeaderAction["VIEW"] = "VIEW";
DefaultHeaderAction["SCALE"] = "SCALE";
DefaultHeaderAction["DOWNLOAD"] = "DOWNLOAD";
DefaultHeaderAction["POD_LOGS"] = "POD_LOGS";
DefaultHeaderAction["POD_TERMINAL"] = "POD_TERMINAL";
DefaultHeaderAction["POD_DEBUG"] = "POD_DEBUG";
DefaultHeaderAction["POD_ATTACH"] = "POD_ATTACH";
DefaultHeaderAction["NODE_TOGGLE_CORDON"] = "NODE_TOGGLE_CORDON";
DefaultHeaderAction["NODE_DRAIN"] = "NODE_DRAIN";
DefaultHeaderAction["NODE_SHELL"] = "NODE_SHELL";
})(DefaultHeaderAction || (DefaultHeaderAction = {}));
export var DefaultAppBarAction;
(function (DefaultAppBarAction) {
DefaultAppBarAction["CLUSTER"] = "CLUSTER";
DefaultAppBarAction["NOTIFICATION"] = "NOTIFICATION";
DefaultAppBarAction["SETTINGS"] = "SETTINGS";
DefaultAppBarAction["USER"] = "USER";
DefaultAppBarAction["GLOBAL_SEARCH"] = "GLOBAL_SEARCH";
})(DefaultAppBarAction || (DefaultAppBarAction = {}));
const initialState = {
headerActions: [],
headerActionsProcessors: [],
appBarActions: [],
appBarActionsProcessors: [],
};
/**
* Normalizes a header actions processor by ensuring it has an 'id' and a processor function.
*
* If the processor is passed as a function, it will be wrapped in an object with a generated ID.
*
* @param action - The payload action containing the header actions processor.
* @returns The normalized header actions processor.
*/
function _normalizeProcessor(action) {
let headerActionsProcessor = action.payload;
if (get(headerActionsProcessor, 'id') === undefined &&
typeof headerActionsProcessor === 'function') {
const headerActionsProcessor2 = {
id: '',
processor: headerActionsProcessor,
};
headerActionsProcessor = headerActionsProcessor2;
}
set(headerActionsProcessor, 'id', get(headerActionsProcessor, 'id') || `generated-id-${Date.now().toString(36)}`);
return headerActionsProcessor;
}
export const actionButtonsSlice = createSlice({
name: 'actionButtons',
initialState,
reducers: {
setDetailsViewHeaderAction(state, action) {
let headerAction = action.payload;
if (headerAction.id === undefined) {
if (headerAction.action === undefined) {
headerAction = { id: '', action: headerAction };
}
else {
headerAction = { id: '', action: headerAction.action };
}
}
headerAction.id = headerAction.id || `generated-id-${Date.now().toString(36)}`;
state.headerActions.push(headerAction);
},
addDetailsViewHeaderActionsProcessor(state, action) {
state.headerActionsProcessors.push(_normalizeProcessor(action));
},
setAppBarAction(state, action) {
state.appBarActions.push(action.payload);
},
setAppBarActionsProcessor(state, action) {
state.appBarActionsProcessors.push(_normalizeProcessor(action));
},
},
});
export const { setDetailsViewHeaderAction, addDetailsViewHeaderActionsProcessor, setAppBarAction, setAppBarActionsProcessor, } = actionButtonsSlice.actions;
export default actionButtonsSlice.reducer;