72 lines
3.0 KiB
TypeScript
72 lines
3.0 KiB
TypeScript
import { useRef } from "react";
|
|
import { Links, Outlet, Scripts, ScrollRestoration } from "react-router";
|
|
import { useTranslation } from "react-i18next";
|
|
import { useChangeLanguage } from "remix-i18next/react";
|
|
|
|
import { defaultFavicon32Href } from "./assets/brand/default/public-icons";
|
|
import { Toaster as SonnerToaster } from "./components/ui/sonner";
|
|
import { RouteTracingProvider } from "./lib/sentry/routeTracing";
|
|
import { isPortalRootData, type PortalRootData } from "./services/customer-portal/portal-root";
|
|
import type { AppRootData } from "./utils/data/useRootData";
|
|
import { getCurrentDocumentCspNonce } from "./utils/security/documentCspNonce";
|
|
import { HiveGridMeta } from "./utils/seo/HiveGridMeta";
|
|
|
|
export const rootI18nNamespaces = ["translations"] as const;
|
|
|
|
export function RootSurfaceApp({ data }: { data: AppRootData | PortalRootData }) {
|
|
if (!isPortalRootData(data)) throw new Error("Portal build received Core root data");
|
|
useChangeLanguage(data.locale);
|
|
return (
|
|
<PortalDocument data={data}>
|
|
<RouteTracingProvider>
|
|
<Outlet />
|
|
</RouteTracingProvider>
|
|
<SonnerToaster />
|
|
</PortalDocument>
|
|
);
|
|
}
|
|
|
|
export function RootSurfaceErrorBoundary({ rootData }: { rootData?: AppRootData | PortalRootData }) {
|
|
const { t } = useTranslation();
|
|
return (
|
|
<PortalDocument data={isPortalRootData(rootData) ? rootData : undefined}>
|
|
<main className="mx-auto flex min-h-screen max-w-xl flex-col items-center justify-center gap-3 p-8 text-center">
|
|
<h1 className="text-heading-2 font-heading-2 text-text-neutral-primary">{t("customerPortal.states.error.title")}</h1>
|
|
<p className="text-body text-text-neutral-secondary">{t("customerPortal.states.error.body")}</p>
|
|
</main>
|
|
</PortalDocument>
|
|
);
|
|
}
|
|
|
|
function PortalDocument({ children, data }: { children: React.ReactNode; data?: Pick<PortalRootData, "locale" | "csrf" | "cspNonce"> }) {
|
|
const cspNonce = useStableDocumentCspNonce(data?.cspNonce);
|
|
const { i18n } = useTranslation();
|
|
|
|
return (
|
|
<html lang={data?.locale ?? "de"} dir={i18n.dir()}>
|
|
<head>
|
|
<HiveGridMeta />
|
|
<link rel="preload" href="/fonts/instrument-sans-latin.woff2" as="font" type="font/woff2" crossOrigin="anonymous" />
|
|
<meta name="theme-color" content="#111827" />
|
|
{data?.csrf && <meta name="csrf-token" content={data.csrf} />}
|
|
<link rel="icon" type="image/png" sizes="32x32" href={defaultFavicon32Href} />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<Links />
|
|
</head>
|
|
<body className="bg-background font-sans">
|
|
{children}
|
|
<ScrollRestoration nonce={cspNonce} />
|
|
<Scripts nonce={cspNonce} />
|
|
</body>
|
|
</html>
|
|
);
|
|
}
|
|
|
|
function useStableDocumentCspNonce(loaderNonce?: string): string | undefined {
|
|
const nonceRef = useRef<string | undefined>(undefined);
|
|
if (!nonceRef.current) {
|
|
nonceRef.current = typeof document === "undefined" ? loaderNonce : (getCurrentDocumentCspNonce(document) ?? loaderNonce);
|
|
}
|
|
return nonceRef.current;
|
|
}
|