All files / web/src main.tsx

0% Statements 0/50
100% Branches 1/1
100% Functions 1/1
0% Lines 0/50

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79                                                                                                                                                             
/// <reference types="vite/client" />
 
import { createRouter, RouterProvider } from '@tanstack/react-router';
import { getAuthStatus } from '@web/api/v1/super-agents/auth';
import { Toaster } from '@web/components/ui/toaster';
import { SidebarProvider } from '@web/providers/side-bar';
import { ThemeProvider } from '@web/providers/theme';
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import { routeTree } from './routeTree.gen';
 
// Self-hosted so the dashboard renders correctly with no network access and
// makes no third-party request. Only the weights the app asks for are here:
// Lato 300/400/700/900 for body text (`font-medium` and `font-semibold` have
// no Lato cut, and match to 400 and 700 respectively), and Ubuntu 400/700 for
// the logo, whose text is `font-bold`.
import '@fontsource/lato/300.css';
import '@fontsource/lato/400.css';
import '@fontsource/lato/700.css';
import '@fontsource/lato/900.css';
// Latin only: the logo is the only thing set in Ubuntu, and it reads
// "Super Agents".
import '@fontsource/ubuntu/latin-400.css';
import '@fontsource/ubuntu/latin-700.css';
import '@web/styles/globals.css';
import '@web/styles/editor.css';
 
/**
 * App-level auth gate that runs before the router initializes.
 * Returns false if the user must log in — caller should NOT mount the app.
 */
async function checkAuthBeforeMount(): Promise<boolean> {
  if (window.location.pathname === '/login') return true;
 
  const data = await getAuthStatus();
  if (!data) return false;
 
  return !(data.authRequired && !data.authenticated);
}
 
const router = createRouter({
  routeTree,
});
 
declare module '@tanstack/react-router' {
  interface Register {
    router: typeof router;
  }
}
 
async function mount() {
  const allowed = await checkAuthBeforeMount();
  if (!allowed) {
    window.location.replace('/login');
    return;
  }
 
  const rootElement = document.getElementById('root');
  if (rootElement) {
    createRoot(rootElement).render(
      <StrictMode>
        <ThemeProvider
          attribute="class"
          defaultTheme="system"
          enableSystem
          disableTransitionOnChange
        >
          <SidebarProvider className="h-full">
            <RouterProvider router={router} />
          </SidebarProvider>
          <Toaster />
        </ThemeProvider>
      </StrictMode>,
    );
  }
}
 
mount();