| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- 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'
- /// 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 /> },
- // 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 /> },
- { path: 'relations', element: <RelationsAdmin /> },
- ],
- },
- ],
- },
- ],
- },
- ]
|