收口 React 认证后路由壳边界

This commit is contained in:
Christ committed 2026-08-15 19:47:39 +08:00
1 parent 627fd838e6
commit dbd337a508
19 files changed
+223 -104

No files matched your search

+29 -83
View File
@@ -1,44 +1,9 @@
import React, { lazy, Suspense, useState, useEffect } from 'react';
import Sidebar from './components/Sidebar';
import React, { useState, useEffect } from 'react';
import { readSidebarCollapsed, writeSidebarCollapsed } from './components/sidebarState';
import { YdisksBrandIcon } from './components/YdisksLogo';
import { initializeAdmin, login, logout, verifySession } from './app/features/session/api';
import { ShieldCheck, ArrowRight, Loader2, User, Lock } from 'lucide-react';
// Dashboard 是按需加载的仪表盘页面,避免首屏同步载入图表依赖。
const Dashboard = lazy(/* Dashboard 页面按路由激活时加载。 */ () => import('./components/Dashboard'));
// AccountList 是按需加载的账号管理页面,避免未访问时载入账号弹窗和二维码代码。
const AccountList = lazy(/* AccountList 页面按路由激活时加载。 */ () => import('./components/AccountList'));
// OrderList 是按需加载的订单页面,避免首屏载入订单导入与刷新代码。
const OrderList = lazy(/* OrderList 页面按路由激活时加载。 */ () => import('./components/OrderList'));
// CardList 是按需加载的卡密页面,避免首屏载入卡密批量处理代码。
const CardList = lazy(/* CardList 页面按路由激活时加载。 */ () => import('./components/CardList'));
// ItemList 是按需加载的商品页面,避免首屏载入商品发布编辑器代码。
const ItemList = lazy(/* ItemList 页面按路由激活时加载。 */ () => import('./components/ItemList'));
// Settings 是按需加载的系统设置页面,仅在管理员访问时加载。
const Settings = lazy(/* Settings 页面按路由激活时加载。 */ () => import('./components/Settings'));
// Rules 是按需加载的自动化规则页面,避免首屏载入规则编辑器代码。
const Rules = lazy(/* Rules 页面按路由激活时加载。 */ () => import('./components/Rules'));
// Notifications 是按需加载的通知页面,避免首屏载入通知配置代码。
const Notifications = lazy(/* Notifications 页面按路由激活时加载。 */ () => import('./components/Notifications'));
// Chat 是按需加载的聊天页面,避免未访问时载入聊天历史和 WebSocket 视图。
const Chat = lazy(/* Chat 页面按路由激活时加载。 */ () => import('./components/Chat'));
// PageLoading 展示路由页面代码加载期间的统一占位状态。
const PageLoading: React.FC = () => (
<div className="flex min-h-[24rem] items-center justify-center" role="status" aria-label="正在加载页面">
<Loader2 className="h-8 w-8 animate-spin text-brand" />
</div>
);
interface DeliveryRuleTarget {
// cookieId 表示cookieId。
cookieId: string;
// itemId 表示当前商品Id。
itemId: string;
// requestId 表示接口请求对象Id。
requestId: number;
}
import AuthenticatedShell, { type DeliveryRuleTarget } from './app/shell/AuthenticatedShell';
// 路由:URL path ↔ tab id。所有 SPA 路由统一挂 /app/ 前缀,避免和后端 API
// 路径(/orders、/cards、/items 等)冲突——后者在 chi 里先注册,刷新会直接
@@ -340,54 +305,35 @@ const App: React.FC = () => {
);
}
// Main App Layout
const renderContent = () => {
switch (activeTab) {
case 'dashboard': return <Dashboard />;
case 'accounts': return <AccountList />;
case 'chat': return <Chat />;
case 'orders': return <OrderList />;
case 'cards': return <CardList />;
case 'items': return <ItemList onConfigureDelivery={(item) => {
setDeliveryRuleTarget({ cookieId: item.cookie_id, itemId: item.item_id, requestId: Date.now() });
navigate('rules');
} /* 回调函数负责当前业务流程。 */} />;
case 'rules': return <Rules
initialDeliveryTarget={deliveryRuleTarget}
onDeliveryTargetHandled={() => setDeliveryRuleTarget(undefined) /* 回调函数负责当前业务流程。 */}
/>;
case 'notifications': return <Notifications isAdmin={isAdmin} />;
case 'settings': return isAdmin ? <Settings /> : <Dashboard />;
default: return <Dashboard />;
}
}; /* renderContent 表示renderContent。 */
// handleConfigureDelivery 保存商品页传来的规则目标并切换到规则页面。
const handleConfigureDelivery = (target /* target 表示商品页传来的规则配置目标。 */: DeliveryRuleTarget) => {
setDeliveryRuleTarget(target);
navigate('rules');
};
// handleDeliveryTargetHandled 清理已经被规则页面消费的联动目标。
const handleDeliveryTargetHandled = () => setDeliveryRuleTarget(undefined);
// handleToggleSidebar 切换侧边栏状态并持久化用户偏好。
const handleToggleSidebar = () => setSidebarCollapsed(/* current 表示更新前的侧边栏折叠状态。 */ current => {
// next 表示切换后的侧边栏折叠状态。
const next = !current;
writeSidebarCollapsed(next);
return next;
});
return (
<div className="flex min-h-screen bg-canvas text-ink">
<Sidebar
activeTab={activeTab}
isAdmin={isAdmin}
collapsed={sidebarCollapsed}
onToggleCollapsed={() => setSidebarCollapsed(current => {
const next = !current; /* next 表示next。 */
writeSidebarCollapsed(next);
return next;
} /* 回调函数负责当前业务流程。 */) /* 回调函数负责当前业务流程。 */}
onNavigate={navigate}
onLogout={handleLogout}
/>
<main className={`h-screen min-w-0 flex-1 overflow-x-hidden overflow-y-auto scroll-smooth transition-[margin] duration-300 ${sidebarCollapsed ? 'ml-16' : 'ml-64'} ${activeTab === 'chat' ? 'p-4 md:p-6' : 'p-8 md:p-12'}`}>
{/* Subtle background decoration */}
<div className="fixed top-0 right-0 w-[800px] h-[800px] bg-gradient-to-bl from-blue-50 to-transparent rounded-full blur-[120px] pointer-events-none -z-10 opacity-60"></div>
<div className={`${activeTab === 'chat' ? 'mx-auto max-w-[1680px]' : 'mx-auto max-w-[1400px] pb-10'}`}>
<Suspense fallback={<PageLoading />}>
{renderContent()}
</Suspense>
</div>
</main>
</div>
<AuthenticatedShell
activeTab={activeTab}
isAdmin={isAdmin}
collapsed={sidebarCollapsed}
deliveryRuleTarget={deliveryRuleTarget}
onToggleCollapsed={handleToggleSidebar}
onNavigate={navigate}
onLogout={handleLogout}
onConfigureDelivery={handleConfigureDelivery}
onDeliveryTargetHandled={handleDeliveryTargetHandled}
/>
);
}; /* App 表示App。 */
+159
View File
@@ -0,0 +1,159 @@
import React, { lazy, Suspense } from 'react';
import Sidebar from '../../components/Sidebar';
import type { Item } from '../../types';
// DeliveryRuleTarget 描述商品页跳转到自动化规则页时需要携带的目标信息。
export interface DeliveryRuleTarget {
// cookieId 表示目标闲鱼账号标识。
cookieId: string;
// itemId 表示目标商品标识。
itemId: string;
// requestId 用于区分连续发起的跳转请求。
requestId: number;
}
// AuthenticatedShellProps 描述认证后应用壳需要的导航、权限和联动回调。
export interface AuthenticatedShellProps {
// activeTab 表示当前正在展示的业务页面标识。
activeTab: string;
// isAdmin 表示当前会话是否拥有管理员权限。
isAdmin: boolean;
// collapsed 表示侧边栏是否处于折叠状态。
collapsed: boolean;
// deliveryRuleTarget 表示待传递给规则页的商品发货目标。
deliveryRuleTarget?: DeliveryRuleTarget;
// onToggleCollapsed 负责切换侧边栏折叠状态。
onToggleCollapsed: () => void;
// onNavigate 负责切换业务页面并同步外部路由。
onNavigate: (tab: string) => void;
// onLogout 负责注销当前会话并清理认证状态。
onLogout: () => void;
// onConfigureDelivery 负责接收商品页发起的规则配置目标。
onConfigureDelivery: (target: DeliveryRuleTarget) => void;
// onDeliveryTargetHandled 负责确认规则页已经消费跳转目标。
onDeliveryTargetHandled: () => void;
}
// Dashboard 是按需加载的仪表盘页面,避免首屏同步载入图表依赖。
const Dashboard = lazy(/* Dashboard 页面按路由激活时加载。 */ () => import('../../components/Dashboard'));
// AccountList 是按需加载的账号管理页面,避免未访问时载入账号弹窗和二维码代码。
const AccountList = lazy(/* AccountList 页面按路由激活时加载。 */ () => import('../../components/AccountList'));
// OrderList 是按需加载的订单页面,避免首屏载入订单导入与刷新代码。
const OrderList = lazy(/* OrderList 页面按路由激活时加载。 */ () => import('../../components/OrderList'));
// CardList 是按需加载的卡密页面,避免首屏载入卡密批量处理代码。
const CardList = lazy(/* CardList 页面按路由激活时加载。 */ () => import('../../components/CardList'));
// ItemList 是按需加载的商品页面,避免首屏载入商品发布编辑器代码。
const ItemList = lazy(/* ItemList 页面按路由激活时加载。 */ () => import('../../components/ItemList'));
// Settings 是按需加载的系统设置页面,仅在管理员访问时加载。
const Settings = lazy(/* Settings 页面按路由激活时加载。 */ () => import('../../components/Settings'));
// Rules 是按需加载的自动化规则页面,避免首屏载入规则编辑器代码。
const Rules = lazy(/* Rules 页面按路由激活时加载。 */ () => import('../../components/Rules'));
// Notifications 是按需加载的通知页面,避免首屏载入通知配置代码。
const Notifications = lazy(/* Notifications 页面按路由激活时加载。 */ () => import('../../components/Notifications'));
// Chat 是按需加载的聊天页面,避免未访问时载入聊天历史和 WebSocket 视图。
const Chat = lazy(/* Chat 页面按路由激活时加载。 */ () => import('../../components/Chat'));
// PageLoading 展示路由页面代码加载期间的统一占位状态。
const PageLoading: React.FC = () => (
<div className="flex min-h-[24rem] items-center justify-center" role="status" aria-label="正在加载页面">
<div className="h-8 w-8 animate-spin rounded-full border-4 border-brand/20 border-t-brand" />
</div>
);
// AppContentProps 描述页面组合器接收的路由和跨页面联动状态。
export interface AppContentProps {
// activeTab 表示当前需要渲染的业务页面标识。
activeTab: string;
// isAdmin 表示当前会话是否允许访问管理员页面。
isAdmin: boolean;
// deliveryRuleTarget 表示商品页传递给规则页的目标信息。
deliveryRuleTarget?: DeliveryRuleTarget;
// onConfigureDelivery 负责接收商品页面发起的规则配置目标。
onConfigureDelivery: (target: DeliveryRuleTarget) => void;
// onDeliveryTargetHandled 负责确认规则页已经消费跳转目标。
onDeliveryTargetHandled: () => void;
}
// AppContent 按当前导航标识选择业务页面,并隔离页面代码的动态加载边界。
export const AppContent: React.FC<AppContentProps> = ({
activeTab,
isAdmin,
deliveryRuleTarget,
onConfigureDelivery,
onDeliveryTargetHandled,
}) => {
// handleConfigureDelivery 将商品页面对象转换成路由壳使用的最小联动载荷。
const handleConfigureDelivery = (item /* item 表示用户选择的商品。 */: Item) => {
onConfigureDelivery({
cookieId: item.cookie_id,
itemId: item.item_id,
requestId: Date.now(),
});
};
// renderPage 根据当前页面标识选择唯一的业务页面组件。
const renderPage = () => {
switch (activeTab) {
case 'dashboard': return <Dashboard />;
case 'accounts': return <AccountList />;
case 'chat': return <Chat />;
case 'orders': return <OrderList />;
case 'cards': return <CardList />;
case 'items': return <ItemList onConfigureDelivery={handleConfigureDelivery} />;
case 'rules': return <Rules
initialDeliveryTarget={deliveryRuleTarget}
onDeliveryTargetHandled={onDeliveryTargetHandled}
/>;
case 'notifications': return <Notifications isAdmin={isAdmin} />;
case 'settings': return isAdmin ? <Settings /> : <Dashboard />;
default: return <Dashboard />;
}
};
return (
<Suspense fallback={<PageLoading />}>
{renderPage()}
</Suspense>
);
};
// AuthenticatedShell 组合认证后的侧边栏、主内容区域和页面动态加载边界。
const AuthenticatedShell: React.FC<AuthenticatedShellProps> = ({
activeTab,
isAdmin,
collapsed,
deliveryRuleTarget,
onToggleCollapsed,
onNavigate,
onLogout,
onConfigureDelivery,
onDeliveryTargetHandled,
}) => (
<div className="flex min-h-screen bg-canvas text-ink">
<Sidebar
activeTab={activeTab}
isAdmin={isAdmin}
collapsed={collapsed}
onToggleCollapsed={onToggleCollapsed}
onNavigate={onNavigate}
onLogout={onLogout}
/>
<main className={`h-screen min-w-0 flex-1 overflow-x-hidden overflow-y-auto scroll-smooth transition-[margin] duration-300 ${collapsed ? 'ml-16' : 'ml-64'} ${activeTab === 'chat' ? 'p-4 md:p-6' : 'p-8 md:p-12'}`}>
{/* 主内容区域的背景装饰不参与交互,避免遮挡业务页面。 */}
<div className="fixed top-0 right-0 -z-10 h-[800px] w-[800px] pointer-events-none rounded-full bg-gradient-to-bl from-blue-50 to-transparent opacity-60 blur-[120px]" />
<div className={`${activeTab === 'chat' ? 'mx-auto max-w-[1680px]' : 'mx-auto max-w-[1400px] pb-10'}`}>
<AppContent
activeTab={activeTab}
isAdmin={isAdmin}
deliveryRuleTarget={deliveryRuleTarget}
onConfigureDelivery={onConfigureDelivery}
onDeliveryTargetHandled={onDeliveryTargetHandled}
/>
</div>
</main>
</div>
);
export default AuthenticatedShell;
+16 -4
View File
@@ -18,7 +18,7 @@ const extractSingleQuotedValues = (source: string, pattern: RegExp) => {
describe('frontend navigation routing', () => {
test('sidebar entries and App activeTab routes stay in sync', () => {
const sidebar = readFrontendFile('components/Sidebar.tsx'); /* sidebar 表示sidebar。 */
const app = readFrontendFile('App.tsx'); /* app 表示app。 */
const app = readFrontendFile('app/shell/AuthenticatedShell.tsx'); /* app 表示认证后页面组合器源码。 */
const sidebarIDs = extractSingleQuotedValues(sidebar, /id:\s*'([^']+)'/g); /* sidebarIDs 表示sidebarIDs。 */
const appRouteIDs = extractSingleQuotedValues(app, /case\s+'([^']+)'/g); /* appRouteIDs 表示appRouteIDs。 */
@@ -34,13 +34,24 @@ describe('frontend navigation routing', () => {
} /* 回调函数负责当前业务流程。 */);
test('route pages are lazy-loaded behind a shared Suspense boundary', () => {
const app = readFrontendFile('App.tsx'); /* app 表示应用根组件源码。 */
const app = readFrontendFile('app/shell/AuthenticatedShell.tsx'); /* app 表示认证后页面组合器源码。 */
const lazyPageCount = (app.match(/const (Dashboard|AccountList|OrderList|CardList|ItemList|Settings|Rules|Notifications|Chat) = lazy\(/g) || []).length; /* lazyPageCount 表示按路由懒加载的页面数量。 */
expect(lazyPageCount).toBe(9);
expect(app).toContain('const PageLoading: React.FC');
expect(app).toContain('<Suspense fallback={<PageLoading />}>');
expect(app).not.toContain("import Dashboard from './components/Dashboard'");
expect(app).not.toContain("import Dashboard from '../../components/Dashboard'");
} /* 回调函数负责当前业务流程。 */);
test('authenticated shell owns sidebar and page composition', () => {
const app = readFrontendFile('App.tsx'); /* app 表示认证状态根组件源码。 */
const shell = readFrontendFile('app/shell/AuthenticatedShell.tsx'); /* shell 表示认证后应用壳源码。 */
expect(app).toContain("import AuthenticatedShell, { type DeliveryRuleTarget }");
expect(app).not.toContain('const renderContent = () =>');
expect(shell).toContain('const AuthenticatedShell: React.FC<AuthenticatedShellProps>');
expect(shell).toContain('<Sidebar');
expect(shell).toContain('<AppContent');
} /* 回调函数负责当前业务流程。 */);
test('logout button invalidates the backend session before clearing UI state', () => {
@@ -66,13 +77,14 @@ describe('frontend navigation routing', () => {
test('admin-only settings navigation is gated by session role', () => {
const app = readFrontendFile('App.tsx'); /* app 表示app。 */
const shell = readFrontendFile('app/shell/AuthenticatedShell.tsx'); /* shell 表示认证后应用壳源码。 */
const sidebar = readFrontendFile('components/Sidebar.tsx'); /* sidebar 表示sidebar。 */
const settingsHook = readFrontendFile('app/features/settings/hooks.ts'); /* settingsHook 表示settingsHook。 */
expect(app).toContain('const [isAdmin, setIsAdmin] = useState(false)');
expect(app).toContain('setIsAdmin(res.is_admin === true)');
expect(app).toContain("activeTab === 'settings'");
expect(app).toContain('isAdmin ? <Settings /> : <Dashboard />');
expect(shell).toContain('isAdmin ? <Settings /> : <Dashboard />');
expect(sidebar).toContain('isAdmin = false');
expect(sidebar).toContain("...(isAdmin ? [{ id: 'settings'");
expect(settingsHook).toContain('setLoadError');
+1 -1
View File
@@ -6,7 +6,7 @@ const source = (path: string) => readFileSync(resolve(__dirname, path), 'utf8');
describe('responsive rules layout', () => {
test('allows the rules page to shrink inside the sidebar layout', () => {
const app = source('App.tsx'); /* app 表示app。 */
const app = source('app/shell/AuthenticatedShell.tsx'); /* app 表示认证后应用壳源码。 */
const rules = source('components/Rules.tsx'); /* rules 表示规则集合。 */
expect(app).toContain('h-screen min-w-0 flex-1 overflow-x-hidden');
expect(rules).toContain('min-w-0 space-y-8');