Skip to content

Commit

Permalink
feat: save and load app state from localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
Kodylow committed Sep 16, 2024
1 parent a99caf9 commit e3bfadd
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion apps/router/src/context/AppContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ export type AppAction =
payload: string;
};

const saveToLocalStorage = (state: AppContextValue) => {
localStorage.setItem('fedimint_ui_state', JSON.stringify(state));
};

const reducer = (
state: AppContextValue,
action: AppAction
Expand Down Expand Up @@ -120,6 +124,15 @@ const reducer = (
}
};

const reducerWithMiddleware = (
state: AppContextValue,
action: AppAction
): AppContextValue => {
const newState = reducer(state, action);
saveToLocalStorage(newState);
return newState;
};

export const AppContext = createContext<AppContextValue>(makeInitialState());

export interface AppContextProviderProps {
Expand All @@ -129,7 +142,10 @@ export interface AppContextProviderProps {
export const AppContextProvider: React.FC<AppContextProviderProps> = ({
children,
}: AppContextProviderProps) => {
const [state, dispatch] = useReducer(reducer, makeInitialState());
const [state, dispatch] = useReducer(
reducerWithMiddleware,
makeInitialState()
);

useEffect(() => {
const isGuardian = (service: Service): service is GuardianConfig =>
Expand Down

0 comments on commit e3bfadd

Please sign in to comment.