| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- import { useEffect } from 'react'
- import { Navigate, Outlet } from 'react-router-dom'
- import type { RouteObject } from 'react-router-dom'
- import { useAuthStore } from '@/stores/authStore'
- import Login from '@/pages/Login'
- import { AppShell } from '@/components/AppShell'
- import Dashboard from '@/pages/Dashboard'
- import Collections from '@/pages/Collections'
- import Documents from '@/pages/Documents'
- import Functions from '@/pages/Functions'
- import RagTester from '@/pages/RagTester'
- import Settings from '@/pages/Settings'
- import ProjectsAdmin from '@/pages/ProjectsAdmin'
- import KeysAdmin from '@/pages/KeysAdmin'
- import RelationsAdmin from '@/pages/RelationsAdmin'
- import ProjectFiles from '@/pages/ProjectFiles'
- import ProjectSecrets from '@/pages/ProjectSecrets'
- /// Shown while the server is being asked whether the restored session is real.
- /// Rendering the shell here is what used to flash the authenticated UI at a
- /// caller who turned out to be logged out.
- function AuthSplash() {
- return (
- <div className="min-h-screen flex items-center justify-center bg-[#0b0f17]">
- <div className="flex flex-col items-center gap-3">
- <div className="h-6 w-6 animate-spin rounded-full border-2 border-slate-600 border-t-indigo-500" />
- <p className="text-sm text-slate-500">Restoring session...</p>
- </div>
- </div>
- )
- }
- /// Runs once per page load: if the browser restored a session, confirm it with
- /// the server before anything downstream renders.
- function AuthBoot() {
- const status = useAuthStore((s) => s.status)
- const checkSession = useAuthStore((s) => s.checkSession)
- useEffect(() => {
- if (status === 'checking') void checkSession()
- // Deliberately once per mount: later status changes come from login/logout.
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [])
- return <Outlet />
- }
- function ProtectedRoute() {
- const status = useAuthStore((s) => s.status)
- if (status === 'checking') return <AuthSplash />
- if (status !== 'authed') return <Navigate to="/login" replace />
- return <AppShell />
- }
- function AdminRoute() {
- const admin = useAuthStore((s) => s.admin)
- if (!admin) return <Navigate to="/" replace />
- return <Outlet />
- }
- export const routes: RouteObject[] = [
- {
- element: <AuthBoot />,
- children: [
- { path: '/login', element: <Login /> },
- {
- element: <ProtectedRoute />,
- children: [
- { index: true, element: <Dashboard /> },
- { path: 'collections', element: <Collections /> },
- { path: 'documents', element: <Documents /> },
- { path: 'rag', element: <RagTester /> },
- // Not admin-gated: a project key manages that project's functions,
- // and the page itself renders the policy editor read-only unless the
- // session is an admin one (the policy route is the admin-only part).
- { path: 'functions', element: <Functions /> },
- // Files and Secrets are deliberately NOT admin-gated. File READS
- // authorize through the `_files` scope rules and the secrets LISTING
- // is project-manage, so an admin-only route would hide both from
- // exactly the keys entitled to them. Each page degrades in place when
- // a WRITE is refused, which is the narrower and correct boundary.
- { path: 'files', element: <ProjectFiles /> },
- { path: 'secrets', element: <ProjectSecrets /> },
- // Settings is not admin-gated: a project manager reaches the per-project
- // embedding tab here. The page renders only the tabs the key may use.
- { path: 'settings', element: <Settings /> },
- {
- element: <AdminRoute />,
- children: [
- { path: 'projects', element: <ProjectsAdmin /> },
- { path: 'keys', element: <KeysAdmin /> },
- // RELATIONS IS GROUPED WITH THE PROJECT ITEMS IN THE NAV AND IS
- // STILL ADMIN-GATED HERE, AND THE TWO ARE NOT IN CONFLICT.
- // The route is project-scoped (`/api/v1/projects/{p}/relations`),
- // which is why it belongs beside Collections and Documents rather
- // than under Administration - it describes one project's data
- // model. But being project-scoped says WHERE a route acts, not
- // WHO may call it: every relations handler in
- // `src/handlers/relations.cpp` runs `requireAdmin(k)` before
- // `requireProjectAccess(k, project)`, the list and read routes
- // included. Dropping this gate on the strength of the URL shape
- // would put a non-admin on a page whose every call answers 403.
- { path: 'relations', element: <RelationsAdmin /> },
- ],
- },
- ],
- },
- ],
- },
- ]
|