Skip to content

Commit 5cd4d82

Browse files
committed
Fix nextjs error
1 parent cda6037 commit 5cd4d82

3 files changed

Lines changed: 10 additions & 3 deletions

File tree

src/state/ExerciseContext.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import React, { createContext, useContext, useReducer, useRef, useEffect } from 'react';
44
import { AppState, Action } from './types';
55
import { reducer, createInitialState } from './reducer';
6-
import { saveProgress } from './persistence';
6+
import { loadProgress, saveProgress } from './persistence';
77
import { StackSim } from '@/engine/simulators/StackSim';
88
import { HeapSim } from '@/engine/simulators/HeapSim';
99
import { WinHeapSim } from '@/engine/simulators/WinHeapSim';
@@ -35,6 +35,10 @@ export function ExerciseContextProvider({ children }: { children: React.ReactNod
3535
? getExercise(state.currentExerciseId) ?? null
3636
: null;
3737

38+
useEffect(() => {
39+
dispatch({ type: 'HYDRATE_COMPLETED', completed: loadProgress() });
40+
}, []);
41+
3842
// Persist completed exercises whenever they change
3943
const completedRef = useRef(state.completed);
4044
useEffect(() => {

src/state/reducer.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { AppState, Action } from './types';
22
import { BASE_SYMBOLS } from '@/exercises/shared/symbols';
3-
import { loadProgress } from './persistence';
43

54
export function createInitialState(): AppState {
65
return {
76
currentExerciseId: null,
8-
completed: loadProgress(),
7+
completed: new Set(),
98
logMessages: [],
109
inputMode: 'text',
1110
inputProgress: null,
@@ -34,6 +33,9 @@ export function createInitialState(): AppState {
3433

3534
export function reducer(state: AppState, action: Action): AppState {
3635
switch (action.type) {
36+
case 'HYDRATE_COMPLETED':
37+
return { ...state, completed: new Set(action.completed) };
38+
3739
case 'LOAD_EXERCISE':
3840
return {
3941
...state,

src/state/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export interface AppState {
2727
}
2828

2929
export type Action =
30+
| { type: 'HYDRATE_COMPLETED'; completed: Set<string> }
3031
| { type: 'LOAD_EXERCISE'; exerciseId: string }
3132
| { type: 'LOG'; cls: string; msg: string }
3233
| { type: 'LOG_BATCH'; messages: Array<{ cls: string; msg: string }> }

0 commit comments

Comments
 (0)