Files
SnowLuma/packages/webui/src/contexts/AppStateContext.tsx
T
DrSmoothl 3c13838867 feat(webui): standardize content loading states
Use consistent delayed placeholders across data-backed console surfaces.
Avoid flashing loading UI for fast local responses while keeping slower transitions stable.
Keep failures visible and prevent gated content from opening on incomplete checks.
2026-07-31 16:56:44 +08:00

63 lines
2.0 KiB
TypeScript

import { createContext, useContext, type ReactNode } from 'react';
import type { AccountConnections, HookProcessInfo, QQInfo, SystemInfo, UpdateInfo } from '@/types';
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;
/** Distinguishes an empty result from an unresolved or failed initial read. */
resources: AppResourceState;
selectedUin: string | null;
setSelectedUin: (uin: string | null) => void;
processOps: HookProcessOps;
refreshQqList: () => void;
refreshProcesses: () => void;
refreshSystem: () => void;
refreshConnections: () => void;
/** Re-run the update check; `force` bypasses the server-side cache. */
refreshUpdate: (force?: boolean) => Promise<UpdateInfo | null>;
/** Triggered from the topbar logout button. */
onLogout: () => void;
}
const AppStateContext = createContext<AppStateValue | null>(null);
export function AppStateProvider({
value,
children,
}: {
value: AppStateValue;
children: ReactNode;
}) {
return <AppStateContext.Provider value={value}>{children}</AppStateContext.Provider>;
}
export function useAppState(): AppStateValue {
const v = useContext(AppStateContext);
if (!v) throw new Error('useAppState must be used inside <AppStateProvider>');
return v;
}