import { StrictMode, useEffect } from "react";
import { createRoot } from "react-dom/client";
import "@/app/globals.css";
import { setOnUnauthorized } from "@/lib/api/client";
import { scheduleFrontendErrorReport } from "@/lib/api/domains/frontend-error-log-api";
import { setBackendReloadDiagnosticReporter } from "@/lib/platform/backend-reload-coordinator";
import { useAppStoreApi, StateProvider } from "@/components/state-provider";
import { PluginBootBridge } from "@/lib/plugins/plugin-boot-bridge";
import { preloadLocale } from "@/lib/i18n";
import { resolveInitialLocale } from "@/lib/i18n/boot";
import { AppShell } from "./app-shell";
import { AuthGatedScreen, useAuthGateDecision } from "./auth-gate";
import { loadBootPayload } from "./boot-payload";
import type { BootPayload } from "./boot-payload";
import { RootErrorBoundary, RouteErrorBoundary } from "./app-error-boundary";
import { SpaRoutes } from "./spa-routes";
import { installVitePreloadRecovery } from "./vite-preload-recovery";
import { installBfcacheRestoreReload } from "./bfcache-restore-reload";
import { markRenderingEngine } from "@/lib/browser/rendering-engine";
import { applyTitlePrefix } from "@/lib/browser/document-title";
installVitePreloadRecovery();
installBfcacheRestoreReload();
markRenderingEngine(document.documentElement);
setBackendReloadDiagnosticReporter((source) => {
scheduleFrontendErrorReport({ source: "backend-reload", title: source });
});
const AUTH_ROUTE_PATHS = new Set(["/login", "/setup", "/invite"]);
/** Registers the 401 handler once: clear the auth slice and bounce to
* /login, guarding against redirect loops when already on an auth page. */
function useUnauthorizedRedirect() {
const store = useAppStoreApi();
useEffect(() => {
setOnUnauthorized(() => {
store.getState().clearAuthenticated();
if (!AUTH_ROUTE_PATHS.has(window.location.pathname)) {
window.location.assign("/login");
}
});
return () => setOnUnauthorized(null);
}, [store]);
}
function AppBody({ payload }: { payload: BootPayload }) {
useUnauthorizedRedirect();
const decision = useAuthGateDecision();
if (decision !== "app") {
return