App.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. import ProjectFiles from '@/pages/ProjectFiles'
  17. import ProjectSecrets from '@/pages/ProjectSecrets'
  18. /// Shown while the server is being asked whether the restored session is real.
  19. /// Rendering the shell here is what used to flash the authenticated UI at a
  20. /// caller who turned out to be logged out.
  21. function AuthSplash() {
  22. return (
  23. <div className="min-h-screen flex items-center justify-center bg-[#0b0f17]">
  24. <div className="flex flex-col items-center gap-3">
  25. <div className="h-6 w-6 animate-spin rounded-full border-2 border-slate-600 border-t-indigo-500" />
  26. <p className="text-sm text-slate-500">Restoring session...</p>
  27. </div>
  28. </div>
  29. )
  30. }
  31. /// Runs once per page load: if the browser restored a session, confirm it with
  32. /// the server before anything downstream renders.
  33. function AuthBoot() {
  34. const status = useAuthStore((s) => s.status)
  35. const checkSession = useAuthStore((s) => s.checkSession)
  36. useEffect(() => {
  37. if (status === 'checking') void checkSession()
  38. // Deliberately once per mount: later status changes come from login/logout.
  39. // eslint-disable-next-line react-hooks/exhaustive-deps
  40. }, [])
  41. return <Outlet />
  42. }
  43. function ProtectedRoute() {
  44. const status = useAuthStore((s) => s.status)
  45. if (status === 'checking') return <AuthSplash />
  46. if (status !== 'authed') return <Navigate to="/login" replace />
  47. return <AppShell />
  48. }
  49. function AdminRoute() {
  50. const admin = useAuthStore((s) => s.admin)
  51. if (!admin) return <Navigate to="/" replace />
  52. return <Outlet />
  53. }
  54. export const routes: RouteObject[] = [
  55. {
  56. element: <AuthBoot />,
  57. children: [
  58. { path: '/login', element: <Login /> },
  59. {
  60. element: <ProtectedRoute />,
  61. children: [
  62. { index: true, element: <Dashboard /> },
  63. { path: 'collections', element: <Collections /> },
  64. { path: 'documents', element: <Documents /> },
  65. { path: 'rag', element: <RagTester /> },
  66. // Not admin-gated: a project key manages that project's functions,
  67. // and the page itself renders the policy editor read-only unless the
  68. // session is an admin one (the policy route is the admin-only part).
  69. { path: 'functions', element: <Functions /> },
  70. // Files and Secrets are deliberately NOT admin-gated. File READS
  71. // authorize through the `_files` scope rules and the secrets LISTING
  72. // is project-manage, so an admin-only route would hide both from
  73. // exactly the keys entitled to them. Each page degrades in place when
  74. // a WRITE is refused, which is the narrower and correct boundary.
  75. { path: 'files', element: <ProjectFiles /> },
  76. { path: 'secrets', element: <ProjectSecrets /> },
  77. // Settings is not admin-gated: a project manager reaches the per-project
  78. // embedding tab here. The page renders only the tabs the key may use.
  79. { path: 'settings', element: <Settings /> },
  80. {
  81. element: <AdminRoute />,
  82. children: [
  83. { path: 'projects', element: <ProjectsAdmin /> },
  84. { path: 'keys', element: <KeysAdmin /> },
  85. // RELATIONS IS GROUPED WITH THE PROJECT ITEMS IN THE NAV AND IS
  86. // STILL ADMIN-GATED HERE, AND THE TWO ARE NOT IN CONFLICT.
  87. // The route is project-scoped (`/api/v1/projects/{p}/relations`),
  88. // which is why it belongs beside Collections and Documents rather
  89. // than under Administration - it describes one project's data
  90. // model. But being project-scoped says WHERE a route acts, not
  91. // WHO may call it: every relations handler in
  92. // `src/handlers/relations.cpp` runs `requireAdmin(k)` before
  93. // `requireProjectAccess(k, project)`, the list and read routes
  94. // included. Dropping this gate on the strength of the URL shape
  95. // would put a non-admin on a page whose every call answers 403.
  96. { path: 'relations', element: <RelationsAdmin /> },
  97. ],
  98. },
  99. ],
  100. },
  101. ],
  102. },
  103. ]