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 (

Restoring session...

) } /// 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 } function ProtectedRoute() { const status = useAuthStore((s) => s.status) if (status === 'checking') return if (status !== 'authed') return return } function AdminRoute() { const admin = useAuthStore((s) => s.admin) if (!admin) return return } export const routes: RouteObject[] = [ { element: , children: [ { path: '/login', element: }, { element: , children: [ { index: true, element: }, { path: 'collections', element: }, { path: 'documents', element: }, { path: 'rag', element: }, // 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: }, // 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: }, { path: 'secrets', element: }, // 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: }, { element: , children: [ { path: 'projects', element: }, { path: 'keys', element: }, // 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: }, ], }, ], }, ], }, ]