import { configureStore } from '@reduxjs/toolkit'; import _ from 'lodash'; export interface RouteState { path: string, history: string[] } const store = configureStore({ reducer: { path: (state = '/load', action) => { if (action.type != 'setPath') return state; return action.path ?? state; }, history: (state = ['/load'], action) => { let new_state: string[] = _.cloneDeep(state); if (action.type == 'addHistory') { // detect attempts to add same as last url if (new_state[new_state.length - 1] == action.path) return new_state; new_state.push(action.path); } if (action.type == 'popHistory') { new_state.pop(); } return new_state; } }, }); export default store; export function route(path: string) { store.dispatch({ type: 'setPath', path }); store.dispatch({ type: 'addHistory', path }); } export function back() { const history: string[] = store.getState().history; if (history[history.length - 2] == '/load') return store.dispatch({ type: 'setPath', path: history[history.length - 2] }); store.dispatch({ type: 'popHistory' }); }