1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const SubApp: React.FC<{ app: AllAppOptions; menu: SubAppMenu; translation: Translation; AppIcon: React.FC; reducers?: ReducersMapObject; preloadedState?: any; store?: Store<{}, Action>; }> = ({ app, menu, children, reducers, preloadedState, store, translation, AppIcon }) => { const globalStore = useStore<AppState, Action>(); const appStore = useMemo( () => store || createStore( createReducer( combineReducers({ ...reducers, ..._.mapValues( globalStore.getState(), (v) => (state: typeof v = v) => state, ), }), ), preloadedState, composeEnhancers( applyMiddleware(routerMiddleware(history), thunk.withExtraArgument(globalStore.dispatch)), (createStore: any) => (...args: any[]) => { const store = createStore(...args); return { ...store, globalDispatch: globalStore.dispatch, }; }, ), ), [], ); const unsubscribe = useMemo( () => globalStore.subscribe(() => { const globalState = globalStore.getState(); const state = appStore.getState(); _.find(globalState, (value, key) => state[key] !== value) && appStore.dispatch(changeGlobalState(globalState)); }), [globalStore, appStore], ); useEffect(() => unsubscribe, [unsubscribe]); const [menuFoldState, setMenuFoldState] = useState(false); const match = useMatch(); return ( <Provider store={appStore}> <div className={classNames('sub-app flex-auto overflow-hidden flex-row position-relative', app)}> <AppLeftMenu fold={menuFoldState} setFold={setMenuFoldState} menu={menu} app={app} match={match} AppIcon={AppIcon} /> <AppMain fold={menuFoldState} menu={menu} app={app} match={match}> {children} </AppMain> </div> </Provider> ); };
|