@kinvolk/headlamp-plugin
Version:
The needed infrastructure for building Headlamp plugins.
95 lines (94 loc) • 3.8 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 DefaultDetailsViewSection;
(function (DefaultDetailsViewSection) {
DefaultDetailsViewSection["METADATA"] = "METADATA";
DefaultDetailsViewSection["BACK_LINK"] = "BACK_LINK";
DefaultDetailsViewSection["MAIN_HEADER"] = "MAIN_HEADER";
DefaultDetailsViewSection["EVENTS"] = "EVENTS";
DefaultDetailsViewSection["ERROR"] = "ERROR";
DefaultDetailsViewSection["LOADING"] = "LOADING";
DefaultDetailsViewSection["CHILDREN"] = "CHILDREN";
})(DefaultDetailsViewSection || (DefaultDetailsViewSection = {}));
/**
* 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 defailsViewSectionsProcessor = action.payload;
if (get(defailsViewSectionsProcessor, 'id') === undefined &&
typeof defailsViewSectionsProcessor === 'function') {
const headerActionsProcessor2 = {
id: '',
processor: defailsViewSectionsProcessor,
};
defailsViewSectionsProcessor = headerActionsProcessor2;
}
set(defailsViewSectionsProcessor, 'id', get(defailsViewSectionsProcessor, 'id') || `generated-id-${Date.now().toString(36)}`);
return defailsViewSectionsProcessor;
}
const initialState = {
detailViews: [],
detailsViewSections: [],
detailsViewSectionsProcessors: [],
};
const detailsViewSectionSlice = createSlice({
name: 'detailsViewSection',
initialState,
reducers: {
/**
* Sets details view.
*/
setDetailsView(state, action) {
state.detailViews.push(action.payload);
},
/**
* Sets details view section.
*
* If the processor is passed as a function, it will be wrapped in an object with a generated ID.
*/
setDetailsViewSection(state, action) {
let section = action.payload;
if (section.id === undefined) {
if (section.section === undefined) {
section = { id: '', section: section };
}
else {
section = { id: '', section: section.section };
}
}
section.id = section.id || `generated-id-${Date.now().toString(36)}`;
state.detailsViewSections.push(section);
},
/**
* Adds a details view sections processor.
*
* If the processor is passed as a function, it will be wrapped in an object with a generated ID.
*/
addDetailsViewSectionsProcessor(state, action) {
state.detailsViewSectionsProcessors.push(_normalizeProcessor(action));
},
},
});
export const { addDetailsViewSectionsProcessor, setDetailsView, setDetailsViewSection } = detailsViewSectionSlice.actions;
export { detailsViewSectionSlice };
export default detailsViewSectionSlice.reducer;