github.com/minio/console@v1.4.1/web-app/src/systemSlice.ts (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2022 MinIO, Inc.
     3  //
     4  // This program is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Affero General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // This program is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  // GNU Affero General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Affero General Public License
    15  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  import { createSlice, PayloadAction } from "@reduxjs/toolkit";
    17  import { snackBarMessage, SRInfoStateType } from "./types";
    18  import { ErrorResponseHandler, IEmbeddedCustomStyles } from "./common/types";
    19  import { AppState } from "./store";
    20  import { SubnetInfo } from "./screens/Console/License/types";
    21  import { isDarkModeOn } from "./utils/stylesUtils";
    22  
    23  // determine whether we have the sidebar state stored on localstorage
    24  const initSideBarOpen = localStorage.getItem("sidebarOpen")
    25    ? JSON.parse(localStorage.getItem("sidebarOpen")!)["open"]
    26    : true;
    27  
    28  export interface SystemState {
    29    value: number;
    30    loggedIn: boolean;
    31    showMarketplace: boolean;
    32    sidebarOpen: boolean;
    33    userName: string;
    34    serverNeedsRestart: boolean;
    35    serverIsLoading: boolean;
    36    loadingConfigurations: boolean;
    37    loadingProgress: number;
    38    snackBar: snackBarMessage;
    39    modalSnackBar: snackBarMessage;
    40    serverDiagnosticStatus: string;
    41    distributedSetup: boolean;
    42    siteReplicationInfo: SRInfoStateType;
    43    licenseInfo: null | SubnetInfo;
    44    overrideStyles: null | IEmbeddedCustomStyles;
    45    anonymousMode: boolean;
    46    helpName: string;
    47    helpTabName: string;
    48    locationPath: string;
    49    darkMode: boolean;
    50  }
    51  
    52  const initialState: SystemState = {
    53    value: 0,
    54    loggedIn: false,
    55    showMarketplace: false,
    56    userName: "",
    57    sidebarOpen: initSideBarOpen,
    58    siteReplicationInfo: { siteName: "", curSite: false, enabled: false },
    59    serverNeedsRestart: false,
    60    serverIsLoading: false,
    61    loadingConfigurations: true,
    62    loadingProgress: 100,
    63    snackBar: {
    64      message: "",
    65      detailedErrorMsg: "",
    66      type: "message",
    67    },
    68    modalSnackBar: {
    69      message: "",
    70      detailedErrorMsg: "",
    71      type: "message",
    72    },
    73    serverDiagnosticStatus: "",
    74    distributedSetup: false,
    75    licenseInfo: null,
    76    overrideStyles: null,
    77    anonymousMode: false,
    78    helpName: "help",
    79    helpTabName: "docs",
    80    locationPath: "",
    81    darkMode: isDarkModeOn(),
    82  };
    83  
    84  export const systemSlice = createSlice({
    85    name: "system",
    86    initialState,
    87    reducers: {
    88      userLogged: (state, action: PayloadAction<boolean>) => {
    89        state.loggedIn = action.payload;
    90      },
    91      showMarketplace: (state, action: PayloadAction<boolean>) => {
    92        state.showMarketplace = action.payload;
    93      },
    94      menuOpen: (state, action: PayloadAction<boolean>) => {
    95        // persist preference to local storage
    96        localStorage.setItem(
    97          "sidebarOpen",
    98          JSON.stringify({ open: action.payload }),
    99        );
   100        state.sidebarOpen = action.payload;
   101      },
   102      setServerNeedsRestart: (state, action: PayloadAction<boolean>) => {
   103        state.serverNeedsRestart = action.payload;
   104      },
   105      serverIsLoading: (state, action: PayloadAction<boolean>) => {
   106        state.serverIsLoading = action.payload;
   107      },
   108      configurationIsLoading: (state, action: PayloadAction<boolean>) => {
   109        state.loadingConfigurations = action.payload;
   110      },
   111      setLoadingProgress: (state, action: PayloadAction<number>) => {
   112        state.loadingProgress = action.payload;
   113      },
   114      setSnackBarMessage: (state, action: PayloadAction<string>) => {
   115        state.snackBar = {
   116          message: action.payload,
   117          detailedErrorMsg: "",
   118          type: "message",
   119        };
   120      },
   121      setErrorSnackMessage: (
   122        state,
   123        action: PayloadAction<ErrorResponseHandler>,
   124      ) => {
   125        state.snackBar = {
   126          message: action.payload.errorMessage,
   127          detailedErrorMsg: action.payload.detailedError,
   128          type: "error",
   129        };
   130      },
   131      setModalSnackMessage: (state, action: PayloadAction<string>) => {
   132        state.modalSnackBar = {
   133          message: action.payload,
   134          detailedErrorMsg: "",
   135          type: "message",
   136        };
   137      },
   138      setModalErrorSnackMessage: (
   139        state,
   140        action: PayloadAction<{ errorMessage: string; detailedError: string }>,
   141      ) => {
   142        state.modalSnackBar = {
   143          message: action.payload.errorMessage,
   144          detailedErrorMsg: action.payload.detailedError,
   145          type: "error",
   146        };
   147      },
   148      setServerDiagStat: (state, action: PayloadAction<string>) => {
   149        state.serverDiagnosticStatus = action.payload;
   150      },
   151      globalSetDistributedSetup: (state, action: PayloadAction<boolean>) => {
   152        state.distributedSetup = action.payload;
   153      },
   154      setSiteReplicationInfo: (state, action: PayloadAction<SRInfoStateType>) => {
   155        state.siteReplicationInfo = action.payload;
   156      },
   157      setSystemLicenseInfo: (state, action: PayloadAction<SubnetInfo | null>) => {
   158        state.licenseInfo = action.payload;
   159      },
   160      setHelpName: (state, action: PayloadAction<string>) => {
   161        state.helpName = action.payload;
   162      },
   163      setHelpTabName: (state, action: PayloadAction<string>) => {
   164        state.helpTabName = action.payload;
   165      },
   166  
   167      setOverrideStyles: (
   168        state,
   169        action: PayloadAction<IEmbeddedCustomStyles>,
   170      ) => {
   171        state.overrideStyles = action.payload;
   172      },
   173      setAnonymousMode: (state) => {
   174        state.anonymousMode = true;
   175        state.loggedIn = true;
   176      },
   177      setLocationPath: (state, action: PayloadAction<string>) => {
   178        state.locationPath = action.payload;
   179      },
   180      setDarkMode: (state, action: PayloadAction<boolean>) => {
   181        state.darkMode = action.payload;
   182      },
   183      resetSystem: () => {
   184        return initialState;
   185      },
   186    },
   187  });
   188  
   189  // Action creators are generated for each case reducer function
   190  export const {
   191    userLogged,
   192    showMarketplace,
   193    menuOpen,
   194    setServerNeedsRestart,
   195    serverIsLoading,
   196    setLoadingProgress,
   197    setSnackBarMessage,
   198    setErrorSnackMessage,
   199    setModalErrorSnackMessage,
   200    setModalSnackMessage,
   201    setServerDiagStat,
   202    globalSetDistributedSetup,
   203    setSiteReplicationInfo,
   204    setSystemLicenseInfo,
   205    setOverrideStyles,
   206    setAnonymousMode,
   207    resetSystem,
   208    configurationIsLoading,
   209    setHelpName,
   210    setHelpTabName,
   211    setLocationPath,
   212    setDarkMode,
   213  } = systemSlice.actions;
   214  
   215  export const selDistSet = (state: AppState) => state.system.distributedSetup;
   216  export const selSiteRep = (state: AppState) => state.system.siteReplicationInfo;
   217  
   218  export default systemSlice.reducer;