UNPKG

@kinvolk/headlamp-plugin

Version:

The needed infrastructure for building Headlamp plugins.

76 lines (75 loc) 3.09 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 const defaultTableRowsPerPageOptions = [15, 25, 50]; function defaultTimezone() { return import.meta.env.UNDER_TEST ? 'UTC' : Intl.DateTimeFormat().resolvedOptions().timeZone; } const storedSettings = JSON.parse(localStorage.getItem('settings') || '{}'); export const initialState = { clusters: null, statelessClusters: null, allClusters: null, isDynamicClusterEnabled: false, allowKubeconfigChanges: false, settings: { tableRowsPerPageOptions: storedSettings.tableRowsPerPageOptions || defaultTableRowsPerPageOptions, timezone: storedSettings.timezone || defaultTimezone(), sidebarSortAlphabetically: storedSettings.sidebarSortAlphabetically || false, useEvict: storedSettings.useEvict ?? true, }, }; const configSlice = createSlice({ name: 'config', initialState, reducers: { /** * Save the config. To both the store, and localStorage. * @param state - The current state. * @param action - The payload action containing the config. */ setConfig(state, action) { state.clusters = action.payload.clusters; if (action.payload.isDynamicClusterEnabled !== undefined) { state.isDynamicClusterEnabled = action.payload.isDynamicClusterEnabled; } if (action.payload.allowKubeconfigChanges !== undefined) { state.allowKubeconfigChanges = action.payload.allowKubeconfigChanges; } }, /** * Save the config. To both the store, and localStorage. * @param state - The current state. * @param action - The payload action containing the config. */ setStatelessConfig(state, action) { state.statelessClusters = action.payload.statelessClusters; }, /** * Save the settings. To both the store, and localStorage. * @param state - The current state. * @param action - The payload action containing the settings. */ setAppSettings(state, action) { Object.keys(action.payload).forEach(key => { state.settings[key] = action.payload[key]; }); localStorage.setItem('settings', JSON.stringify(state.settings)); }, }, }); export const { setConfig, setAppSettings, setStatelessConfig } = configSlice.actions; export default configSlice.reducer;