Merge branch 'dev' into main

This commit is contained in:
Bingxi Zhao (Frank)
2026-04-20 20:34:26 +08:00
5 changed files with 124 additions and 90 deletions

View File

@@ -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";

View File

@@ -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");

View File

@@ -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;

View File

@@ -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";

View File

@@ -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
}
}