Files
DeepTutor/web/context/AppShellContext.tsx
wayblink be14c3ef23 feat: separate UI and model response languages (#758)
* feat: separate UI and model response languages

* refactor(settings): keep one owner for the interface.json language rules

The legacy migration — a file written before the UI/response split
inherits its single language into both fields — had been duplicated into
both readers of interface.json, as had the theme/language defaults. Two
copies of a migration rule drift.

Move the rule into resolve_languages() in the module that owns the file,
have the router compose its superset of defaults on top of the service's,
and cover the agreement with a test that reads one legacy file through
both paths. The helper also drops the separate key-presence check:
_normalize_language already falls back to its default for missing, null
and unrecognized values alike.

* fix: keep the language bootstrap single and local-choice-first

Merging dev surfaced three collisions this branch had not seen:

- The new bootstrap fetched /settings/ui unconditionally and overwrote
  writeStoredLanguage on every mount, undoing #730's rule that a choice
  made in this browser is the more specific signal and must win. Folded it
  into the existing guarded effect so one fetch serves both language
  fields and neither can clobber a local selection.
- apiFetch/apiUrl ended up imported twice in AppShellContext, which git
  merged without a conflict and tsc rejects outright.
- Three tests still stubbed get_ui_language at call sites that now ask for
  get_response_language: the question router's stand-in module and two
  mastery_path patches (renamed the zh case to say response language).

* fix(api): include response_language in the pre-session UI read

The bootstrap adopts both languages from this one endpoint, so leaving
response_language out of the projection meant a fresh browser always
inherited the interface locale and never saw the saved output-language
preference. It belongs with the other two: all three are the visitor's
own presentation choices, none describe the deployment.

---------

Co-authored-by: Bingxi Zhao (Frank) <13051345080@163.com>
2026-08-07 03:13:04 +08:00

321 lines
10 KiB
TypeScript

"use client";
import {
createContext,
useCallback,
useContext,
useEffect,
useMemo,
useState,
} from "react";
import { apiFetch, apiUrl } from "@/lib/api";
import {
getStoredTheme,
getSystemTheme,
setTheme as applyThemePreference,
subscribeToThemeChanges,
type Theme,
} from "@/lib/theme";
import {
ACTIVE_SESSION_EVENT,
ACTIVE_SESSION_STORAGE_KEY,
CODE_BLOCK_SHOW_LINE_NUMBERS_STORAGE_KEY,
CODE_BLOCK_SETTINGS_EVENT,
CODE_BLOCK_THEME_STORAGE_KEY,
CODE_BLOCK_WRAP_LONG_LINES_STORAGE_KEY,
LANGUAGE_EVENT,
LANGUAGE_STORAGE_KEY,
hasStoredLanguage,
SIDEBAR_COLLAPSED_EVENT,
SIDEBAR_COLLAPSED_STORAGE_KEY,
normalizeCodeBlockShowLineNumbers,
normalizeCodeBlockTheme,
normalizeCodeBlockWrapLongLines,
normalizeLanguage,
resolveResponseLanguage,
readStoredActiveSessionId,
readStoredCodeBlockShowLineNumbers,
readStoredCodeBlockTheme,
readStoredCodeBlockWrapLongLines,
readStoredLanguage,
writeStoredResponseLanguage,
readStoredSidebarCollapsed,
writeStoredActiveSessionId,
writeStoredCodeBlockShowLineNumbers,
writeStoredCodeBlockTheme,
writeStoredCodeBlockWrapLongLines,
writeStoredLanguage,
writeStoredSidebarCollapsed,
type AppLanguage,
} from "@/context/app-shell-storage";
interface AppShellContextValue {
theme: Theme;
setTheme: (theme: Theme) => void;
language: AppLanguage;
setLanguage: (language: AppLanguage) => void;
activeSessionId: string | null;
setActiveSessionId: (sessionId: string | null) => void;
sidebarCollapsed: boolean;
setSidebarCollapsed: (collapsed: boolean) => void;
codeBlockTheme: string;
setCodeBlockTheme: (theme: string) => void;
codeBlockShowLineNumbers: boolean;
setCodeBlockShowLineNumbers: (show: boolean) => void;
codeBlockWrapLongLines: boolean;
setCodeBlockWrapLongLines: (wrap: boolean) => void;
}
const AppShellContext = createContext<AppShellContextValue | null>(null);
export function AppShellProvider({ children }: { children: React.ReactNode }) {
const [theme, setThemeState] = useState<Theme>(() => {
return getStoredTheme() ?? getSystemTheme();
});
// Always start with "en" to match SSR; hydrate from localStorage after mount
const [language, setLanguageState] = useState<AppLanguage>("en");
const [activeSessionId, setActiveSessionIdState] = useState<string | null>(
() => readStoredActiveSessionId(),
);
// Always start expanded to match SSR; hydrate from localStorage after mount
const [sidebarCollapsed, setSidebarCollapsedState] = useState<boolean>(false);
// Code block settings - start with defaults, hydrate from localStorage after mount
const [codeBlockTheme, setCodeBlockThemeState] = useState<string>(() =>
readStoredCodeBlockTheme(),
);
const [codeBlockShowLineNumbers, setCodeBlockShowLineNumbersState] =
useState<boolean>(() => readStoredCodeBlockShowLineNumbers());
const [codeBlockWrapLongLines, setCodeBlockWrapLongLinesState] =
useState<boolean>(() => readStoredCodeBlockWrapLongLines());
useEffect(() => {
// Hydrate client-only preferences after SSR-safe first render.
// eslint-disable-next-line react-hooks/set-state-in-effect
setLanguageState(readStoredLanguage());
setSidebarCollapsedState(readStoredSidebarCollapsed());
setCodeBlockThemeState(readStoredCodeBlockTheme());
setCodeBlockShowLineNumbersState(readStoredCodeBlockShowLineNumbers());
setCodeBlockWrapLongLinesState(readStoredCodeBlockWrapLongLines());
}, []);
useEffect(() => {
// The saved languages live in the backend's ui settings, but only the
// settings route ever read them, so every other page started in English
// until the user changed it again in this browser. Adopt them once, and
// only when this browser has made no choice of its own — a local selection
// is the more specific signal and must win.
//
// One fetch carries both fields: the interface locale and the
// reader-facing output language are stored together and are gated by the
// same "has this browser chosen yet?" question, so splitting them into two
// bootstraps would only give them a chance to disagree.
if (hasStoredLanguage()) return;
const controller = new AbortController();
void (async () => {
try {
const response = await apiFetch(apiUrl("/api/v1/settings/ui"), {
signal: controller.signal,
skipAuthRedirect: true,
});
if (!response.ok) return;
const payload = (await response.json()) as {
language?: unknown;
response_language?: unknown;
};
if (payload.language !== "zh" && payload.language !== "en") return;
writeStoredLanguage(payload.language);
// A backend that predates the split sends no response_language;
// resolveResponseLanguage inherits the interface locale, matching what
// the server does for a legacy interface.json.
writeStoredResponseLanguage(
resolveResponseLanguage(
typeof payload.response_language === "string"
? payload.response_language
: null,
payload.language,
),
);
setLanguageState(payload.language);
} catch {
// Offline or unauthenticated: keep the local default.
}
})();
return () => controller.abort();
}, []);
useEffect(() => {
return subscribeToThemeChanges((nextTheme) => {
setThemeState(nextTheme);
});
}, []);
useEffect(() => {
if (typeof window === "undefined") return;
const onStorage = (event: StorageEvent) => {
if (event.key === LANGUAGE_STORAGE_KEY) {
setLanguageState(normalizeLanguage(event.newValue));
}
if (event.key === ACTIVE_SESSION_STORAGE_KEY) {
setActiveSessionIdState(event.newValue);
}
if (event.key === SIDEBAR_COLLAPSED_STORAGE_KEY) {
setSidebarCollapsedState(event.newValue === "1");
}
if (event.key === CODE_BLOCK_THEME_STORAGE_KEY) {
setCodeBlockThemeState(normalizeCodeBlockTheme(event.newValue));
}
if (event.key === CODE_BLOCK_SHOW_LINE_NUMBERS_STORAGE_KEY) {
setCodeBlockShowLineNumbersState(
normalizeCodeBlockShowLineNumbers(event.newValue),
);
}
if (event.key === CODE_BLOCK_WRAP_LONG_LINES_STORAGE_KEY) {
setCodeBlockWrapLongLinesState(
normalizeCodeBlockWrapLongLines(event.newValue),
);
}
};
const onLanguage = (event: Event) => {
const detail = (event as CustomEvent<{ language?: AppLanguage }>).detail;
setLanguageState(normalizeLanguage(detail?.language));
};
const onActiveSession = (event: Event) => {
const detail = (event as CustomEvent<{ sessionId?: string | null }>)
.detail;
setActiveSessionIdState(detail?.sessionId ?? null);
};
const onSidebarCollapsed = (event: Event) => {
const detail = (event as CustomEvent<{ collapsed?: boolean }>).detail;
setSidebarCollapsedState(Boolean(detail?.collapsed));
};
const onCodeBlockSettings = (event: Event) => {
const detail = (
event as CustomEvent<{
codeBlockTheme?: string;
codeBlockShowLineNumbers?: boolean;
codeBlockWrapLongLines?: boolean;
}>
).detail;
if (detail?.codeBlockTheme !== undefined) {
setCodeBlockThemeState(normalizeCodeBlockTheme(detail.codeBlockTheme));
}
if (detail?.codeBlockShowLineNumbers !== undefined) {
setCodeBlockShowLineNumbersState(
Boolean(detail.codeBlockShowLineNumbers),
);
}
if (detail?.codeBlockWrapLongLines !== undefined) {
setCodeBlockWrapLongLinesState(Boolean(detail.codeBlockWrapLongLines));
}
};
window.addEventListener("storage", onStorage);
window.addEventListener(LANGUAGE_EVENT, onLanguage);
window.addEventListener(ACTIVE_SESSION_EVENT, onActiveSession);
window.addEventListener(SIDEBAR_COLLAPSED_EVENT, onSidebarCollapsed);
window.addEventListener(CODE_BLOCK_SETTINGS_EVENT, onCodeBlockSettings);
return () => {
window.removeEventListener("storage", onStorage);
window.removeEventListener(LANGUAGE_EVENT, onLanguage);
window.removeEventListener(ACTIVE_SESSION_EVENT, onActiveSession);
window.removeEventListener(SIDEBAR_COLLAPSED_EVENT, onSidebarCollapsed);
window.removeEventListener(
CODE_BLOCK_SETTINGS_EVENT,
onCodeBlockSettings,
);
};
}, []);
const setTheme = useCallback((nextTheme: Theme) => {
applyThemePreference(nextTheme);
setThemeState(nextTheme);
}, []);
const setLanguage = useCallback((nextLanguage: AppLanguage) => {
writeStoredLanguage(nextLanguage);
setLanguageState(nextLanguage);
}, []);
const setActiveSessionId = useCallback((sessionId: string | null) => {
writeStoredActiveSessionId(sessionId);
setActiveSessionIdState(sessionId);
}, []);
const setSidebarCollapsed = useCallback((collapsed: boolean) => {
writeStoredSidebarCollapsed(collapsed);
setSidebarCollapsedState(collapsed);
}, []);
const setCodeBlockTheme = useCallback((nextTheme: string) => {
const normalizedTheme = normalizeCodeBlockTheme(nextTheme);
writeStoredCodeBlockTheme(normalizedTheme);
setCodeBlockThemeState(normalizedTheme);
}, []);
const setCodeBlockShowLineNumbers = useCallback((show: boolean) => {
writeStoredCodeBlockShowLineNumbers(show);
setCodeBlockShowLineNumbersState(show);
}, []);
const setCodeBlockWrapLongLines = useCallback((wrap: boolean) => {
writeStoredCodeBlockWrapLongLines(wrap);
setCodeBlockWrapLongLinesState(wrap);
}, []);
const value = useMemo<AppShellContextValue>(
() => ({
theme,
setTheme,
language,
setLanguage,
activeSessionId,
setActiveSessionId,
sidebarCollapsed,
setSidebarCollapsed,
codeBlockTheme,
setCodeBlockTheme,
codeBlockShowLineNumbers,
setCodeBlockShowLineNumbers,
codeBlockWrapLongLines,
setCodeBlockWrapLongLines,
}),
[
activeSessionId,
codeBlockShowLineNumbers,
codeBlockTheme,
codeBlockWrapLongLines,
language,
setActiveSessionId,
setCodeBlockShowLineNumbers,
setCodeBlockTheme,
setCodeBlockWrapLongLines,
setLanguage,
setSidebarCollapsed,
setTheme,
sidebarCollapsed,
theme,
],
);
return (
<AppShellContext.Provider value={value}>
{children}
</AppShellContext.Provider>
);
}
export function useAppShell() {
const context = useContext(AppShellContext);
if (!context) {
throw new Error("useAppShell must be used inside AppShellProvider");
}
return context;
}