MCPcopy Create free account
hub / github.com/HelloCSV/HelloCSV / usePersistedReducer

Function usePersistedReducer

src/importer/reducer.tsx:192–221  ·  view source on GitHub ↗
(
  sheets: SheetDefinition[],
  persistenceConfig: PersistenceConfig,
  initialState?: ImporterState
)

Source from the content-addressed store, hash-verified

190};
191
192const 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
223const ImporterStateContext = createContext<ImporterState>({} as ImporterState);
224

Callers 1

ReducerProviderFunction · 0.85

Calls 3

buildInitialStateFunction · 0.90
setIndexedDBStateFunction · 0.90
fetchStateFunction · 0.85

Tested by

no test coverage detected