nex_animation/frontend/src/app/router.tsx

102 lines
4.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
* 路由表。
*
* 路由级懒加载:以前所有视图静态 import,打开「平台资产库」也要先把
* 分镜 / 资产 / 模型中心一整套模块下载并编译完(开发模式首屏 77 个请求 / 4.7MB,
* 其中 1.9MB 是这次根本用不到的页面)。现在只保留入口页(项目列表)同步加载,
* 其余按路由切分:点进哪个页面才拉哪个页面的 chunk,构建产物也跟着按路由分包。
*
* 鉴权守卫做在 App 的 <AuthGate> 里(会话状态在 zustand,组件订阅比 loader 更省事),
* 这里只声明每条路由的元信息 handle。
*/
import { lazy } from 'react'
import { createBrowserRouter, Navigate } from 'react-router-dom'
import App from './App'
import Dashboard from '../pages/Dashboard'
/** 侧栏里一定点得到的相邻页:首屏渲染完在空闲时提前拉好 */
const loadAssetLibrary = () => import('../pages/AssetLibrary')
const loadModelCenter = () => import('../pages/ModelCenter')
const loadProjectDetail = () => import('../pages/ProjectDetail')
const loadOverview = () => import('../pages/project/Overview')
const loadLibrary = () => import('../pages/project/Library')
const loadStoryboard = () => import('../pages/project/Storyboard')
const loadDispatch = () => import('../pages/project/Dispatch')
const loadReview = () => import('../pages/project/Review')
const loadTimeline = () => import('../pages/project/Timeline')
const loadDelivery = () => import('../pages/project/Delivery')
const loadLogin = () => import('../pages/Login')
const loadAccount = () => import('../pages/Account')
const loadUserAdmin = () => import('../pages/UserAdmin')
const AssetLibrary = lazy(loadAssetLibrary)
const ModelCenter = lazy(loadModelCenter)
const ProjectDetail = lazy(loadProjectDetail)
const Overview = lazy(loadOverview)
const Library = lazy(loadLibrary)
const Storyboard = lazy(loadStoryboard)
const Dispatch = lazy(loadDispatch)
const Review = lazy(loadReview)
const Timeline = lazy(loadTimeline)
const Delivery = lazy(loadDelivery)
const Login = lazy(loadLogin)
const Account = lazy(loadAccount)
const UserAdmin = lazy(loadUserAdmin)
/** 挂在每条路由上的元信息:App 用 useMatches() 读取 */
export type RouteHandle = { title?: string; public?: boolean; admin?: boolean }
const withHandle = (title: string, extra: RouteHandle = {}): RouteHandle => ({ title, ...extra })
export const router = createBrowserRouter([
{
path: '/',
element: <App />,
children: [
{ index: true, element: <Dashboard />, handle: withHandle('项目') },
{ path: 'assets', element: <AssetLibrary />, handle: withHandle('平台资产库') },
{ path: 'models', element: <ModelCenter />, handle: withHandle('模型管理中心') },
{
path: 'projects/:id',
element: <ProjectDetail />,
handle: withHandle('项目工作台'),
children: [
{ index: true, element: <Navigate to="overview" replace /> },
{ path: 'overview', element: <Overview />, handle: withHandle('流程概览') },
{ path: 'library', element: <Library />, handle: withHandle('资产管理') },
{ path: 'storyboard', element: <Storyboard />, handle: withHandle('分镜与脚本') },
{ path: 'dispatch', element: <Dispatch />, handle: withHandle('调度中心') },
{ path: 'review', element: <Review />, handle: withHandle('质检与审核') },
{ path: 'timeline', element: <Timeline />, handle: withHandle('时间线合成') },
{ path: 'delivery', element: <Delivery />, handle: withHandle('导出与成本') }
]
},
{ path: 'login', element: <Login />, handle: withHandle('登录', { public: true }) },
{ path: 'account', element: <Account />, handle: withHandle('我的账号') },
{ path: 'users', element: <UserAdmin />, handle: withHandle('用户管理', { admin: true }) },
{ path: '*', element: <Navigate to="/" replace /> }
]
}
])
/**
* 空闲预取:首屏渲染完成后,把「侧边栏里一定能点到」的相邻页面 chunk 提前拉好,
* 这样懒加载不会让用户在真正切换时等一次网络往返。
* 只在浏览器空闲时做,绝不和首屏的图片/接口抢带宽。
*/
let warmed = false
export function warmupRoutes() {
if (warmed) return
warmed = true
const run = () => {
loadAssetLibrary().catch(() => undefined)
loadModelCenter().catch(() => undefined)
loadProjectDetail().catch(() => undefined)
}
const w = window as any
if (typeof w.requestIdleCallback === 'function') w.requestIdleCallback(run, { timeout: 4000 })
else window.setTimeout(run, 1500)
}
export default router