({children}: {children: ReactNode})
| 33 | * Make Store and dispatch function available to all sub-components in children. |
| 34 | */ |
| 35 | export function StoreProvider({children}: {children: ReactNode}): JSX.Element { |
| 36 | const [store, dispatch] = useReducer(storeReducer, emptyStore); |
| 37 | const [isPageReady, setIsPageReady] = useState<boolean>(false); |
| 38 | |
| 39 | useEffect(() => { |
| 40 | let mountStore: Store; |
| 41 | try { |
| 42 | mountStore = initStoreFromUrlOrLocalStorage(); |
| 43 | } catch (e) { |
| 44 | console.error('Failed to initialize store from URL or local storage', e); |
| 45 | mountStore = defaultStore; |
| 46 | } |
| 47 | dispatch({type: 'setStore', payload: {store: mountStore}}); |
| 48 | setIsPageReady(true); |
| 49 | }, []); |
| 50 | |
| 51 | useEffect(() => { |
| 52 | if (store !== emptyStore) { |
| 53 | saveStore(store); |
| 54 | } |
| 55 | }, [store]); |
| 56 | |
| 57 | return ( |
| 58 | <StoreContext.Provider value={store}> |
| 59 | <StoreDispatchContext.Provider value={dispatch}> |
| 60 | {isPageReady ? children : null} |
| 61 | </StoreDispatchContext.Provider> |
| 62 | </StoreContext.Provider> |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | type ReducerAction = |
| 67 | | { |
nothing calls this directly
no test coverage detected