253 lines
6.6 KiB
TypeScript
253 lines
6.6 KiB
TypeScript
import * as React from "react";
|
|
import {
|
|
data,
|
|
isRouteErrorResponse,
|
|
Links,
|
|
Meta,
|
|
Outlet,
|
|
Scripts,
|
|
ScrollRestoration,
|
|
useRouteLoaderData,
|
|
} from "react-router";
|
|
|
|
import { EMPTY_CONFIG } from "./context/config";
|
|
import type { UiConfig } from "./types/tensorzero";
|
|
import type { Route } from "./+types/root";
|
|
import "./tailwind.css";
|
|
import {
|
|
getConfig,
|
|
checkAutopilotAvailable,
|
|
} from "./utils/config/index.server";
|
|
import { AppSidebar } from "./components/layout/app.sidebar";
|
|
import {
|
|
ErrorAppShell,
|
|
ErrorContent,
|
|
ErrorDialog,
|
|
PageNotFound,
|
|
} from "./components/ui/error";
|
|
import {
|
|
InfraErrorType,
|
|
isInfraErrorData,
|
|
isAuthenticationError,
|
|
isGatewayConnectionError,
|
|
isClickHouseError,
|
|
classifyError,
|
|
getErrorLabel,
|
|
type ClassifiedError,
|
|
} from "./utils/tensorzero/errors";
|
|
import { ContentLayout } from "./components/layout/ContentLayout";
|
|
import { startPeriodicCleanup } from "./utils/evaluations.server";
|
|
import { AppProviders } from "./providers/app-providers";
|
|
import { EntitySheet } from "./components/entity-sheet/EntitySheet";
|
|
import { isReadOnlyMode, readOnlyMiddleware } from "./utils/read-only.server";
|
|
import {
|
|
loadFeatureFlags,
|
|
type FeatureFlags,
|
|
} from "./utils/feature_flags.server";
|
|
import {
|
|
apiKeyCookie,
|
|
getApiKeyFromRequest,
|
|
isSecureRequest,
|
|
runWithRequest,
|
|
} from "./utils/api-key-override.server";
|
|
|
|
export const links: Route.LinksFunction = () => [
|
|
{ rel: "preconnect", href: "https://fonts.googleapis.com" },
|
|
{
|
|
rel: "preconnect",
|
|
href: "https://fonts.gstatic.com",
|
|
crossOrigin: "anonymous",
|
|
},
|
|
{
|
|
rel: "stylesheet",
|
|
href: "https://fonts.googleapis.com/css2?family=Geist+Mono:wght@100..900&family=Geist:wght@100..900&display=swap",
|
|
},
|
|
{
|
|
rel: "icon",
|
|
type: "image/svg+xml",
|
|
href: "/favicon.svg",
|
|
},
|
|
];
|
|
|
|
const apiKeyMiddleware: Route.MiddlewareFunction = async (
|
|
{ request },
|
|
next,
|
|
) => {
|
|
return runWithRequest(request, next);
|
|
};
|
|
|
|
export const middleware: Route.MiddlewareFunction[] = [
|
|
apiKeyMiddleware,
|
|
readOnlyMiddleware,
|
|
];
|
|
|
|
interface LoaderData {
|
|
config: UiConfig;
|
|
isReadOnly: boolean;
|
|
autopilotAvailable: boolean;
|
|
featureFlags: FeatureFlags;
|
|
infraError: ClassifiedError | null;
|
|
}
|
|
|
|
export async function loader({ request }: Route.LoaderArgs) {
|
|
// Initialize evaluation cleanup when the app loads
|
|
startPeriodicCleanup();
|
|
const isReadOnly = isReadOnlyMode();
|
|
const featureFlags = loadFeatureFlags();
|
|
try {
|
|
// Fetch config and autopilot availability in parallel
|
|
const [config, autopilotAvailable] = await Promise.all([
|
|
getConfig(),
|
|
checkAutopilotAvailable(),
|
|
]);
|
|
return {
|
|
config,
|
|
isReadOnly,
|
|
autopilotAvailable,
|
|
featureFlags,
|
|
infraError: null,
|
|
};
|
|
} catch (e) {
|
|
// Graceful degradation for infrastructure errors:
|
|
// Return fallback state so UI renders with dismissible error dialog.
|
|
// Child routes will handle their own errors via their error boundaries.
|
|
if (isGatewayConnectionError(e)) {
|
|
return {
|
|
config: EMPTY_CONFIG,
|
|
isReadOnly,
|
|
autopilotAvailable: false,
|
|
featureFlags,
|
|
infraError: { type: InfraErrorType.GatewayUnavailable },
|
|
};
|
|
}
|
|
if (isAuthenticationError(e)) {
|
|
const loaderData: LoaderData = {
|
|
config: EMPTY_CONFIG,
|
|
isReadOnly,
|
|
autopilotAvailable: false,
|
|
featureFlags,
|
|
infraError: { type: InfraErrorType.GatewayAuthFailed },
|
|
};
|
|
// Clear stale cookie so the auth dialog starts fresh
|
|
if (await getApiKeyFromRequest(request)) {
|
|
return data(loaderData, {
|
|
headers: {
|
|
"Set-Cookie": await apiKeyCookie.serialize("", {
|
|
maxAge: 0,
|
|
secure: isSecureRequest(request),
|
|
}),
|
|
},
|
|
});
|
|
}
|
|
return loaderData;
|
|
}
|
|
if (isClickHouseError(e)) {
|
|
const message = e instanceof Error ? e.message : undefined;
|
|
return {
|
|
config: EMPTY_CONFIG,
|
|
isReadOnly,
|
|
autopilotAvailable: false,
|
|
featureFlags,
|
|
infraError: {
|
|
type: InfraErrorType.ClickHouseUnavailable,
|
|
message,
|
|
},
|
|
};
|
|
}
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
// Global Layout
|
|
export function Layout({ children }: { children: React.ReactNode }) {
|
|
return (
|
|
<html lang="en">
|
|
<head>
|
|
<meta charSet="utf-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
<Meta />
|
|
<Links />
|
|
</head>
|
|
<body>
|
|
{children}
|
|
<ScrollRestoration />
|
|
<Scripts />
|
|
</body>
|
|
</html>
|
|
);
|
|
}
|
|
|
|
export default function App({ loaderData }: Route.ComponentProps) {
|
|
const { infraError } = loaderData;
|
|
const [dialogOpen, setDialogOpen] = React.useState(true);
|
|
|
|
// Reset dialog when infraError changes (component re-renders, not remounts)
|
|
React.useEffect(() => {
|
|
if (infraError) {
|
|
setDialogOpen(true);
|
|
}
|
|
}, [infraError]);
|
|
|
|
return (
|
|
<AppProviders loaderData={loaderData}>
|
|
<div className="fixed inset-0 flex">
|
|
<AppSidebar />
|
|
<ContentLayout>
|
|
<Outlet />
|
|
</ContentLayout>
|
|
</div>
|
|
<EntitySheet />
|
|
{infraError && (
|
|
<ErrorDialog
|
|
open={dialogOpen}
|
|
onDismiss={() => setDialogOpen(false)}
|
|
onReopen={() => setDialogOpen(true)}
|
|
label={getErrorLabel(infraError.type)}
|
|
>
|
|
<ErrorContent error={infraError} />
|
|
</ErrorDialog>
|
|
)}
|
|
</AppProviders>
|
|
);
|
|
}
|
|
|
|
export function ErrorBoundary({ error }: Route.ErrorBoundaryProps) {
|
|
const [open, setOpen] = React.useState(true);
|
|
const rootLoaderData = useRouteLoaderData<typeof loader>("root");
|
|
|
|
// Reset dialog when error changes (component may re-render, not remount)
|
|
React.useEffect(() => {
|
|
setOpen(true);
|
|
}, [error]);
|
|
|
|
// Client 404s (page not found in React Router) - show in content area with sidebar
|
|
// Check that it's not an infrastructure error (those go through classifyError)
|
|
if (isRouteErrorResponse(error) && error.status === 404) {
|
|
if (!isInfraErrorData(error.data)) {
|
|
return (
|
|
<ErrorAppShell content={<PageNotFound />} loaderData={rootLoaderData} />
|
|
);
|
|
}
|
|
}
|
|
|
|
// All other errors use the dismissible modal pattern with sidebar visible
|
|
const classified = classifyError(error);
|
|
const label = getErrorLabel(classified.type);
|
|
|
|
return (
|
|
<ErrorAppShell
|
|
loaderData={rootLoaderData}
|
|
overlay={
|
|
<ErrorDialog
|
|
open={open}
|
|
onDismiss={() => setOpen(false)}
|
|
onReopen={() => setOpen(true)}
|
|
label={label}
|
|
>
|
|
<ErrorContent error={classified} />
|
|
</ErrorDialog>
|
|
}
|
|
/>
|
|
);
|
|
}
|