import { combineReducers } from 'redux'; import { StoreState } from '../../types'; import { ActionOf } from '../redux'; import { CleanUp, cleanUpAction } from '../actions/cleanUp'; import sharedReducers from 'app/core/reducers'; import alertingReducers from 'app/features/alerting/state/reducers'; import teamsReducers from 'app/features/teams/state/reducers'; import apiKeysReducers from 'app/features/api-keys/state/reducers'; import foldersReducers from 'app/features/folders/state/reducers'; import dashboardReducers from 'app/features/dashboard/state/reducers'; import exploreReducers from 'app/features/explore/state/reducers'; import pluginReducers from 'app/features/plugins/state/reducers'; import dataSourcesReducers from 'app/features/datasources/state/reducers'; import usersReducers from 'app/features/users/state/reducers'; import userReducers from 'app/features/profile/state/reducers'; import organizationReducers from 'app/features/org/state/reducers'; import ldapReducers from 'app/features/admin/state/reducers'; const rootReducers = { ...sharedReducers, ...alertingReducers, ...teamsReducers, ...apiKeysReducers, ...foldersReducers, ...dashboardReducers, ...exploreReducers, ...pluginReducers, ...dataSourcesReducers, ...usersReducers, ...userReducers, ...organizationReducers, ...ldapReducers, }; export function addRootReducer(reducers: any) { Object.assign(rootReducers, ...reducers); } export const recursiveCleanState = (state: any, stateSlice: any): boolean => { for (const stateKey in state) { if (state[stateKey] === stateSlice) { state[stateKey] = undefined; return true; } if (typeof state[stateKey] === 'object') { const cleaned = recursiveCleanState(state[stateKey], stateSlice); if (cleaned) { return true; } } } return false; }; const appReducer = combineReducers(rootReducers); export const rootReducer = (state: StoreState, action: ActionOf): StoreState => { if (action.type !== cleanUpAction.type) { return appReducer(state, action); } const { stateSelector } = action.payload as CleanUp; const stateSlice = stateSelector(state); recursiveCleanState(state, stateSlice); return appReducer(state, action); };