import type { ReactNode } from 'react'; import { Navigate, useLocation, createBrowserRouter } from 'react-router-dom'; import MainLayout from '@/layouts/MainLayout'; import SessionSplash from '@/components/SessionSplash'; import CollectionPage from '@/pages/collection/CollectionPage'; import LoginPage from '@/pages/auth/LoginPage'; import OnlineProductsPage from '@/pages/online-products/OnlineProductsPage'; import PricingPage from '@/pages/pricing/PricingPage'; import ProductEditPage from '@/pages/products/ProductEditPage'; import RegisterPage from '@/pages/auth/RegisterPage'; import ShopsPage from '@/pages/shops/ShopsPage'; import WatermarksPage from '@/pages/watermarks/WatermarksPage'; import { useSession } from '@/services/session'; /** 主布局守卫:会话恢复中显示启动屏;未认证跳登录页并记住来源路径 */ function RequireSession({ children }: { children: ReactNode }) { const { status } = useSession(); const location = useLocation(); if (status === 'restoring') { return ; } if (status === 'anonymous') { return ; } return children; } /** 认证页互斥:已登录访问 /login、/register 时回到主布局 */ function RedirectIfAuthenticated({ children }: { children: ReactNode }) { const { status } = useSession(); if (status === 'authenticated') { return ; } return children; } export const router = createBrowserRouter([ { path: '/', element: ( ), children: [ { index: true, element: }, { path: 'online-products', element: }, { path: 'collection', element: }, { path: 'shops', element: }, { path: 'watermarks', element: }, { path: 'pricing', element: }, // 商品 AI 编辑页:从采集箱/在线商品进入,不在左侧菜单中出现 { path: 'products/:id', element: }, ], }, { path: 'login', element: ( ), }, { path: 'register', element: ( ), }, { path: '*', element: }, ]);