Files
DeepTutor/web/context/app-shell-storage.ts
Bingxi Zhao (Frank) 77f29cbcc7 fix lint
2026-04-23 19:29:16 +08:00

94 lines
2.5 KiB
TypeScript

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