App.tsx 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { useEffect } from 'react'
  2. import { Navigate, Outlet } from 'react-router-dom'
  3. import type { RouteObject } from 'react-router-dom'
  4. import { useAuthStore } from '@/stores/authStore'
  5. import Login from '@/pages/Login'
  6. import { AppShell } from '@/components/AppShell'
  7. import Dashboard from '@/pages/Dashboard'
  8. import Collections from '@/pages/Collections'
  9. import Documents from '@/pages/Documents'
  10. import Functions from '@/pages/Functions'
  11. import RagTester from '@/pages/RagTester'
  12. import Settings from '@/pages/Settings'
  13. import ProjectsAdmin from '@/pages/ProjectsAdmin'
  14. import KeysAdmin from '@/pages/KeysAdmin'
  15. import RelationsAdmin from '@/pages/RelationsAdmin'
  16. /// Shown while the server is being asked whether the restored session is real.
  17. /// Rendering the shell here is what used to flash the authenticated UI at a
  18. /// caller who turned out to be logged out.
  19. function AuthSplash() {
  20. return (
  21. <div className="min-h-screen flex items-center justify-center bg-[#0b0f17]">
  22. <div className="flex flex-col items-center gap-3">
  23. <div className="h-6 w-6 animate-spin rounded-full border-2 border-slate-600 border-t-indigo-500" />
  24. <p className="text-sm text-slate-500">Restoring session...</p>
  25. </div>
  26. </div>
  27. )
  28. }
  29. /// Runs once per page load: if the browser restored a session, confirm it with
  30. /// the server before anything downstream renders.
  31. function AuthBoot() {
  32. const status = useAuthStore((s) => s.status)
  33. const checkSession = useAuthStore((s) => s.checkSession)
  34. useEffect(() => {
  35. if (status === 'checking') void checkSession()
  36. // Deliberately once per mount: later status changes come from login/logout.
  37. // eslint-disable-next-line react-hooks/exhaustive-deps
  38. }, [])
  39. return <Outlet />
  40. }
  41. function ProtectedRoute() {
  42. const status = useAuthStore((s) => s.status)
  43. if (status === 'checking') return <AuthSplash />
  44. if (status !== 'authed') return <Navigate to="/login" replace />
  45. return <AppShell />
  46. }
  47. function AdminRoute() {
  48. const admin = useAuthStore((s) => s.admin)
  49. if (!admin) return <Navigate to="/" replace />
  50. return <Outlet />
  51. }
  52. export const routes: RouteObject[] = [
  53. {
  54. element: <AuthBoot />,
  55. children: [
  56. { path: '/login', element: <Login /> },
  57. {
  58. element: <ProtectedRoute />,
  59. children: [
  60. { index: true, element: <Dashboard /> },
  61. { path: 'collections', element: <Collections /> },
  62. { path: 'documents', element: <Documents /> },
  63. { path: 'rag', element: <RagTester /> },
  64. // Not admin-gated: a project key manages that project's functions,
  65. // and the page itself renders the policy editor read-only unless the
  66. // session is an admin one (the policy route is the admin-only part).
  67. { path: 'functions', element: <Functions /> },
  68. // Settings is not admin-gated: a project manager reaches the per-project
  69. // embedding tab here. The page renders only the tabs the key may use.
  70. { path: 'settings', element: <Settings /> },
  71. {
  72. element: <AdminRoute />,
  73. children: [
  74. { path: 'projects', element: <ProjectsAdmin /> },
  75. { path: 'keys', element: <KeysAdmin /> },
  76. { path: 'relations', element: <RelationsAdmin /> },
  77. ],
  78. },
  79. ],
  80. },
  81. ],
  82. },
  83. ]