Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don’t store the demo state when DEMO_MODE is off #627

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 30 additions & 20 deletions frontend/app/src/demo-mode/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,31 +102,43 @@ const DemoContext = createContext<DemoModeContext>({
updateAccountConnected: noop,
});

export function DemoMode({
children,
}: {
children: ReactNode;
}) {
const [state, setState] = useState<DemoModeState>(() => {
// attempt restoring state from local storage
const storedState = typeof localStorage !== "undefined"
? localStorage.getItem(DEMO_STATE_KEY)
: null;
const DemoStorage = {
get: (): DemoModeState | null => {
if (!DEMO_MODE || typeof localStorage === "undefined") {
return null;
}
const storedState = localStorage.getItem(DEMO_STATE_KEY);
if (storedState) {
try {
return v.parse(DemoModeStateSchema, JSON.parse(storedState));
} catch {
return demoModeStateDefault;
return null;
}
}
return demoModeStateDefault;
});

// save state to local storage
useEffect(() => {
if (typeof localStorage !== "undefined") {
return null;
},
set: (state: DemoModeState) => {
if (DEMO_MODE && typeof localStorage !== "undefined") {
localStorage.setItem(DEMO_STATE_KEY, JSON.stringify(state));
}
},
clear: () => {
if (DEMO_MODE && typeof localStorage !== "undefined") {
localStorage.removeItem(DEMO_STATE_KEY);
}
},
};

export function DemoMode({
children,
}: {
children: ReactNode;
}) {
const [state, setState] = useState<DemoModeState>(() => DemoStorage.get() ?? demoModeStateDefault);

// save state to storage
useEffect(() => {
DemoStorage.set(state);
}, [state]);

const setDemoModeState = useCallback((
Expand All @@ -144,9 +156,7 @@ export function DemoMode({
}, []);

const clearDemoMode = useCallback(() => {
if (typeof localStorage !== "undefined") {
localStorage.removeItem(DEMO_STATE_KEY);
}
DemoStorage.clear();
setState(demoModeStateDefault);
}, []);

Expand Down