github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/ui/src/redux/state.ts (about)

     1  // Copyright 2018 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  import _ from "lodash";
    12  import { createStore, combineReducers, applyMiddleware, compose, Store } from "redux";
    13  import createSagaMiddleware from "redux-saga";
    14  import thunk from "redux-thunk";
    15  import {connectRouter, routerMiddleware, RouterState} from "connected-react-router";
    16  import { createHashHistory, History } from "history";
    17  
    18  import { apiReducersReducer, APIReducersState } from "./apiReducers";
    19  import { hoverReducer, HoverState } from "./hover";
    20  import { localSettingsReducer, LocalSettingsState } from "./localsettings";
    21  import { metricsReducer, MetricsState } from "./metrics";
    22  import { queryManagerReducer, QueryManagerState } from "./queryManager/reducer";
    23  import { timeWindowReducer, TimeWindowState } from "./timewindow";
    24  import { uiDataReducer, UIDataState } from "./uiData";
    25  import { loginReducer, LoginAPIState } from "./login";
    26  import rootSaga from "./sagas";
    27  
    28  export interface AdminUIState {
    29    cachedData: APIReducersState;
    30    hover: HoverState;
    31    localSettings: LocalSettingsState;
    32    metrics: MetricsState;
    33    queryManager: QueryManagerState;
    34    router: RouterState;
    35    timewindow: TimeWindowState;
    36    uiData: UIDataState;
    37    login: LoginAPIState;
    38  }
    39  
    40  const history = createHashHistory();
    41  
    42  const routerReducer = connectRouter(history);
    43  
    44  // createAdminUIStore is a function that returns a new store for the admin UI.
    45  // It's in a function so it can be recreated as necessary for testing.
    46  export function createAdminUIStore(historyInst: History<any>) {
    47    const sagaMiddleware = createSagaMiddleware();
    48  
    49    const s: Store<AdminUIState> = createStore(
    50      combineReducers<AdminUIState>({
    51        cachedData: apiReducersReducer,
    52        hover: hoverReducer,
    53        localSettings: localSettingsReducer,
    54        metrics: metricsReducer,
    55        queryManager: queryManagerReducer,
    56        router: routerReducer,
    57        timewindow: timeWindowReducer,
    58        uiData: uiDataReducer,
    59        login: loginReducer,
    60      }),
    61      compose(
    62        applyMiddleware(thunk, sagaMiddleware, routerMiddleware(historyInst)),
    63        // Support for redux dev tools
    64        // https://chrome.google.com/webstore/detail/redux-devtools/lmhkpmbekcpmknklioeibfkpmmfibljd
    65        (window as any).__REDUX_DEVTOOLS_EXTENSION__ ? (window as any).__REDUX_DEVTOOLS_EXTENSION__({
    66          serialize: {
    67            options: {
    68              function: (_key: string, value: any): Object => {
    69                if (value && value.toRaw) {
    70                  return value.toRaw();
    71                }
    72                return value;
    73              },
    74            },
    75          },
    76        }) : _.identity,
    77      ),
    78    );
    79  
    80    sagaMiddleware.run(rootSaga);
    81    return s;
    82  }
    83  
    84  const store = createAdminUIStore(history);
    85  
    86  export { history, store };