Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

refactor(web): extract app-shell storage helpers and fix /book prerender

Bingxi Zhao (Frank)9035c476e511
diff --git a/web/app/(utility)/settings/page.tsx b/web/app/(utility)/settings/page.tsx
index 9c0de9c7b..a6e2d7b7c 100644
--- a/web/app/(utility)/settings/page.tsx
+++ b/web/app/(utility)/settings/page.tsx
@@ -23,7 +23,7 @@ import {
import { useTranslation } from "react-i18next";
-import { writeStoredLanguage } from "@/context/AppShellContext";
+import { writeStoredLanguage } from "@/context/app-shell-storage";
import { apiUrl } from "@/lib/api";
import { setTheme as applyThemePreference } from "@/lib/theme";
diff --git a/web/app/(workspace)/book/page.tsx b/web/app/(workspace)/book/page.tsx
index fe82be296..8bc645553 100644
--- a/web/app/(workspace)/book/page.tsx
+++ b/web/app/(workspace)/book/page.tsx
@@ -1,6 +1,6 @@
"use client";
-import { useCallback, useEffect, useMemo, useReducer, useRef, useState } from "react";
+import { Suspense, useCallback, useEffect, useMemo, useReducer, useRef, useState } from "react";
import { useSearchParams } from "next/navigation";
import { Loader2, MessageSquare } from "lucide-react";
@@ -32,6 +32,23 @@ import SpineEditor from "./components/SpineEditor";
type View = "list" | "creator" | "spine" | "reader";
export default function BookPage() {
+ // `useSearchParams()` requires a Suspense boundary during static prerender
+ // (Next.js CSR bailout). Wrap the actual page implementation here so the
+ // production build (`next build`) doesn't fail prerendering `/book`.
+ return (
+ <Suspense
+ fallback={
+ <div className="flex h-screen w-full items-center justify-center text-[var(--muted-foreground)]">
+ <Loader2 className="mr-2 h-4 w-4 animate-spin" /> Loading…
+ </div>
+ }
+ >
+ <BookPageInner />
+ </Suspense>
+ );
+}
+
+function BookPageInner() {
const [books, setBooks] = useState<Book[]>([]);
const [loadingBooks, setLoadingBooks] = useState(false);
const [view, setView] = useState<View>("list");
diff --git a/web/context/AppShellContext.tsx b/web/context/AppShellContext.tsx
index c72a2bf32..fdeaadec8 100644
--- a/web/context/AppShellContext.tsx
+++ b/web/context/AppShellContext.tsx
@@ -15,93 +15,22 @@ import {
subscribeToThemeChanges,
type Theme,
} from "@/lib/theme";
-
-export type AppLanguage = "en" | "zh";
-
-export const ACTIVE_SESSION_STORAGE_KEY = "deeptutor.activeSessionId.tab";
-export const LANGUAGE_STORAGE_KEY = "deeptutor-language";
-export const SIDEBAR_COLLAPSED_STORAGE_KEY = "deeptutor.sidebarCollapsed";
-
-const ACTIVE_SESSION_EVENT = "deeptutor:active-session";
-const LANGUAGE_EVENT = "deeptutor:language";
-const SIDEBAR_COLLAPSED_EVENT = "deeptutor:sidebar-collapsed";
-
-function normalizeLanguage(value: string | null | undefined): AppLanguage {
- return value === "zh" ? "zh" : "en";
-}
-
-export function readStoredLanguage(): AppLanguage {
- if (typeof window === "undefined") return "en";
- try {
- return normalizeLanguage(window.localStorage.getItem(LANGUAGE_STORAGE_KEY));
- } catch {
- return "en";
- }
-}
-
-export function writeStoredLanguage(language: AppLanguage): void {
- if (typeof window === "undefined") return;
- try {
- window.localStorage.setItem(LANGUAGE_STORAGE_KEY, language);
- window.dispatchEvent(
- new CustomEvent(LANGUAGE_EVENT, {
- detail: { language },
- }),
- );
- } catch {
- // localStorage may be unavailable
- }
-}
-
-export function readStoredActiveSessionId(): string | null {
- if (typeof window === "undefined") return null;
- try {
- return window.sessionStorage.getItem(ACTIVE_SESSION_STORAGE_KEY);
- } catch {
- return null;
- }
-}
-
-export function readStoredSidebarCollapsed(): boolean {
- if (typeof window === "undefined") return false;
- try {
- return window.localStorage.getItem(SIDEBAR_COLLAPSED_STORAGE_KEY) === "1";
- } catch {
- return false;
- }
-}
-
-export function writeStoredSidebarCollapsed(collapsed: boolean): void {
- if (typeof window === "undefined") return;
- try {
- window.localStorage.setItem(SIDEBAR_COLLAPSED_STORAGE_KEY, collapsed ? "1" : "0");
- window.dispatchEvent(
- new CustomEvent(SIDEBAR_COLLAPSED_EVENT, {
- detail: { collapsed },
- }),
- );
- } catch {
- // localStorage may be unavailable
- }
-}
-
-export function writeStoredActiveSessionId(sessionId: string | null): void {
- if (typeof window === "undefined") return;
- try {
- if (sessionId) {
- window.sessionStorage.setItem(ACTIVE_SESSION_STORAGE_KEY, sessionId);
- } else {
- window.sessionStorage.removeItem(ACTIVE_SESSION_STORAGE_KEY);
- }
- window.dispatchEvent(
- new CustomEvent(ACTIVE_SESSION_EVENT, {
- detail: { sessionId },
- }),
- );
- } catch {
- // sessionStorage may be unavailable
- }
-}
+import {
+ ACTIVE_SESSION_EVENT,
+ ACTIVE_SESSION_STORAGE_KEY,
+ LANGUAGE_EVENT,
+ LANGUAGE_STORAGE_KEY,
+ SIDEBAR_COLLAPSED_EVENT,
+ SIDEBAR_COLLAPSED_STORAGE_KEY,
+ normalizeLanguage,
+ readStoredActiveSessionId,
+ readStoredLanguage,
+ readStoredSidebarCollapsed,
+ writeStoredActiveSessionId,
+ writeStoredLanguage,
+ writeStoredSidebarCollapsed,
+ type AppLanguage,
+} from "@/context/app-shell-storage";
interface AppShellContextValue {
theme: Theme;
diff --git a/web/context/UnifiedChatContext.tsx b/web/context/UnifiedChatContext.tsx
index b8826a0e8..dccc4dcd5 100644
--- a/web/context/UnifiedChatContext.tsx
+++ b/web/context/UnifiedChatContext.tsx
@@ -12,7 +12,7 @@ import React, {
import {
readStoredLanguage,
writeStoredActiveSessionId,
-} from "@/context/AppShellContext";
+} from "@/context/app-shell-storage";
import type { StreamEvent, ChatMessage } from "@/lib/unified-ws";
import { UnifiedWSClient } from "@/lib/unified-ws";
import { getSession, type SessionMessage } from "@/lib/session-api";
diff --git a/web/context/app-shell-storage.ts b/web/context/app-shell-storage.ts
new file mode 100644
index 000000000..35a167868
--- /dev/null
+++ b/web/context/app-shell-storage.ts
@@ -0,0 +1,88 @@
+"use client";
+
+export type AppLanguage = "en" | "zh";
+
+export const ACTIVE_SESSION_STORAGE_KEY = "deeptutor.activeSessionId.tab";
+export const LANGUAGE_STORAGE_KEY = "deeptutor-language";
+export const SIDEBAR_COLLAPSED_STORAGE_KEY = "deeptutor.sidebarCollapsed";
+
+export const ACTIVE_SESSION_EVENT = "deeptutor:active-session";
+export const LANGUAGE_EVENT = "deeptutor:language";
+export const SIDEBAR_COLLAPSED_EVENT = "deeptutor:sidebar-collapsed";
+
+export function normalizeLanguage(value: string | null | undefined): AppLanguage {
+ return value === "zh" ? "zh" : "en";
+}
+
+export function readStoredLanguage(): AppLanguage {
+ if (typeof window === "undefined") return "en";
+ try {
+ return normalizeLanguage(window.localStorage.getItem(LANGUAGE_STORAGE_KEY));
+ } catch {
+ return "en";
+ }
+}
+
+export function writeStoredLanguage(language: AppLanguage): void {
+ if (typeof window === "undefined") return;
+ try {
+ window.localStorage.setItem(LANGUAGE_STORAGE_KEY, language);
+ window.dispatchEvent(
+ new CustomEvent(LANGUAGE_EVENT, {
+ detail: { language },
+ }),
+ );
+ } catch {
+ // localStorage may be unavailable
+ }
+}
+
+export function readStoredActiveSessionId(): string | null {
+ if (typeof window === "undefined") return null;
+ try {
+ return window.sessionStorage.getItem(ACTIVE_SESSION_STORAGE_KEY);
+ } catch {
+ return null;
+ }
+}
+
+export function writeStoredActiveSessionId(sessionId: string | null): void {
+ if (typeof window === "undefined") return;
+ try {
+ if (sessionId) {
+ window.sessionStorage.setItem(ACTIVE_SESSION_STORAGE_KEY, sessionId);
+ } else {
+ window.sessionStorage.removeItem(ACTIVE_SESSION_STORAGE_KEY);
+ }
+ window.dispatchEvent(
+ new CustomEvent(ACTIVE_SESSION_EVENT, {
+ detail: { sessionId },
+ }),
+ );
+ } catch {
+ // sessionStorage may be unavailable
+ }
+}
+
+export function readStoredSidebarCollapsed(): boolean {
+ if (typeof window === "undefined") return false;
+ try {
+ return window.localStorage.getItem(SIDEBAR_COLLAPSED_STORAGE_KEY) === "1";
+ } catch {
+ return false;
+ }
+}
+
+export function writeStoredSidebarCollapsed(collapsed: boolean): void {
+ if (typeof window === "undefined") return;
+ try {
+ window.localStorage.setItem(SIDEBAR_COLLAPSED_STORAGE_KEY, collapsed ? "1" : "0");
+ window.dispatchEvent(
+ new CustomEvent(SIDEBAR_COLLAPSED_EVENT, {
+ detail: { collapsed },
+ }),
+ );
+ } catch {
+ // localStorage may be unavailable
+ }
+}