// This file is part of MinIO Console Server // Copyright (c) 2022 MinIO, Inc. // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . import { createSlice, PayloadAction } from "@reduxjs/toolkit"; import { snackBarMessage, SRInfoStateType } from "./types"; import { ErrorResponseHandler, IEmbeddedCustomStyles } from "./common/types"; import { AppState } from "./store"; import { SubnetInfo } from "./screens/Console/License/types"; import { isDarkModeOn } from "./utils/stylesUtils"; // determine whether we have the sidebar state stored on localstorage const initSideBarOpen = localStorage.getItem("sidebarOpen") ? JSON.parse(localStorage.getItem("sidebarOpen")!)["open"] : true; export interface SystemState { value: number; loggedIn: boolean; showMarketplace: boolean; sidebarOpen: boolean; userName: string; serverNeedsRestart: boolean; serverIsLoading: boolean; loadingConfigurations: boolean; loadingProgress: number; snackBar: snackBarMessage; modalSnackBar: snackBarMessage; serverDiagnosticStatus: string; distributedSetup: boolean; siteReplicationInfo: SRInfoStateType; licenseInfo: null | SubnetInfo; overrideStyles: null | IEmbeddedCustomStyles; anonymousMode: boolean; helpName: string; helpTabName: string; locationPath: string; darkMode: boolean; } const initialState: SystemState = { value: 0, loggedIn: false, showMarketplace: false, userName: "", sidebarOpen: initSideBarOpen, siteReplicationInfo: { siteName: "", curSite: false, enabled: false }, serverNeedsRestart: false, serverIsLoading: false, loadingConfigurations: true, loadingProgress: 100, snackBar: { message: "", detailedErrorMsg: "", type: "message", }, modalSnackBar: { message: "", detailedErrorMsg: "", type: "message", }, serverDiagnosticStatus: "", distributedSetup: false, licenseInfo: null, overrideStyles: null, anonymousMode: false, helpName: "help", helpTabName: "docs", locationPath: "", darkMode: isDarkModeOn(), }; export const systemSlice = createSlice({ name: "system", initialState, reducers: { userLogged: (state, action: PayloadAction) => { state.loggedIn = action.payload; }, showMarketplace: (state, action: PayloadAction) => { state.showMarketplace = action.payload; }, menuOpen: (state, action: PayloadAction) => { // persist preference to local storage localStorage.setItem( "sidebarOpen", JSON.stringify({ open: action.payload }), ); state.sidebarOpen = action.payload; }, setServerNeedsRestart: (state, action: PayloadAction) => { state.serverNeedsRestart = action.payload; }, serverIsLoading: (state, action: PayloadAction) => { state.serverIsLoading = action.payload; }, configurationIsLoading: (state, action: PayloadAction) => { state.loadingConfigurations = action.payload; }, setLoadingProgress: (state, action: PayloadAction) => { state.loadingProgress = action.payload; }, setSnackBarMessage: (state, action: PayloadAction) => { state.snackBar = { message: action.payload, detailedErrorMsg: "", type: "message", }; }, setErrorSnackMessage: ( state, action: PayloadAction, ) => { state.snackBar = { message: action.payload.errorMessage, detailedErrorMsg: action.payload.detailedError, type: "error", }; }, setModalSnackMessage: (state, action: PayloadAction) => { state.modalSnackBar = { message: action.payload, detailedErrorMsg: "", type: "message", }; }, setModalErrorSnackMessage: ( state, action: PayloadAction<{ errorMessage: string; detailedError: string }>, ) => { state.modalSnackBar = { message: action.payload.errorMessage, detailedErrorMsg: action.payload.detailedError, type: "error", }; }, setServerDiagStat: (state, action: PayloadAction) => { state.serverDiagnosticStatus = action.payload; }, globalSetDistributedSetup: (state, action: PayloadAction) => { state.distributedSetup = action.payload; }, setSiteReplicationInfo: (state, action: PayloadAction) => { state.siteReplicationInfo = action.payload; }, setSystemLicenseInfo: (state, action: PayloadAction) => { state.licenseInfo = action.payload; }, setHelpName: (state, action: PayloadAction) => { state.helpName = action.payload; }, setHelpTabName: (state, action: PayloadAction) => { state.helpTabName = action.payload; }, setOverrideStyles: ( state, action: PayloadAction, ) => { state.overrideStyles = action.payload; }, setAnonymousMode: (state) => { state.anonymousMode = true; state.loggedIn = true; }, setLocationPath: (state, action: PayloadAction) => { state.locationPath = action.payload; }, setDarkMode: (state, action: PayloadAction) => { state.darkMode = action.payload; }, resetSystem: () => { return initialState; }, }, }); // Action creators are generated for each case reducer function export const { userLogged, showMarketplace, menuOpen, setServerNeedsRestart, serverIsLoading, setLoadingProgress, setSnackBarMessage, setErrorSnackMessage, setModalErrorSnackMessage, setModalSnackMessage, setServerDiagStat, globalSetDistributedSetup, setSiteReplicationInfo, setSystemLicenseInfo, setOverrideStyles, setAnonymousMode, resetSystem, configurationIsLoading, setHelpName, setHelpTabName, setLocationPath, setDarkMode, } = systemSlice.actions; export const selDistSet = (state: AppState) => state.system.distributedSetup; export const selSiteRep = (state: AppState) => state.system.siteReplicationInfo; export default systemSlice.reducer;