Files
atlas-seller-kit/apps/web/src/router/index.tsx
T
2026-09-03 16:06:13 +08:00

76 lines
2.7 KiB
TypeScript

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 <SessionSplash />;
}
if (status === 'anonymous') {
return <Navigate to="/login" replace state={{ from: location.pathname }} />;
}
return children;
}
/** 认证页互斥:已登录访问 /login、/register 时回到主布局 */
function RedirectIfAuthenticated({ children }: { children: ReactNode }) {
const { status } = useSession();
if (status === 'authenticated') {
return <Navigate to="/" replace />;
}
return children;
}
export const router = createBrowserRouter([
{
path: '/',
element: (
<RequireSession>
<MainLayout />
</RequireSession>
),
children: [
{ index: true, element: <Navigate to="/online-products" replace /> },
{ path: 'online-products', element: <OnlineProductsPage /> },
{ path: 'collection', element: <CollectionPage /> },
{ path: 'shops', element: <ShopsPage /> },
{ path: 'watermarks', element: <WatermarksPage /> },
{ path: 'pricing', element: <PricingPage /> },
// 商品 AI 编辑页:从采集箱/在线商品进入,不在左侧菜单中出现
{ path: 'products/:id', element: <ProductEditPage /> },
],
},
{
path: 'login',
element: (
<RedirectIfAuthenticated>
<LoginPage />
</RedirectIfAuthenticated>
),
},
{
path: 'register',
element: (
<RedirectIfAuthenticated>
<RegisterPage />
</RedirectIfAuthenticated>
),
},
{ path: '*', element: <Navigate to="/" replace /> },
]);