Files
SnowLuma/packages/webui/src/App.tsx
T
DrSmoothl a1a49944df refactor(webui): migrate routing to TanStack Router
The four-page switch lived as a string union (Page) plus an if/else
chain in AppInner, with TopBar reverse-importing Sidebar's NAV_ITEMS
to find the icon for the active tab. Adding a page touched four
places and there was no URL, no history, no deep-linking.

Move routing behind @tanstack/react-router with a layout route
(AppLayout) that owns the polling lists, processOps, selectedUin
and the unload-failed alert; child routes render through <Outlet/>
and read shared state from a new AppStateContext. AuthBoundary still
gates login / forced-password-change above the router. Sidebar uses
<Link> + useRouterState for active state; TopBar and MainLayout look
up the current path the same way.

Behaviour-preserving:
- LoginPage / ChangePasswordPage still mount above the router.
- AppLayout never unmounts during navigation, so selectedUin,
  processOps state, the polling state and the unload-failed dialog
  all survive page switches just like before.
- Logout calls history.replaceState('/') so the next login lands on
  the overview page, matching pre-router behaviour.
- The sidebar-active-pill layoutId animation and the page-transition
  AnimatePresence are preserved, keyed off pathname instead of the
  removed `active` prop.

New URLs (/, /config, /logs, /settings) work on refresh because the
backend already had SPA fallback in core/src/webui/server.ts. Bundle
grows ~84KB (gzip +27KB) for the router runtime.
2026-05-16 02:35:49 +08:00

109 lines
3.2 KiB
TypeScript

import { useCallback, useEffect, useMemo, useState } from 'react';
import { RouterProvider } from '@tanstack/react-router';
import { TooltipProvider } from '@/components/ui/tooltip';
import { ThemeProvider } from '@/contexts/ThemeContext';
import { SessionProvider } from '@/contexts/SessionContext';
import { LoginPage } from '@/components/pages/login-page';
import { ChangePasswordPage } from '@/components/pages/change-password-page';
import { ApiProvider, createApiClient, useApi, type ApiClient } from '@/lib/api';
import { appRouter } from '@/router';
export default function App() {
return (
<ThemeProvider>
<AuthBoundary />
</ThemeProvider>
);
}
function AuthBoundary() {
const [authChecked, setAuthChecked] = useState(false);
const [authed, setAuthed] = useState(false);
const [mustChange, setMustChange] = useState(false);
const [status, setStatus] = useState('未连接');
const client = useMemo<ApiClient>(
() =>
createApiClient({
onUnauthorized: () => {
setAuthed(false);
setStatus('未授权');
},
}),
[],
);
useEffect(() => {
(async () => {
const ok = await client.status();
if (ok) {
setAuthed(true);
setStatus('已连接');
setMustChange(await client.mustChangePassword());
}
setAuthChecked(true);
})();
}, [client]);
const handleLogoutComplete = useCallback(() => {
// Reset the URL so the next login lands on the overview page, matching
// the pre-router behaviour.
window.history.replaceState({}, '', '/');
setAuthed(false);
setStatus('未连接');
setMustChange(false);
}, []);
return (
<ApiProvider client={client}>
<TooltipProvider delayDuration={150}>
{!authChecked ? (
<div className="flex min-h-screen items-center justify-center bg-background text-sm text-muted-foreground">
初始化中…
</div>
) : !authed ? (
<LoginGate
onAuthed={(needsChange) => {
setAuthed(true);
setStatus('已连接');
setMustChange(needsChange);
}}
/>
) : mustChange ? (
<ForcedChangePasswordGate onSuccess={() => setMustChange(false)} />
) : (
<SessionProvider value={{ status, onLogoutComplete: handleLogoutComplete }}>
<RouterProvider router={appRouter} />
</SessionProvider>
)}
</TooltipProvider>
</ApiProvider>
);
}
function LoginGate({ onAuthed }: { onAuthed: (mustChange: boolean) => void }) {
const api = useApi();
const handleLogin = useCallback(
async (password: string) => {
const result = await api.login(password);
if (!result.ok) return { success: false, error: result.message };
onAuthed(result.mustChangePassword);
return { success: true };
},
[api, onAuthed],
);
return <LoginPage onLogin={handleLogin} />;
}
function ForcedChangePasswordGate({ onSuccess }: { onSuccess: () => void }) {
const api = useApi();
return (
<ChangePasswordPage
forced
checkStrength={(p) => api.checkPasswordStrength(p)}
submit={(o, n) => api.changePassword(o, n)}
onSuccess={onSuccess}
/>
);
}