root.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { AnyAction, combineReducers } from 'redux';
  2. import sharedReducers from 'app/core/reducers';
  3. import ldapReducers from 'app/features/admin/state/reducers';
  4. import alertingReducers from 'app/features/alerting/state/reducers';
  5. import apiKeysReducers from 'app/features/api-keys/state/reducers';
  6. import panelEditorReducers from 'app/features/dashboard/components/PanelEditor/state/reducers';
  7. import dashboardReducers from 'app/features/dashboard/state/reducers';
  8. import dataSourcesReducers from 'app/features/datasources/state/reducers';
  9. import exploreReducers from 'app/features/explore/state/main';
  10. import foldersReducers from 'app/features/folders/state/reducers';
  11. import invitesReducers from 'app/features/invites/state/reducers';
  12. import importDashboardReducers from 'app/features/manage-dashboards/state/reducers';
  13. import organizationReducers from 'app/features/org/state/reducers';
  14. import panelsReducers from 'app/features/panel/state/reducers';
  15. import { reducer as pluginsReducer } from 'app/features/plugins/admin/state/reducer';
  16. import userReducers from 'app/features/profile/state/reducers';
  17. import serviceAccountsReducer from 'app/features/serviceaccounts/state/reducers';
  18. import teamsReducers from 'app/features/teams/state/reducers';
  19. import usersReducers from 'app/features/users/state/reducers';
  20. import templatingReducers from 'app/features/variables/state/keyedVariablesReducer';
  21. import { CleanUp, cleanUpAction } from '../actions/cleanUp';
  22. const rootReducers = {
  23. ...sharedReducers,
  24. ...alertingReducers,
  25. ...teamsReducers,
  26. ...apiKeysReducers,
  27. ...foldersReducers,
  28. ...dashboardReducers,
  29. ...exploreReducers,
  30. ...dataSourcesReducers,
  31. ...usersReducers,
  32. ...serviceAccountsReducer,
  33. ...userReducers,
  34. ...invitesReducers,
  35. ...organizationReducers,
  36. ...ldapReducers,
  37. ...importDashboardReducers,
  38. ...panelEditorReducers,
  39. ...panelsReducers,
  40. ...templatingReducers,
  41. plugins: pluginsReducer,
  42. };
  43. const addedReducers = {};
  44. export const addReducer = (newReducers: any) => {
  45. Object.assign(addedReducers, newReducers);
  46. };
  47. export const createRootReducer = () => {
  48. const appReducer = combineReducers({
  49. ...rootReducers,
  50. ...addedReducers,
  51. });
  52. return (state: any, action: AnyAction) => {
  53. if (action.type !== cleanUpAction.type) {
  54. return appReducer(state, action);
  55. }
  56. const { stateSelector } = action.payload as CleanUp<any>;
  57. const stateSlice = stateSelector(state);
  58. recursiveCleanState(state, stateSlice);
  59. return appReducer(state, action);
  60. };
  61. };
  62. export const recursiveCleanState = (state: any, stateSlice: any): boolean => {
  63. for (const stateKey in state) {
  64. if (!state.hasOwnProperty(stateKey)) {
  65. continue;
  66. }
  67. const slice = state[stateKey];
  68. if (slice === stateSlice) {
  69. state[stateKey] = undefined;
  70. return true;
  71. }
  72. if (typeof slice === 'object') {
  73. const cleaned = recursiveCleanState(slice, stateSlice);
  74. if (cleaned) {
  75. return true;
  76. }
  77. }
  78. }
  79. return false;
  80. };