UNPKG

@kinvolk/headlamp-plugin

Version:

The needed infrastructure for building Headlamp plugins.

90 lines (89 loc) 2.91 kB
/* * 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'; export var DefaultSidebars; (function (DefaultSidebars) { DefaultSidebars["HOME"] = "HOME"; DefaultSidebars["IN_CLUSTER"] = "IN-CLUSTER"; })(DefaultSidebars || (DefaultSidebars = {})); export function setInitialSidebarOpen() { let defaultOpen; const openUserSelected = localStorage?.getItem('sidebar') ? !JSON.parse(localStorage.getItem('sidebar')).shrink : undefined; if (openUserSelected !== undefined) { defaultOpen = openUserSelected; } else { defaultOpen = window?.innerWidth ? window.innerWidth > 960 : true; } return { isSidebarOpen: defaultOpen, isSidebarOpenUserSelected: undefined, }; } export const initialState = { selected: { item: null, sidebar: null, }, isVisible: false, ...setInitialSidebarOpen(), entries: {}, filters: [], }; const sidebarSlice = createSlice({ name: 'sidebar', initialState, reducers: { /** * Sets the selected item in the sidebar. */ setSidebarSelected(state, action) { state.selected = action.payload; state.isVisible = !!action.payload.item; }, /** * Sets the visibility of the sidebar. */ setSidebarVisible(state, action) { state.isVisible = action.payload; }, /** * Sets an item in the sidebar. */ setSidebarItem(state, action) { state.entries[action.payload.name] = action.payload; }, /** * Sets a filter for sidebar items. */ setSidebarItemFilter(state, action) { state.filters.push(action.payload); }, /** * Sets whether the sidebar is open or not. */ setWhetherSidebarOpen(state, action) { localStorage.setItem('sidebar', JSON.stringify({ shrink: !action.payload })); state.isSidebarOpen = action.payload; state.isSidebarOpenUserSelected = action.payload; }, }, }); export const { setSidebarSelected, setSidebarVisible, setSidebarItem, setSidebarItemFilter, setWhetherSidebarOpen, } = sidebarSlice.actions; export { sidebarSlice }; export default sidebarSlice.reducer;