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.
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
"@radix-ui/react-separator": "^1.1.3",
|
||||
"@radix-ui/react-slot": "^1.1.3",
|
||||
"@radix-ui/react-tooltip": "^1.1.9",
|
||||
"@tanstack/react-router": "^1.169.2",
|
||||
"class-variance-authority": "^0.7.1",
|
||||
"clsx": "^2.1.1",
|
||||
"lucide-react": "^0.469.0",
|
||||
|
||||
+16
-134
@@ -1,18 +1,12 @@
|
||||
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||
import { RouterProvider } from '@tanstack/react-router';
|
||||
import { TooltipProvider } from '@/components/ui/tooltip';
|
||||
import { ThemeProvider, useTheme } from '@/contexts/ThemeContext';
|
||||
import { ThemeProvider } from '@/contexts/ThemeContext';
|
||||
import { SessionProvider } from '@/contexts/SessionContext';
|
||||
import { LoginPage } from '@/components/pages/login-page';
|
||||
import { MainLayout } from '@/components/layout/main-layout';
|
||||
import { OverviewPage } from '@/components/pages/overview-page';
|
||||
import { ConfigPage } from '@/components/pages/config-page';
|
||||
import { LogsPage } from '@/components/pages/logs-page';
|
||||
import { SettingsPage } from '@/components/pages/settings-page';
|
||||
import { ChangePasswordPage } from '@/components/pages/change-password-page';
|
||||
import { ConfirmDialog } from '@/components/confirm-dialog';
|
||||
import type { Page } from '@/components/layout/sidebar';
|
||||
import type { HookProcessInfo, QQInfo, SystemInfo } from '@/types';
|
||||
import { ApiProvider, createApiClient, useApi, type ApiClient } from '@/lib/api';
|
||||
import { useHookProcessOps } from '@/hooks/use-hook-process-ops';
|
||||
import { appRouter } from '@/router';
|
||||
|
||||
export default function App() {
|
||||
return (
|
||||
@@ -51,6 +45,15 @@ function AuthBoundary() {
|
||||
})();
|
||||
}, [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}>
|
||||
@@ -69,14 +72,9 @@ function AuthBoundary() {
|
||||
) : mustChange ? (
|
||||
<ForcedChangePasswordGate onSuccess={() => setMustChange(false)} />
|
||||
) : (
|
||||
<AppInner
|
||||
status={status}
|
||||
onLogoutComplete={() => {
|
||||
setAuthed(false);
|
||||
setStatus('未连接');
|
||||
setMustChange(false);
|
||||
}}
|
||||
/>
|
||||
<SessionProvider value={{ status, onLogoutComplete: handleLogoutComplete }}>
|
||||
<RouterProvider router={appRouter} />
|
||||
</SessionProvider>
|
||||
)}
|
||||
</TooltipProvider>
|
||||
</ApiProvider>
|
||||
@@ -108,119 +106,3 @@ function ForcedChangePasswordGate({ onSuccess }: { onSuccess: () => void }) {
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
interface AppInnerProps {
|
||||
status: string;
|
||||
onLogoutComplete: () => void;
|
||||
}
|
||||
|
||||
function AppInner({ status, onLogoutComplete }: AppInnerProps) {
|
||||
const api = useApi();
|
||||
const { pollInterval } = useTheme();
|
||||
const [active, setActive] = useState<Page>('overview');
|
||||
|
||||
const [qqList, setQqList] = useState<QQInfo[]>([]);
|
||||
const [processList, setProcessList] = useState<HookProcessInfo[]>([]);
|
||||
const [systemInfo, setSystemInfo] = useState<SystemInfo | null>(null);
|
||||
// Lives at AppInner level so the user's selection survives across page
|
||||
// navigation (ConfigPage gets unmounted when you switch tabs).
|
||||
const [selectedUin, setSelectedUin] = useState<string | null>(null);
|
||||
|
||||
const refreshQqList = useCallback(async () => {
|
||||
try {
|
||||
setQqList(await api.qqList());
|
||||
} catch (e) {
|
||||
console.error('qq-list', e);
|
||||
}
|
||||
}, [api]);
|
||||
|
||||
const refreshProcesses = useCallback(async () => {
|
||||
try {
|
||||
setProcessList(await api.processes.list());
|
||||
} catch (e) {
|
||||
console.error('processes', e);
|
||||
}
|
||||
}, [api]);
|
||||
|
||||
const refreshSystem = useCallback(async () => {
|
||||
try {
|
||||
setSystemInfo(await api.system());
|
||||
} catch (e) {
|
||||
console.error('system', e);
|
||||
}
|
||||
}, [api]);
|
||||
|
||||
const { ops: processOps, unloadFailedAlert, dismissUnloadFailedAlert } = useHookProcessOps({
|
||||
onAfterOp: refreshProcesses,
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (pollInterval <= 0) return;
|
||||
let cancelled = false;
|
||||
const tick = async () => {
|
||||
if (cancelled) return;
|
||||
await Promise.all([refreshQqList(), refreshProcesses(), refreshSystem()]);
|
||||
};
|
||||
tick();
|
||||
const interval = setInterval(tick, pollInterval);
|
||||
return () => {
|
||||
cancelled = true;
|
||||
clearInterval(interval);
|
||||
};
|
||||
}, [pollInterval, refreshQqList, refreshProcesses, refreshSystem]);
|
||||
|
||||
const handleLogout = useCallback(async () => {
|
||||
await api.logout();
|
||||
setQqList([]);
|
||||
setProcessList([]);
|
||||
setSystemInfo(null);
|
||||
setSelectedUin(null);
|
||||
onLogoutComplete();
|
||||
}, [api, onLogoutComplete]);
|
||||
|
||||
return (
|
||||
<>
|
||||
<MainLayout active={active} onNavigate={setActive} status={status} onLogout={handleLogout}>
|
||||
{active === 'overview' && (
|
||||
<OverviewPage
|
||||
qqList={qqList}
|
||||
status={status}
|
||||
processList={processList}
|
||||
systemInfo={systemInfo}
|
||||
onRefreshProcesses={refreshProcesses}
|
||||
onRefreshSystem={refreshSystem}
|
||||
processOps={processOps}
|
||||
/>
|
||||
)}
|
||||
{active === 'config' && (
|
||||
<ConfigPage
|
||||
qqList={qqList}
|
||||
selectedUin={selectedUin}
|
||||
onSelectedUinChange={setSelectedUin}
|
||||
/>
|
||||
)}
|
||||
{active === 'logs' && <LogsPage />}
|
||||
{active === 'settings' && <SettingsPage onLogout={handleLogout} />}
|
||||
</MainLayout>
|
||||
|
||||
<ConfirmDialog
|
||||
open={!!unloadFailedAlert}
|
||||
onOpenChange={(open) => !open && dismissUnloadFailedAlert()}
|
||||
title="卸载失败"
|
||||
description={
|
||||
unloadFailedAlert ? (
|
||||
<>
|
||||
<p>进程 {unloadFailedAlert.pid} 的 SnowLuma DLL 卸载失败。</p>
|
||||
<p className="mt-2 text-sm">{unloadFailedAlert.error}</p>
|
||||
<p className="mt-2 text-sm text-muted-foreground">
|
||||
系统将继续尝试重新连接该进程。如需彻底卸载,请重启 QQ 进程。
|
||||
</p>
|
||||
</>
|
||||
) : null
|
||||
}
|
||||
confirmText="知道了"
|
||||
onConfirm={dismissUnloadFailedAlert}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,26 +1,26 @@
|
||||
import { useEffect, useState, type ReactNode } from 'react';
|
||||
import { motion, AnimatePresence } from 'motion/react';
|
||||
import { useRouterState } from '@tanstack/react-router';
|
||||
import { Sheet, SheetContent } from '@/components/ui/sheet';
|
||||
import { ScrollArea } from '@/components/ui/scroll-area';
|
||||
import { Sidebar, type Page } from '@/components/layout/sidebar';
|
||||
import { Sidebar } from '@/components/layout/sidebar';
|
||||
import { TopBar } from '@/components/layout/top-bar';
|
||||
import { useMediaQuery } from '@/hooks/use-media-query';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
interface MainLayoutProps {
|
||||
active: Page;
|
||||
onNavigate: (page: Page) => void;
|
||||
status: string;
|
||||
onLogout: () => void;
|
||||
children: ReactNode;
|
||||
}
|
||||
|
||||
export function MainLayout({ active, onNavigate, status, onLogout, children }: MainLayoutProps) {
|
||||
export function MainLayout({ status, onLogout, children }: MainLayoutProps) {
|
||||
const isMobile = !useMediaQuery('(min-width: 768px)');
|
||||
const [collapsed, setCollapsed] = useState(() => {
|
||||
return localStorage.getItem('snowluma_sidebar_collapsed') === '1';
|
||||
});
|
||||
const [mobileOpen, setMobileOpen] = useState(false);
|
||||
const pathname = useRouterState({ select: (s) => s.location.pathname });
|
||||
|
||||
useEffect(() => {
|
||||
localStorage.setItem('snowluma_sidebar_collapsed', collapsed ? '1' : '0');
|
||||
@@ -36,7 +36,7 @@ export function MainLayout({ active, onNavigate, status, onLogout, children }: M
|
||||
transition={{ type: 'spring', stiffness: 280, damping: 32 }}
|
||||
className="relative h-full shrink-0 border-r overflow-hidden"
|
||||
>
|
||||
<Sidebar active={active} onNavigate={onNavigate} collapsed={collapsed} />
|
||||
<Sidebar collapsed={collapsed} />
|
||||
</motion.aside>
|
||||
)}
|
||||
|
||||
@@ -44,11 +44,7 @@ export function MainLayout({ active, onNavigate, status, onLogout, children }: M
|
||||
{isMobile && (
|
||||
<Sheet open={mobileOpen} onOpenChange={setMobileOpen}>
|
||||
<SheetContent side="left" className="w-64 max-w-[80vw] p-0">
|
||||
<Sidebar
|
||||
active={active}
|
||||
onNavigate={onNavigate}
|
||||
onItemClick={() => setMobileOpen(false)}
|
||||
/>
|
||||
<Sidebar onItemClick={() => setMobileOpen(false)} />
|
||||
</SheetContent>
|
||||
</Sheet>
|
||||
)}
|
||||
@@ -56,7 +52,6 @@ export function MainLayout({ active, onNavigate, status, onLogout, children }: M
|
||||
{/* Main column */}
|
||||
<div className="flex min-w-0 flex-1 flex-col">
|
||||
<TopBar
|
||||
active={active}
|
||||
status={status}
|
||||
collapsed={collapsed}
|
||||
onToggleCollapse={() => setCollapsed((v) => !v)}
|
||||
@@ -70,7 +65,7 @@ export function MainLayout({ active, onNavigate, status, onLogout, children }: M
|
||||
<div className="mx-auto w-full max-w-7xl px-4 py-5 sm:px-6 sm:py-6 lg:px-8">
|
||||
<AnimatePresence mode="wait">
|
||||
<motion.div
|
||||
key={active}
|
||||
key={pathname}
|
||||
initial={{ opacity: 0, y: 10 }}
|
||||
animate={{ opacity: 1, y: 0 }}
|
||||
exit={{ opacity: 0, y: -8 }}
|
||||
|
||||
@@ -1,27 +1,34 @@
|
||||
import { LayoutDashboard, Settings, SlidersHorizontal, Terminal } from 'lucide-react';
|
||||
import { motion } from 'motion/react';
|
||||
import { Link, useRouterState } from '@tanstack/react-router';
|
||||
import { ScrollArea } from '@/components/ui/scroll-area';
|
||||
import { Separator } from '@/components/ui/separator';
|
||||
import { cn } from '@/lib/utils';
|
||||
import { APP_NAME, APP_VERSION } from '@/types';
|
||||
import type { AppPath } from '@/router';
|
||||
|
||||
export type Page = 'overview' | 'config' | 'logs' | 'settings';
|
||||
export interface NavItem {
|
||||
to: AppPath;
|
||||
label: string;
|
||||
icon: typeof LayoutDashboard;
|
||||
description: string;
|
||||
}
|
||||
|
||||
export const NAV_ITEMS: { page: Page; label: string; icon: typeof LayoutDashboard; description: string }[] = [
|
||||
{ page: 'overview', label: '总览', icon: LayoutDashboard, description: '主机与服务状态' },
|
||||
{ page: 'config', label: '节点配置', icon: Settings, description: 'OneBot 协议端点' },
|
||||
{ page: 'logs', label: '日志', icon: Terminal, description: '实时事件流' },
|
||||
{ page: 'settings', label: '系统设置', icon: SlidersHorizontal, description: '主题与账号' },
|
||||
export const NAV_ITEMS: NavItem[] = [
|
||||
{ to: '/', label: '总览', icon: LayoutDashboard, description: '主机与服务状态' },
|
||||
{ to: '/config', label: '节点配置', icon: Settings, description: 'OneBot 协议端点' },
|
||||
{ to: '/logs', label: '日志', icon: Terminal, description: '实时事件流' },
|
||||
{ to: '/settings', label: '系统设置', icon: SlidersHorizontal, description: '主题与账号' },
|
||||
];
|
||||
|
||||
interface SidebarProps {
|
||||
active: Page;
|
||||
onNavigate: (page: Page) => void;
|
||||
collapsed?: boolean;
|
||||
onItemClick?: () => void;
|
||||
}
|
||||
|
||||
export function Sidebar({ active, onNavigate, collapsed = false, onItemClick }: SidebarProps) {
|
||||
export function Sidebar({ collapsed = false, onItemClick }: SidebarProps) {
|
||||
const pathname = useRouterState({ select: (s) => s.location.pathname });
|
||||
|
||||
return (
|
||||
<div className="flex h-full w-full flex-col bg-sidebar text-sidebar-foreground">
|
||||
{/* Brand */}
|
||||
@@ -43,23 +50,21 @@ export function Sidebar({ active, onNavigate, collapsed = false, onItemClick }:
|
||||
{/* Nav */}
|
||||
<ScrollArea className="flex-1 min-h-0" viewportClassName="[&>div]:!block">
|
||||
<nav className={cn('flex flex-col gap-1 p-2', collapsed && 'items-center')}>
|
||||
{NAV_ITEMS.map(({ page, label, icon: Icon, description }) => {
|
||||
const isActive = active === page;
|
||||
{NAV_ITEMS.map(({ to, label, icon: Icon, description }) => {
|
||||
const isActive = pathname === to;
|
||||
return (
|
||||
<button
|
||||
key={page}
|
||||
type="button"
|
||||
<Link
|
||||
key={to}
|
||||
to={to}
|
||||
title={collapsed ? label : undefined}
|
||||
onClick={() => {
|
||||
onNavigate(page);
|
||||
onItemClick?.();
|
||||
}}
|
||||
onClick={onItemClick}
|
||||
aria-current={isActive ? 'page' : undefined}
|
||||
className={cn(
|
||||
'group relative flex w-full items-center gap-3 rounded-lg px-3 py-2.5 text-sm font-medium transition-colors cursor-pointer outline-none',
|
||||
collapsed && 'w-10 justify-center px-0',
|
||||
isActive
|
||||
? 'text-sidebar-accent-foreground'
|
||||
: 'text-muted-foreground hover:bg-sidebar-accent/40 hover:text-foreground'
|
||||
: 'text-muted-foreground hover:bg-sidebar-accent/40 hover:text-foreground',
|
||||
)}
|
||||
>
|
||||
{isActive && (
|
||||
@@ -76,7 +81,7 @@ export function Sidebar({ active, onNavigate, collapsed = false, onItemClick }:
|
||||
<span className="text-[10px] font-normal text-muted-foreground truncate">{description}</span>
|
||||
</span>
|
||||
)}
|
||||
</button>
|
||||
</Link>
|
||||
);
|
||||
})}
|
||||
</nav>
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
import { useState } from 'react';
|
||||
import { LogOut, Menu, PanelLeftClose, PanelLeftOpen } from 'lucide-react';
|
||||
import { motion, AnimatePresence } from 'motion/react';
|
||||
import { useRouterState } from '@tanstack/react-router';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Badge } from '@/components/ui/badge';
|
||||
import { ThemeToggle } from '@/components/theme-toggle';
|
||||
import { ConfirmDialog } from '@/components/confirm-dialog';
|
||||
import type { Page } from '@/components/layout/sidebar';
|
||||
import { NAV_ITEMS } from '@/components/layout/sidebar';
|
||||
|
||||
interface TopBarProps {
|
||||
active: Page;
|
||||
status: string;
|
||||
collapsed: boolean;
|
||||
onToggleCollapse: () => void;
|
||||
@@ -19,7 +18,6 @@ interface TopBarProps {
|
||||
}
|
||||
|
||||
export function TopBar({
|
||||
active,
|
||||
status,
|
||||
collapsed,
|
||||
onToggleCollapse,
|
||||
@@ -28,7 +26,8 @@ export function TopBar({
|
||||
isMobile,
|
||||
}: TopBarProps) {
|
||||
const [confirmLogout, setConfirmLogout] = useState(false);
|
||||
const meta = NAV_ITEMS.find((n) => n.page === active);
|
||||
const pathname = useRouterState({ select: (s) => s.location.pathname });
|
||||
const meta = NAV_ITEMS.find((n) => n.to === pathname);
|
||||
const PageIcon = meta?.icon;
|
||||
const online = status === '已连接';
|
||||
|
||||
@@ -56,7 +55,7 @@ export function TopBar({
|
||||
{/* Page title */}
|
||||
<AnimatePresence mode="wait">
|
||||
<motion.div
|
||||
key={active}
|
||||
key={pathname}
|
||||
initial={{ opacity: 0, x: -8 }}
|
||||
animate={{ opacity: 1, x: 0 }}
|
||||
exit={{ opacity: 0, x: 8 }}
|
||||
|
||||
@@ -16,16 +16,10 @@ import type {
|
||||
NetworkKind,
|
||||
OneBotConfig,
|
||||
OneBotNetworks,
|
||||
QQInfo,
|
||||
WsRole,
|
||||
} from '@/types';
|
||||
import { useOneBotInstanceConfig } from '@/hooks/use-onebot-instance-config';
|
||||
|
||||
interface ConfigPageProps {
|
||||
qqList: QQInfo[];
|
||||
selectedUin: string | null;
|
||||
onSelectedUinChange: (uin: string | null) => void;
|
||||
}
|
||||
import { useAppState } from '@/contexts/AppStateContext';
|
||||
|
||||
function qqAvatarUrl(uin: string) {
|
||||
return `/avatar/${encodeURIComponent(uin)}`;
|
||||
@@ -95,7 +89,8 @@ function Section({ title, description, onAdd, children, count }: SectionProps) {
|
||||
);
|
||||
}
|
||||
|
||||
export function ConfigPage({ qqList, selectedUin, onSelectedUinChange }: ConfigPageProps) {
|
||||
export function ConfigPage() {
|
||||
const { qqList, selectedUin, setSelectedUin } = useAppState();
|
||||
const {
|
||||
config,
|
||||
setConfig,
|
||||
@@ -106,7 +101,10 @@ export function ConfigPage({ qqList, selectedUin, onSelectedUinChange }: ConfigP
|
||||
cancelSwitch,
|
||||
save,
|
||||
saveStatus,
|
||||
} = useOneBotInstanceConfig(qqList, { selectedUin, onSelectedUinChange });
|
||||
} = useOneBotInstanceConfig(qqList, {
|
||||
selectedUin,
|
||||
onSelectedUinChange: setSelectedUin,
|
||||
});
|
||||
const [confirmSave, setConfirmSave] = useState(false);
|
||||
|
||||
const update = (next: OneBotConfig) => setConfig(next);
|
||||
|
||||
@@ -22,18 +22,9 @@ import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar';
|
||||
import { Skeleton } from '@/components/ui/skeleton';
|
||||
import { ConfirmDialog } from '@/components/confirm-dialog';
|
||||
import { cn, formatBytes, formatUptime } from '@/lib/utils';
|
||||
import type { HookProcessInfo, QQInfo, SystemInfo } from '@/types';
|
||||
import type { HookProcessOps } from '@/hooks/use-hook-process-ops';
|
||||
|
||||
interface OverviewPageProps {
|
||||
qqList: QQInfo[];
|
||||
status: string;
|
||||
processList: HookProcessInfo[];
|
||||
systemInfo: SystemInfo | null;
|
||||
onRefreshProcesses: () => void;
|
||||
onRefreshSystem: () => void;
|
||||
processOps: HookProcessOps;
|
||||
}
|
||||
import type { HookProcessInfo } from '@/types';
|
||||
import { useAppState } from '@/contexts/AppStateContext';
|
||||
import { useSession } from '@/contexts/SessionContext';
|
||||
|
||||
const processStatusLabel: Record<HookProcessInfo['status'], string> = {
|
||||
available: '可加载',
|
||||
@@ -91,15 +82,10 @@ function StatTile({
|
||||
);
|
||||
}
|
||||
|
||||
export function OverviewPage({
|
||||
qqList,
|
||||
status,
|
||||
processList,
|
||||
systemInfo,
|
||||
onRefreshProcesses,
|
||||
onRefreshSystem,
|
||||
processOps,
|
||||
}: OverviewPageProps) {
|
||||
export function OverviewPage() {
|
||||
const { qqList, processList, systemInfo, processOps, refreshProcesses, refreshSystem } =
|
||||
useAppState();
|
||||
const { status } = useSession();
|
||||
const { statusOf, banner: processActionStatus, load, unload, refresh } = processOps;
|
||||
const [confirm, setConfirm] = useState<
|
||||
| { kind: 'load' | 'unload'; pid: number; name: string }
|
||||
@@ -157,7 +143,7 @@ export function OverviewPage({
|
||||
: '正在采集主机信息…'}
|
||||
</CardDescription>
|
||||
</div>
|
||||
<Button variant="outline" size="sm" onClick={onRefreshSystem}>
|
||||
<Button variant="outline" size="sm" onClick={refreshSystem}>
|
||||
<RefreshCw className="size-3.5" /> 刷新
|
||||
</Button>
|
||||
</CardHeader>
|
||||
@@ -260,7 +246,7 @@ export function OverviewPage({
|
||||
加载 SnowLuma 后会监听登录状态,登录后自动接入 OneBot 流程
|
||||
</CardDescription>
|
||||
</div>
|
||||
<Button variant="outline" size="sm" onClick={onRefreshProcesses}>
|
||||
<Button variant="outline" size="sm" onClick={refreshProcesses}>
|
||||
<RefreshCw className="size-3.5" /> 刷新
|
||||
</Button>
|
||||
</CardHeader>
|
||||
|
||||
@@ -16,13 +16,9 @@ import {
|
||||
} from '@/contexts/ThemeContext';
|
||||
import { ChangePasswordPage } from '@/components/pages/change-password-page';
|
||||
import { useApi } from '@/lib/api';
|
||||
import { useAppState } from '@/contexts/AppStateContext';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
interface SettingsPageProps {
|
||||
/** Called after successful logout (e.g. all-sessions invalidated). */
|
||||
onLogout: () => void;
|
||||
}
|
||||
|
||||
const MODE_OPTIONS: { value: ThemeMode; label: string; icon: typeof Sun }[] = [
|
||||
{ value: 'light', label: '浅色', icon: Sun },
|
||||
{ value: 'dark', label: '深色', icon: Moon },
|
||||
@@ -34,8 +30,9 @@ const DENSITY_OPTIONS: { value: Density; label: string; description: string }[]
|
||||
{ value: 'compact', label: '紧凑', description: '更小的字号与行距,单屏放更多内容' },
|
||||
];
|
||||
|
||||
export function SettingsPage({ onLogout }: SettingsPageProps) {
|
||||
export function SettingsPage() {
|
||||
const api = useApi();
|
||||
const { onLogout } = useAppState();
|
||||
const {
|
||||
mode, setMode,
|
||||
accent, setAccent,
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
import { createContext, useContext, type ReactNode } from 'react';
|
||||
import type { HookProcessInfo, QQInfo, SystemInfo } from '@/types';
|
||||
import type { HookProcessOps } from '@/hooks/use-hook-process-ops';
|
||||
|
||||
/**
|
||||
* 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;
|
||||
selectedUin: string | null;
|
||||
setSelectedUin: (uin: string | null) => void;
|
||||
processOps: HookProcessOps;
|
||||
refreshProcesses: () => void;
|
||||
refreshSystem: () => void;
|
||||
/** 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;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
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.
|
||||
*/
|
||||
export interface SessionValue {
|
||||
status: string;
|
||||
/** Invoked by AppLayout's logout handler after it has scrubbed its own state. */
|
||||
onLogoutComplete: () => void;
|
||||
}
|
||||
|
||||
const SessionContext = createContext<SessionValue | null>(null);
|
||||
|
||||
export function SessionProvider({
|
||||
value,
|
||||
children,
|
||||
}: {
|
||||
value: SessionValue;
|
||||
children: ReactNode;
|
||||
}) {
|
||||
return <SessionContext.Provider value={value}>{children}</SessionContext.Provider>;
|
||||
}
|
||||
|
||||
export function useSession(): SessionValue {
|
||||
const v = useContext(SessionContext);
|
||||
if (!v) throw new Error('useSession must be used inside <SessionProvider>');
|
||||
return v;
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
import { useCallback, useEffect, useState } from 'react';
|
||||
import { Outlet } from '@tanstack/react-router';
|
||||
import { useTheme } from '@/contexts/ThemeContext';
|
||||
import { useApi } from '@/lib/api';
|
||||
import { useHookProcessOps } from '@/hooks/use-hook-process-ops';
|
||||
import { MainLayout } from '@/components/layout/main-layout';
|
||||
import { ConfirmDialog } from '@/components/confirm-dialog';
|
||||
import { AppStateProvider } from '@/contexts/AppStateContext';
|
||||
import { useSession } from '@/contexts/SessionContext';
|
||||
import type { HookProcessInfo, QQInfo, SystemInfo } from '@/types';
|
||||
|
||||
/**
|
||||
* The layout route. Owns the live state shared across the four pages
|
||||
* (polling lists, processOps, selectedUin) and renders `<Outlet />` inside
|
||||
* the chrome. The unload-failed alert sits here so it survives navigation
|
||||
* away from the overview page.
|
||||
*/
|
||||
export function AppLayout() {
|
||||
const api = useApi();
|
||||
const { pollInterval } = useTheme();
|
||||
const session = useSession();
|
||||
|
||||
const [qqList, setQqList] = useState<QQInfo[]>([]);
|
||||
const [processList, setProcessList] = useState<HookProcessInfo[]>([]);
|
||||
const [systemInfo, setSystemInfo] = useState<SystemInfo | null>(null);
|
||||
const [selectedUin, setSelectedUin] = useState<string | null>(null);
|
||||
|
||||
const refreshQqList = useCallback(async () => {
|
||||
try {
|
||||
setQqList(await api.qqList());
|
||||
} catch (e) {
|
||||
console.error('qq-list', e);
|
||||
}
|
||||
}, [api]);
|
||||
|
||||
const refreshProcesses = useCallback(async () => {
|
||||
try {
|
||||
setProcessList(await api.processes.list());
|
||||
} catch (e) {
|
||||
console.error('processes', e);
|
||||
}
|
||||
}, [api]);
|
||||
|
||||
const refreshSystem = useCallback(async () => {
|
||||
try {
|
||||
setSystemInfo(await api.system());
|
||||
} catch (e) {
|
||||
console.error('system', e);
|
||||
}
|
||||
}, [api]);
|
||||
|
||||
const { ops: processOps, unloadFailedAlert, dismissUnloadFailedAlert } = useHookProcessOps({
|
||||
onAfterOp: refreshProcesses,
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (pollInterval <= 0) return;
|
||||
let cancelled = false;
|
||||
const tick = async () => {
|
||||
if (cancelled) return;
|
||||
await Promise.all([refreshQqList(), refreshProcesses(), refreshSystem()]);
|
||||
};
|
||||
tick();
|
||||
const interval = setInterval(tick, pollInterval);
|
||||
return () => {
|
||||
cancelled = true;
|
||||
clearInterval(interval);
|
||||
};
|
||||
}, [pollInterval, refreshQqList, refreshProcesses, refreshSystem]);
|
||||
|
||||
const handleLogout = useCallback(async () => {
|
||||
await api.logout();
|
||||
setQqList([]);
|
||||
setProcessList([]);
|
||||
setSystemInfo(null);
|
||||
setSelectedUin(null);
|
||||
session.onLogoutComplete();
|
||||
}, [api, session]);
|
||||
|
||||
return (
|
||||
<AppStateProvider
|
||||
value={{
|
||||
qqList,
|
||||
processList,
|
||||
systemInfo,
|
||||
selectedUin,
|
||||
setSelectedUin,
|
||||
processOps,
|
||||
refreshProcesses,
|
||||
refreshSystem,
|
||||
onLogout: handleLogout,
|
||||
}}
|
||||
>
|
||||
<MainLayout status={session.status} onLogout={handleLogout}>
|
||||
<Outlet />
|
||||
</MainLayout>
|
||||
|
||||
<ConfirmDialog
|
||||
open={!!unloadFailedAlert}
|
||||
onOpenChange={(open) => !open && dismissUnloadFailedAlert()}
|
||||
title="卸载失败"
|
||||
description={
|
||||
unloadFailedAlert ? (
|
||||
<>
|
||||
<p>进程 {unloadFailedAlert.pid} 的 SnowLuma DLL 卸载失败。</p>
|
||||
<p className="mt-2 text-sm">{unloadFailedAlert.error}</p>
|
||||
<p className="mt-2 text-sm text-muted-foreground">
|
||||
系统将继续尝试重新连接该进程。如需彻底卸载,请重启 QQ 进程。
|
||||
</p>
|
||||
</>
|
||||
) : null
|
||||
}
|
||||
confirmText="知道了"
|
||||
onConfirm={dismissUnloadFailedAlert}
|
||||
/>
|
||||
</AppStateProvider>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
import {
|
||||
createRootRoute,
|
||||
createRoute,
|
||||
createRouter,
|
||||
Outlet,
|
||||
} from '@tanstack/react-router';
|
||||
import { OverviewPage } from '@/components/pages/overview-page';
|
||||
import { ConfigPage } from '@/components/pages/config-page';
|
||||
import { LogsPage } from '@/components/pages/logs-page';
|
||||
import { SettingsPage } from '@/components/pages/settings-page';
|
||||
import { AppLayout } from './app-layout';
|
||||
|
||||
const rootRoute = createRootRoute({
|
||||
component: () => <Outlet />,
|
||||
});
|
||||
|
||||
const appLayoutRoute = createRoute({
|
||||
id: 'app-layout',
|
||||
getParentRoute: () => rootRoute,
|
||||
component: AppLayout,
|
||||
});
|
||||
|
||||
const overviewRoute = createRoute({
|
||||
path: '/',
|
||||
getParentRoute: () => appLayoutRoute,
|
||||
component: OverviewPage,
|
||||
});
|
||||
|
||||
const configRoute = createRoute({
|
||||
path: '/config',
|
||||
getParentRoute: () => appLayoutRoute,
|
||||
component: ConfigPage,
|
||||
});
|
||||
|
||||
const logsRoute = createRoute({
|
||||
path: '/logs',
|
||||
getParentRoute: () => appLayoutRoute,
|
||||
component: LogsPage,
|
||||
});
|
||||
|
||||
const settingsRoute = createRoute({
|
||||
path: '/settings',
|
||||
getParentRoute: () => appLayoutRoute,
|
||||
component: SettingsPage,
|
||||
});
|
||||
|
||||
const routeTree = rootRoute.addChildren([
|
||||
appLayoutRoute.addChildren([overviewRoute, configRoute, logsRoute, settingsRoute]),
|
||||
]);
|
||||
|
||||
export const appRouter = createRouter({
|
||||
routeTree,
|
||||
defaultPreload: 'intent',
|
||||
});
|
||||
|
||||
declare module '@tanstack/react-router' {
|
||||
interface Register {
|
||||
router: typeof appRouter;
|
||||
}
|
||||
}
|
||||
|
||||
/** Paths registered on the layout — single source of truth for nav metadata. */
|
||||
export type AppPath = '/' | '/config' | '/logs' | '/settings';
|
||||
Generated
+90
@@ -110,6 +110,9 @@ importers:
|
||||
'@radix-ui/react-tooltip':
|
||||
specifier: ^1.1.9
|
||||
version: 1.2.8(@types/react-dom@19.2.3(@types/react@19.2.14))(@types/react@19.2.14)(react-dom@19.2.5(react@19.2.5))(react@19.2.5)
|
||||
'@tanstack/react-router':
|
||||
specifier: ^1.169.2
|
||||
version: 1.169.2(react-dom@19.2.5(react@19.2.5))(react@19.2.5)
|
||||
class-variance-authority:
|
||||
specifier: ^0.7.1
|
||||
version: 0.7.1
|
||||
@@ -1150,6 +1153,30 @@ packages:
|
||||
peerDependencies:
|
||||
vite: ^5.2.0 || ^6 || ^7 || ^8
|
||||
|
||||
'@tanstack/history@1.161.6':
|
||||
resolution: {integrity: sha512-NaOGLRrddszbQj9upGat6HG/4TKvXLvu+osAIgfxPYA+eIvYKv8GKDJOrY2D3/U9MRnKfMWD7bU4jeD4xmqyIg==}
|
||||
engines: {node: '>=20.19'}
|
||||
|
||||
'@tanstack/react-router@1.169.2':
|
||||
resolution: {integrity: sha512-OJM7Kguc7ERnweaNRWsyWgIKcl3z23rD1B4jaxjzd9RGdnzpt2HfrWa9rggbT0Hfzhfo4D2ZmsfoTme035tniQ==}
|
||||
engines: {node: '>=20.19'}
|
||||
peerDependencies:
|
||||
react: '>=18.0.0 || >=19.0.0'
|
||||
react-dom: '>=18.0.0 || >=19.0.0'
|
||||
|
||||
'@tanstack/react-store@0.9.3':
|
||||
resolution: {integrity: sha512-y2iHd/N9OkoQbFJLUX1T9vbc2O9tjH0pQRgTcx1/Nz4IlwLvkgpuglXUx+mXt0g5ZDFrEeDnONPqkbfxXJKwRg==}
|
||||
peerDependencies:
|
||||
react: ^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0
|
||||
react-dom: ^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0
|
||||
|
||||
'@tanstack/router-core@1.169.2':
|
||||
resolution: {integrity: sha512-5sm0DJF1A7Mz+9gy4Gz/lLovNailK3yot4vYvz9MkBUPw26uLnhQiR8hSCYxucjE0wD6Mdlc5l+Z0/XTlZ7xHw==}
|
||||
engines: {node: '>=20.19'}
|
||||
|
||||
'@tanstack/store@0.9.3':
|
||||
resolution: {integrity: sha512-8reSzl/qGWGGVKhBoxXPMWzATSbZLZFWhwBAFO9NAyp0TxzfBP0mIrGb8CP8KrQTmvzXlR/vFPPUrHTLBGyFyw==}
|
||||
|
||||
'@tybys/wasm-util@0.10.1':
|
||||
resolution: {integrity: sha512-9tTaPJLSiejZKx+Bmog4uSubteqTvFrVrURwkmHixBo0G4seD0zUxp98E1DzUBJxLQ3NPwXrGKDiVjwx/DpPsg==}
|
||||
|
||||
@@ -1371,6 +1398,9 @@ packages:
|
||||
convert-source-map@2.0.0:
|
||||
resolution: {integrity: sha512-Kvp459HrV2FEJ1CAsi1Ku+MY3kasH19TFykTz2xWmMeq6bk2NU3XXvfJ+Q61m0xktWwt+1HSYf3JZsTms3aRJg==}
|
||||
|
||||
cookie-es@3.1.1:
|
||||
resolution: {integrity: sha512-UaXxwISYJPTr9hwQxMFYZ7kNhSXboMXP+Z3TRX6f1/NyaGPfuNUZOWP1pUEb75B2HjfklIYLVRfWiFZJyC6Npg==}
|
||||
|
||||
cross-env@10.1.0:
|
||||
resolution: {integrity: sha512-GsYosgnACZTADcmEyJctkJIoqAhHjttw7RsFrVoJNXbsWWqaq6Ym+7kZjq6mS45O0jij6vtiReppKQEtqWy6Dw==}
|
||||
engines: {node: '>=20'}
|
||||
@@ -1632,6 +1662,10 @@ packages:
|
||||
is-what-type@1.1.4:
|
||||
resolution: {integrity: sha512-120PeT8LdXO7m503rQtOapWA7lHBtWErN+asRxKyxF/SFGeqLEAZhlItjNynNVABHteRMSjomQCdAnxEJldMMQ==}
|
||||
|
||||
isbot@5.1.40:
|
||||
resolution: {integrity: sha512-yNeeynhhtIVRBk12tBV4eHNxwB42HzR4Q3Ea7vCOiJhImGaAIdIMrbJtacQlBizGLjUPw+akkFI5Dn9T70XoVQ==}
|
||||
engines: {node: '>=18'}
|
||||
|
||||
isexe@2.0.0:
|
||||
resolution: {integrity: sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==}
|
||||
|
||||
@@ -1922,6 +1956,16 @@ packages:
|
||||
engines: {node: '>=10'}
|
||||
hasBin: true
|
||||
|
||||
seroval-plugins@1.5.4:
|
||||
resolution: {integrity: sha512-S0xQPhUTefAhNvNWFg0c1J8qJArHt5KdtJ/cFAofo06KD1MVSeFWyl4iiu+ApDIuw0WhjpOfCdgConOfAnLgkw==}
|
||||
engines: {node: '>=10'}
|
||||
peerDependencies:
|
||||
seroval: ^1.0
|
||||
|
||||
seroval@1.5.4:
|
||||
resolution: {integrity: sha512-46uFvgrXTVxZcUorgSSRZ4y+ieqLLQRMlG4bnCZKW3qI6BZm7Rg4ntMW4p1mILEEBZWrFlcpp0AyIIlM6jD9iw==}
|
||||
engines: {node: '>=10'}
|
||||
|
||||
shebang-command@2.0.0:
|
||||
resolution: {integrity: sha512-kHxr2zZpYtdmrN1qDjrrX/Z1rR1kG8Dx+gkpK1G4eXmvXswmcE1hTWBWYUzlraYw1/yZp6YuDY77YtvbN0dmDA==}
|
||||
engines: {node: '>=8'}
|
||||
@@ -2044,6 +2088,11 @@ packages:
|
||||
'@types/react':
|
||||
optional: true
|
||||
|
||||
use-sync-external-store@1.6.0:
|
||||
resolution: {integrity: sha512-Pp6GSwGP/NrPIrxVFAIkOQeyw8lFenOHijQWkUTrDvrF4ALqylP2C/KCkeS9dpUM3KvYRQhna5vt7IL95+ZQ9w==}
|
||||
peerDependencies:
|
||||
react: ^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0
|
||||
|
||||
vite-plugin-cp@6.0.3:
|
||||
resolution: {integrity: sha512-qxKGH3v9wPwUDbDchJf4IH4mRE1zkLWgzZSxwrl8LapwUWm48IFS7SlbRmcF4NquC/fESA5cHFge9E2Ps+woxg==}
|
||||
engines: {node: '>=14.18.0', vite: '>=3.1.0'}
|
||||
@@ -2944,6 +2993,33 @@ snapshots:
|
||||
tailwindcss: 4.2.2
|
||||
vite: 8.0.8(@types/node@25.6.0)(esbuild@0.27.7)(jiti@2.6.1)(sass@1.99.0)(tsx@4.21.0)
|
||||
|
||||
'@tanstack/history@1.161.6': {}
|
||||
|
||||
'@tanstack/react-router@1.169.2(react-dom@19.2.5(react@19.2.5))(react@19.2.5)':
|
||||
dependencies:
|
||||
'@tanstack/history': 1.161.6
|
||||
'@tanstack/react-store': 0.9.3(react-dom@19.2.5(react@19.2.5))(react@19.2.5)
|
||||
'@tanstack/router-core': 1.169.2
|
||||
isbot: 5.1.40
|
||||
react: 19.2.5
|
||||
react-dom: 19.2.5(react@19.2.5)
|
||||
|
||||
'@tanstack/react-store@0.9.3(react-dom@19.2.5(react@19.2.5))(react@19.2.5)':
|
||||
dependencies:
|
||||
'@tanstack/store': 0.9.3
|
||||
react: 19.2.5
|
||||
react-dom: 19.2.5(react@19.2.5)
|
||||
use-sync-external-store: 1.6.0(react@19.2.5)
|
||||
|
||||
'@tanstack/router-core@1.169.2':
|
||||
dependencies:
|
||||
'@tanstack/history': 1.161.6
|
||||
cookie-es: 3.1.1
|
||||
seroval: 1.5.4
|
||||
seroval-plugins: 1.5.4(seroval@1.5.4)
|
||||
|
||||
'@tanstack/store@0.9.3': {}
|
||||
|
||||
'@tybys/wasm-util@0.10.1':
|
||||
dependencies:
|
||||
tslib: 2.8.1
|
||||
@@ -3206,6 +3282,8 @@ snapshots:
|
||||
|
||||
convert-source-map@2.0.0: {}
|
||||
|
||||
cookie-es@3.1.1: {}
|
||||
|
||||
cross-env@10.1.0:
|
||||
dependencies:
|
||||
'@epic-web/invariant': 1.0.0
|
||||
@@ -3467,6 +3545,8 @@ snapshots:
|
||||
|
||||
is-what-type@1.1.4: {}
|
||||
|
||||
isbot@5.1.40: {}
|
||||
|
||||
isexe@2.0.0: {}
|
||||
|
||||
jiti@2.6.1: {}
|
||||
@@ -3715,6 +3795,12 @@ snapshots:
|
||||
|
||||
semver@7.7.4: {}
|
||||
|
||||
seroval-plugins@1.5.4(seroval@1.5.4):
|
||||
dependencies:
|
||||
seroval: 1.5.4
|
||||
|
||||
seroval@1.5.4: {}
|
||||
|
||||
shebang-command@2.0.0:
|
||||
dependencies:
|
||||
shebang-regex: 3.0.0
|
||||
@@ -3811,6 +3897,10 @@ snapshots:
|
||||
optionalDependencies:
|
||||
'@types/react': 19.2.14
|
||||
|
||||
use-sync-external-store@1.6.0(react@19.2.5):
|
||||
dependencies:
|
||||
react: 19.2.5
|
||||
|
||||
vite-plugin-cp@6.0.3:
|
||||
dependencies:
|
||||
fs-extra: 11.3.4
|
||||
|
||||
Reference in New Issue
Block a user