( sheets: SheetDefinition[], persistenceConfig: PersistenceConfig, initialState?: ImporterState )
| 190 | }; |
| 191 | |
| 192 | const usePersistedReducer = ( |
| 193 | sheets: SheetDefinition[], |
| 194 | persistenceConfig: PersistenceConfig, |
| 195 | initialState?: ImporterState |
| 196 | ): [ImporterState, (action: ImporterAction) => void] => { |
| 197 | const [state, dispatch] = useReducer( |
| 198 | reducer, |
| 199 | initialState ?? buildInitialState(sheets) |
| 200 | ); |
| 201 | |
| 202 | useEffect(() => { |
| 203 | const fetchState = async () => { |
| 204 | const newState = await buildState(sheets, persistenceConfig); |
| 205 | dispatch({ type: 'SET_STATE', payload: { state: newState } }); |
| 206 | }; |
| 207 | if (initialState == null) { |
| 208 | fetchState(); |
| 209 | } |
| 210 | // We only want to fetch the state once |
| 211 | // eslint-disable-next-line react-hooks/exhaustive-deps |
| 212 | }, []); |
| 213 | |
| 214 | useEffect(() => { |
| 215 | if (persistenceConfig.enabled) { |
| 216 | setIndexedDBState(state, persistenceConfig.customKey); |
| 217 | } |
| 218 | }, [state, persistenceConfig]); |
| 219 | |
| 220 | return [state, dispatch]; |
| 221 | }; |
| 222 | |
| 223 | const ImporterStateContext = createContext<ImporterState>({} as ImporterState); |
| 224 |
no test coverage detected