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