App.tsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { lazy, Suspense } from 'react'
  2. import { Routes, Route, Navigate } from 'react-router-dom'
  3. import { Layout } from './components/layout/Layout'
  4. import { useAuth } from './contexts/AuthContext'
  5. const Dashboard = lazy(() => import('./pages/Dashboard').then(m => ({ default: m.Dashboard })))
  6. const Models = lazy(() => import('./pages/Models').then(m => ({ default: m.Models })))
  7. const Datasets = lazy(() => import('./pages/Datasets').then(m => ({ default: m.Datasets })))
  8. const Training = lazy(() => import('./pages/Training').then(m => ({ default: m.Training })))
  9. const Evaluation = lazy(() => import('./pages/Evaluation').then(m => ({ default: m.Evaluation })))
  10. const Deployment = lazy(() => import('./pages/Deployment').then(m => ({ default: m.Deployment })))
  11. const Inference = lazy(() => import('./pages/Inference').then(m => ({ default: m.Inference })))
  12. const Login = lazy(() => import('./pages/AuthLogin').then(m => ({ default: m.Login })))
  13. const AuthCallback = lazy(() => import('./pages/AuthCallback').then(m => ({ default: m.AuthCallback })))
  14. function PageFallback() {
  15. return <div style={{ padding: 24, color: '#94a3b8' }}>加载中...</div>
  16. }
  17. function AuthRoute({ children }: { children: React.ReactNode }) {
  18. const { isAuthenticated } = useAuth()
  19. return isAuthenticated ? <>{children}</> : <Navigate to="/login" replace />
  20. }
  21. export default function App() {
  22. return (
  23. <Routes>
  24. <Route path="/login" element={<Suspense fallback={<PageFallback />}><Login /></Suspense>} />
  25. <Route path="/auth/callback" element={<Suspense fallback={<PageFallback />}><AuthCallback /></Suspense>} />
  26. <Route path="/*" element={
  27. <AuthRoute>
  28. <Layout>
  29. <Suspense fallback={<PageFallback />}>
  30. <Routes>
  31. <Route path="/" element={<Dashboard />} />
  32. <Route path="/models" element={<Models />} />
  33. <Route path="/datasets" element={<Datasets />} />
  34. <Route path="/training" element={<Training />} />
  35. <Route path="/evaluation" element={<Evaluation />} />
  36. <Route path="/deployment" element={<Deployment />} />
  37. <Route path="/inference" element={<Inference />} />
  38. </Routes>
  39. </Suspense>
  40. </Layout>
  41. </AuthRoute>
  42. } />
  43. </Routes>
  44. )
  45. }