import { ApiError } from '@/shared/api/client' import { Failure } from '@/shared/ui/route-feedback' import { Spinner } from '@/shared/ui/spinner' import { configureFormatting, configureTimeZone } from '@/shared/lib/format' import { BrandMark } from '@/shared/brand/brand-mark' import { useTranslation } from 'react-i18next' import { Suspense, useCallback, useEffect, useMemo, useRef, useState, } from 'react' import { MutationCache, QueryCache, QueryClient, QueryClientProvider, useQuery, } from '@tanstack/react-query' import { RouterProvider } from '@tanstack/react-router' import { createGrantAdapter, sessionSchema, type Session, } from '@/shared/auth/model' import { ServicesContext, type Services } from '@/shared/api/services' import { SignedOutApp, signOutRequest } from '@/modules/account/public' import { ThemeProvider } from '@/shared/theme/theme-provider' import { usePreferences } from '@/shared/preferences/store' import { reportError } from '@/shared/observability/report' import { createRuntime, permissionCatalog, type Runtime } from './runtime' import { appConfig } from './config' import { router } from './router' import { ProgressBar } from './route-progress' import { modules } from './modules' import { invalidationsFor } from '@/shared/realtime/events' import { createUploader } from '@/shared/uploads/model' import { withBase } from '@/shared/routing/base' function SessionScope({ runtime, session, refreshSession, signOut, switchDemo, }: { runtime: Runtime session: Session refreshSession: () => Promise signOut: () => Promise switchDemo: ( kind: 'persona' | 'workspace' | 'plan' | 'flag', value: string, ) => void }) { // Business dates follow the workspace's time zone; set before children render. useState(() => configureTimeZone(session.workspace.timeZone)) // A 401 from any request means the backend session ended: reload the session so // bootstrap shows sign-in and returns the user here afterwards. const refreshRef = useRef(refreshSession) useEffect(() => { refreshRef.current = refreshSession }, [refreshSession]) const [client] = useState(() => { const onError = (source: string) => (error: unknown) => { if (error instanceof ApiError && error.status === 401) void refreshRef.current() else reportError(error, { source }) } return new QueryClient({ queryCache: new QueryCache({ onError: onError('query') }), mutationCache: new MutationCache({ onError: onError('mutation') }), defaultOptions: { queries: { staleTime: 30_000, retry: false }, mutations: { retry: false }, }, }) }) useEffect( () => () => { void client.cancelQueries() client.clear() }, [client], ) // Live updates: a change event refreshes the queries of subscribing modules. useEffect( () => runtime.events?.((event) => { for (const queryKey of invalidationsFor(modules, event)) void client.invalidateQueries({ queryKey }) }), [runtime, client], ) // Route loaders read the same services from the router context. const services = useMemo( () => ({ api: runtime.api, session, access: createGrantAdapter(session, permissionCatalog), catalog: permissionCatalog, loadPermissionLabels: () => Promise.all(modules.map((module) => module.permissionLabels?.())).then(() => undefined), mode: runtime.mode, aiEnabled: runtime.aiEnabled, refreshSession, signOut, upload: createUploader( runtime.api, runtime.mode === 'demo' ? runtime.demo.putFile : undefined, ), demo: runtime.mode === 'demo' ? { plan: runtime.demo.plan, setPlan: (value) => switchDemo('plan', value), requestsEnabled: runtime.demo.requestsEnabled, setRequestsEnabled: (value) => switchDemo('flag', String(value)), persona: runtime.demo.persona, workspace: runtime.demo.workspace, setPersona: (value) => switchDemo('persona', value), setWorkspace: (value) => switchDemo('workspace', value), } : undefined, }), [runtime, session, refreshSession, signOut, switchDemo], ) return ( ) } function SessionLoader({ runtime }: { runtime: Runtime }) { const { t } = useTranslation() const [switching, setSwitching] = useState(false) // Set by an explicit sign-out, so sign-in says "signed out" rather than "expired". const [signedOut, setSignedOut] = useState(false) // Each sign-in starts a fresh session scope (query cache, drafts, router state). const [generation, setGeneration] = useState(0) const query = useQuery({ queryKey: ['session'], queryFn: ({ signal }) => runtime.api.request('/session', sessionSchema, { signal }), retry: false, staleTime: 30_000, }) const { refetch } = query const refreshSession = useCallback(() => refetch(), [refetch]) // Other tabs of this app follow sign-in and sign-out immediately. const channel = useRef(null) useEffect(() => { if (typeof BroadcastChannel === 'undefined') return const current = new BroadcastChannel(`${appConfig.id}:session`) current.onmessage = (event: MessageEvent<'signed-in' | 'signed-out'>) => { // A signed-out tab unmounts its scope, so signing in again starts fresh. if (event.data === 'signed-out') setSignedOut(true) void refetch() } channel.current = current return () => current.close() }, [refetch]) const signOut = useCallback(async () => { await signOutRequest(runtime.api) setSignedOut(true) channel.current?.postMessage('signed-out') await refetch() }, [runtime, refetch]) async function switchDemo( kind: 'persona' | 'workspace' | 'plan' | 'flag', value: string, ) { if (runtime.mode !== 'demo') return setSwitching(true) if (kind === 'persona') runtime.demo.setPersona(value) else if (kind === 'workspace') runtime.demo.setWorkspace(value) else if (kind === 'plan') runtime.demo.setPlan(value) else runtime.demo.setRequestsEnabled(value === 'true') await query.refetch() setSwitching(false) } if (query.isPending) return const status = query.error instanceof ApiError ? query.error.status : 0 if (status === 401) { const onSignedIn = async (returnTo: string) => { router.history.replace(withBase(returnTo)) setSignedOut(false) setGeneration((value) => value + 1) await refetch() channel.current?.postMessage('signed-in') } return ( }> { await runtime.api.response('/auth/sso/demo', { method: 'POST', }) await onSignedIn(returnTo) } : async (returnTo) => { // Full-page redirect to the backend, which returns to `returnTo`. window.location.assign( `${runtime.api.baseUrl.replace(/\/$/, '')}/auth/sso/start?${new URLSearchParams({ returnTo })}`, ) await new Promise(() => undefined) } } onSignedIn={onSignedIn} /> ) } return ( <> {query.isError && (

{t('unavailable')}

void query.refetch()} />
)} {switching && (
)} {/* 403 removes the old workspace; transient failures keep it hidden with drafts. */} {query.data && status !== 403 && ( )} ) } export function Bootstrap() { const { t } = useTranslation() const [state, setState] = useState<{ runtime?: Runtime; error?: string }>({}) const [client] = useState(() => new QueryClient()) useEffect(() => { configureFormatting(appConfig.currency) usePreferences.getState().configure(appConfig.id, appConfig.defaults) let active = true void createRuntime().then( (runtime) => { if (active) setState({ runtime }) }, (error) => { if (active) setState({ error: error instanceof Error ? error.message : 'Configuration failed.', }) }, ) return () => { active = false } }, []) if (state.error) return (

{t('configure')}

{t('configureHint')}

          {state.error}
        
) if (!state.runtime) return return ( ) } /** Branded startup state shown while runtime configuration and the session load. */ function Splash() { const { t } = useTranslation() return (
) }