refactor: use native async discovery and simplify shared state

This commit is contained in:
手瓜一十雪 committed 2026-10-18 12:57:18 +08:00
1 parent 5032cbb8c0
commit 4e2b6b1200
87 files changed
+2063 -2002

No files matched your search

@@ -1,35 +1,11 @@
import { createContext, useContext, type ReactNode } from 'react';
import type { AccountConnections, HookProcessInfo, QQInfo, SystemInfo, UpdateInfo } from '@/types';
import type { UpdateInfo } from '@/types';
import type { AppResourceData, AppResources } from '@/lib/app-resources';
import type { HookProcessOps } from '@/hooks/use-hook-process-ops';
export interface ResourceLoadState {
ready: boolean;
error: string | null;
}
export interface AppResourceState {
qqList: ResourceLoadState;
processList: ResourceLoadState;
systemInfo: ResourceLoadState;
connections: ResourceLoadState;
updateInfo: ResourceLoadState;
}
/**
* Live mutable state owned by AppLayout and shared with the child route
* components rendered inside its `<Outlet />`. Pages used to receive these
* via prop drilling — now they pull from context so adding a new page does
* not touch AppLayout.
*/
export interface AppStateValue {
qqList: QQInfo[];
processList: HookProcessInfo[];
systemInfo: SystemInfo | null;
connections: AccountConnections[];
/** Advisory update-availability info; null until the first check resolves. */
updateInfo: UpdateInfo | null;
export interface AppStateValue extends AppResourceData {
/** Distinguishes an empty result from an unresolved or failed initial read. */
resources: AppResourceState;
resources: AppResources;
selectedUin: string | null;
setSelectedUin: (uin: string | null) => void;
processOps: HookProcessOps;
@@ -1,13 +1,9 @@
import { createContext, useContext, type ReactNode } from 'react';
/**
* Auth-level state surfaced into the router subtree. `AuthBoundary` mints
* this and `AppLayout` consumes it — used to drive the status badge and to
* complete logout after page-local state has been cleared.
*/
/** Session state shared by the authentication boundary and routed pages. */
export interface SessionValue {
status: string;
/** Invoked by AppLayout's logout handler after it has scrubbed its own state. */
/** Ends the session and unmounts its page state. */
onLogoutComplete: () => void;
/** Replays the first-run wizard without changing persisted account state. */
restartOnboarding: () => void;
+4 -88
View File
@@ -228,7 +228,6 @@ export const DEFAULT_APPEARANCE: UiAppearance = {
export const FONT_CUSTOM_ID = 'custom';
const LS_CACHE = 'snowluma_ui_appearance';
const LS_MIGRATED = 'snowluma_ui_migrated';
const TOKEN_KEY = 'snowluma_token';
// ─── Colour helpers (custom hex accent → readable foreground) ──────────────
@@ -1012,61 +1011,6 @@ async function persistAppearance(appearance: UiAppearance): Promise<void> {
}
}
// ─── Migration of the pre-server-config localStorage theme ─────────────────
function readLegacyOverlay(): Partial<UiAppearance> | null {
try {
const out: Partial<UiAppearance> = {};
const mode = localStorage.getItem('snowluma_theme');
if (mode === 'light' || mode === 'dark' || mode === 'system') {
out.mode = mode;
}
const accent = localStorage.getItem('snowluma_accent');
if (accent && ACCENTS.some((a) => a.id === accent)) {
out.accentMode = 'preset';
out.accentPreset = accent;
}
const radius = Number(localStorage.getItem('snowluma_radius'));
if (Number.isFinite(radius) && radius > 0 && radius <= 2) {
out.radius = radius;
}
const density = localStorage.getItem('snowluma_density');
if (density === 'cozy' || density === 'compact') {
out.density = density;
}
const poll = Number(localStorage.getItem('snowluma_poll_interval'));
if (Number.isFinite(poll) && poll >= 0 && poll <= 60_000) {
out.pollInterval = poll;
}
return Object.keys(out).length > 0 ? out : null;
} catch {
return null;
}
}
function isPristine(a: UiAppearance): boolean {
const d = DEFAULT_APPEARANCE;
return (
a.mode === d.mode &&
a.accentMode === d.accentMode &&
a.accentPreset === d.accentPreset &&
a.radius === d.radius &&
a.density === d.density &&
a.pollInterval === d.pollInterval &&
a.background.type === 'none'
);
}
// ─── Light/dark reveal (View Transitions API) ──────────────────────────────
// Where the user last clicked, so the theme reveal emanates from there.
let lastPointer: { x: number; y: number } | null = null;
@@ -1149,7 +1093,7 @@ interface ThemeContextValue {
/** Re-fetch appearance from the server (e.g. after login, so the authed
* `customCss` loads). */
reloadAppearance: () => Promise<void>;
// ── back-compat conveniences ──
// Common appearance controls.
mode: ThemeMode;
setMode: (m: ThemeMode) => void;
pollInterval: number;
@@ -1190,7 +1134,7 @@ export function ThemeProvider({ children }: { children: ReactNode }) {
return () => mq.removeEventListener('change', handler);
}, []);
// Initial server load (+ one-time legacy migration). Runs once.
// Load the server appearance, retaining the cache if it is unavailable.
useEffect(() => {
let cancelled = false;
@@ -1201,34 +1145,7 @@ export function ThemeProvider({ children }: { children: ReactNode }) {
return;
}
let next = server ?? readCache();
let migrated = true;
try {
migrated = Boolean(localStorage.getItem(LS_MIGRATED));
} catch {
/* storage unavailable: skip migration */
}
if (!migrated) {
try {
localStorage.setItem(LS_MIGRATED, '1');
} catch {
/* ignore */
}
const legacy = readLegacyOverlay();
// Only migrate when the server has never been customized, so we don't
// clobber a look already set from another device.
if (legacy && server && isPristine(server)) {
next = { ...server, ...legacy, background: server.background };
void persistAppearance(next).catch((error) => {
console.error('appearance migration save failed', error);
});
}
}
const next = server ?? readCache();
setAppearanceState(next);
writeCache(next);
@@ -1256,8 +1173,7 @@ export function ThemeProvider({ children }: { children: ReactNode }) {
applyBackgroundLayer(appearance);
}, [appearance]);
// Flush a pending debounced save on unmount (defensive — the provider lives
// at the app root and normally never unmounts).
// Cancel a pending save when the provider unmounts.
useEffect(
() => () => {
if (saveTimer.current) {