UNPKG

@kinvolk/headlamp-plugin

Version:

The needed infrastructure for building Headlamp plugins.

112 lines (111 loc) 3.74 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; let openUserSelected = undefined; const storedSidebar = localStorage?.getItem('sidebar'); if (storedSidebar) { try { const parsed = JSON.parse(storedSidebar); if (parsed && typeof parsed === 'object' && 'shrink' in parsed && typeof parsed.shrink === 'boolean') { openUserSelected = !parsed.shrink; } } catch (e) { console.warn('Failed to parse sidebar local storage', e); } } if (openUserSelected !== undefined) { defaultOpen = openUserSelected; } else { // Check for build-time environment variable first const envDefaultOpen = import.meta.env.REACT_APP_HEADLAMP_SIDEBAR_DEFAULT_OPEN; if (envDefaultOpen !== undefined) { // Convert string to boolean, accepting 'true'/'false' or '1'/'0' defaultOpen = envDefaultOpen === 'true' || envDefaultOpen === '1'; } else { // Fall back to window width-based logic 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;