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:
13 files changed
+413
-209
No files matched your search
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user