Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

2.9 KB
Raw Blame
import type { QueryClient } from "@tanstack/react-query";
import { createRootRouteWithContext, Outlet, useRouterState } from "@tanstack/react-router";

import { NotificationContainer } from "@app/components/notifications";
import { TooltipProvider } from "@app/components/v3";
import {
  RootCommandMenu,
  type RootCommandMenuShell
} from "@app/components/v3/platform/RootCommandMenu";
import { ThemeProvider } from "@app/components/v3/platform/ThemeProvider";
import { adminQueryKeys, fetchServerConfig } from "@app/hooks/api/admin/queries";
import { TServerConfig } from "@app/hooks/api/admin/types";
import { authKeys } from "@app/hooks/api/auth/queries";
import { fetchAuthToken, shouldRetryAuthTokenFetch } from "@app/hooks/api/auth/refresh";
import { ProjectType } from "@app/hooks/api/projects/types";

type TRouterContext = {
  serverConfig: TServerConfig | null;
  queryClient: QueryClient;
  // Published by the Certificate Manager, Secret Scanning, PAM and Agent Vault layouts, whose URLs carry no $projectId.
  // Optional because only those branches of the tree set them; see useImplicitProjectId and
  // useImplicitProduct.
  implicitProjectId?: string;
  implicitProductType?: ProjectType;
};

const RootCommandMenuMount = () => {
  const shell = useRouterState({
    select: (state): RootCommandMenuShell | null => {
      const routeIds = new Set(state.matches.map((match) => match.routeId));

      if (routeIds.has("/_authenticate/_inject-org-details/admin/_admin-layout")) {
        return "admin";
      }
      if (routeIds.has("/_authenticate/_inject-org-details/_org-layout")) {
        return "organization";
      }
      if (routeIds.has("/_authenticate/personal-settings/_layout")) {
        return "personal-settings";
      }
      return null;
    }
  });

  if (!shell) return null;

  return <RootCommandMenu shell={shell} />;
};

const RootPage = () => {
  const pathname = useRouterState({ select: (state) => state.location.pathname });
  return (
    <ThemeProvider pathname={pathname}>
      <TooltipProvider delayDuration={700} skipDelayDuration={300}>
        <Outlet />
        <RootCommandMenuMount />
        <NotificationContainer />
      </TooltipProvider>
    </ThemeProvider>
  );
};

export const Route = createRootRouteWithContext<TRouterContext>()({
  component: RootPage,
  beforeLoad: async ({ context }) => {
    const authToken = await context.queryClient
      .fetchQuery({
        queryKey: authKeys.getAuthToken,
        queryFn: fetchAuthToken,
        staleTime: Infinity,
        retry: shouldRetryAuthTokenFetch
      })
      .catch(() => {
        // No valid refresh cookie — boot continues unauthenticated.
        // Downstream middlewares handle redirects for protected routes.
        return null;
      });

    const serverConfig = await context.queryClient.ensureQueryData({
      queryKey: adminQueryKeys.serverConfig(),
      queryFn: fetchServerConfig
    });
    return { authToken, serverConfig };
  }
});