aboutsummaryrefslogtreecommitdiffstats
path: root/packages/website/ts/redux/store.ts
blob: 0ef345c80f9b2fdcf0c0075afa5e106022c3e476 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import * as _ from 'lodash';
import { applyMiddleware, createStore, Store as ReduxStore } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension/developmentOnly';
import { stateStorage } from 'ts/local_storage/state_storage';
import { analyticsMiddleware } from 'ts/redux/analyticsMiddleware';
import { reducer, State } from 'ts/redux/reducer';

const ONE_SECOND = 1000;

export const store: ReduxStore<State> = createStore(
    reducer as any,
    stateStorage.getPersistedDefaultState(),
    composeWithDevTools(applyMiddleware(analyticsMiddleware) as any) as any,
);
store.subscribe(
    _.throttle(() => {
        const state = store.getState();
        // Persisted state
        stateStorage.saveState({
            hasPortalOnboardingBeenClosed: state.hasPortalOnboardingBeenClosed,
            isPortalOnboardingShowing: state.isPortalOnboardingShowing,
        });
    }, ONE_SECOND),
);