import * as React from "react"; import * as ErrorBoundaryModule from "../components/error-boundary/ErrorBoundary"; import * as LoadingFallbackModule from "../components/loading/LoadingFallback"; import * as PageMetaHookModule from "../hooks/usePageMeta"; import * as ReducedMotionModule from "../hooks/useReducedMotion"; import * as ShellModule from "../layouts/ProductionAppShell"; import { applyReducedMotionToDocument, readMotionPreference, resolveReducedMotion, subscribeMotionPreference, } from "../utils/motionPreference"; export interface IntegratedPageMeta { title: string; description?: string; image?: string; url?: string; type?: "website" | "article"; canonical?: string; noIndex?: boolean; } export interface IntegratedShellProps { children: React.ReactNode; activePath?: string; toolbarSlot?: React.ReactNode; onNavigate?: (href: string) => void; onGlobalSearchSubmit?: (query: string) => void; } export interface IntegratedLoadingFallbackProps { label?: string; detail?: string; className?: string; } export interface IntegratedErrorBoundaryProps { children: React.ReactNode; fallback?: React.ReactNode | ((error: Error, reset: () => void) => React.ReactNode); name?: string; resetKeys?: readonly unknown[]; } function pickExport(moduleValue: unknown, exportNames: readonly string[]): TValue | null { const record = moduleValue as Record; for (const exportName of exportNames) { const candidate = record[exportName]; if (candidate) { return candidate as TValue; } } return null; } function arraysShallowEqual(left: readonly unknown[] = [], right: readonly unknown[] = []): boolean { return left.length === right.length && left.every((value, index) => Object.is(value, right[index])); } class LocalErrorBoundary extends React.Component< IntegratedErrorBoundaryProps, { error: Error | null } > { public state = { error: null }; public static getDerivedStateFromError(error: Error): { error: Error } { return { error }; } public componentDidCatch(error: Error, info: React.ErrorInfo): void { if (import.meta.env.DEV) { console.error(`[Mechanica] ${this.props.name ?? "view"} crashed`, error, info); } } public componentDidUpdate(previousProps: IntegratedErrorBoundaryProps): void { if ( this.state.error && !arraysShallowEqual(previousProps.resetKeys, this.props.resetKeys) ) { this.reset(); } } private reset = (): void => { this.setState({ error: null }); }; public render(): React.ReactNode { if (!this.state.error) { return this.props.children; } if (typeof this.props.fallback === "function") { return this.props.fallback(this.state.error, this.reset); } if (this.props.fallback) { return this.props.fallback; } return (

Rendering interrupted

This Mechanica view crashed.

The scene boundary contained the failure so you can keep browsing. Try resetting this view, opening another machine, or reloading the page.

); } } const ExternalErrorBoundary = pickExport>>( ErrorBoundaryModule, ["ErrorBoundary", "default"], ); export function IntegratedErrorBoundary(props: IntegratedErrorBoundaryProps): JSX.Element { if (ExternalErrorBoundary) { return )} />; } return ; } const ExternalLoadingFallback = pickExport>>( LoadingFallbackModule, ["LoadingFallback", "default"], ); export function IntegratedLoadingFallback({ label = "Loading Mechanica", detail = "Preparing production assets, controls, and scene state…", className = "", }: IntegratedLoadingFallbackProps): JSX.Element { if (ExternalLoadingFallback) { return ( )} /> ); } return (

{label}

{detail}

); } function setNamedMeta(name: string, content: string): void { let element = document.querySelector(`meta[name="${name}"]`); if (!element) { element = document.createElement("meta"); element.name = name; document.head.appendChild(element); } element.content = content; } function setPropertyMeta(property: string, content: string): void { let element = document.querySelector(`meta[property="${property}"]`); if (!element) { element = document.createElement("meta"); element.setAttribute("property", property); document.head.appendChild(element); } element.content = content; } function setCanonical(url: string | undefined): void { let link = document.querySelector('link[rel="canonical"]'); if (!url) { link?.remove(); return; } if (!link) { link = document.createElement("link"); link.rel = "canonical"; document.head.appendChild(link); } link.href = url; } function applyFallbackPageMeta(meta: IntegratedPageMeta): void { if (typeof document === "undefined") { return; } const description = meta.description ?? "Explore mechanical systems through interactive 3D visualizations, procedural animations, and engineering annotations."; const image = meta.image ?? "/social/mechanica-og.svg"; const pageUrl = meta.url ?? (typeof window !== "undefined" ? window.location.href : "https://mechanica.local"); document.title = meta.title; setNamedMeta("description", description); setPropertyMeta("og:title", meta.title); setPropertyMeta("og:description", description); setPropertyMeta("og:type", meta.type ?? "website"); setPropertyMeta("og:image", image); setPropertyMeta("og:url", pageUrl); setNamedMeta("twitter:card", "summary_large_image"); setNamedMeta("twitter:title", meta.title); setNamedMeta("twitter:description", description); setNamedMeta("twitter:image", image); setNamedMeta("robots", meta.noIndex ? "noindex,nofollow" : "index,follow"); setCanonical(meta.canonical ?? pageUrl); } const externalUsePageMeta = pickExport<(meta: IntegratedPageMeta) => void>(PageMetaHookModule, [ "usePageMeta", "default", ]); export function useIntegratedPageMeta(meta: IntegratedPageMeta): void { if (externalUsePageMeta) { externalUsePageMeta(meta); return; } React.useEffect(() => { applyFallbackPageMeta(meta); }, [ meta.title, meta.description, meta.image, meta.url, meta.type, meta.canonical, meta.noIndex, ]); } const ExternalShell = pickExport>>(ShellModule, [ "ProductionAppShell", "default", ]); function LocalShell({ children, activePath, toolbarSlot, onNavigate, onGlobalSearchSubmit, }: IntegratedShellProps): JSX.Element { const [query, setQuery] = React.useState(""); const submitSearch = (event: React.FormEvent) => { event.preventDefault(); onGlobalSearchSubmit?.(query); }; const go = (href: string) => (event: React.MouseEvent) => { if (!onNavigate) { return; } event.preventDefault(); onNavigate(href); }; return (
Skip to content
M Mechanica Mechanical Systems Explorer
setQuery(event.currentTarget.value)} />
{toolbarSlot}
{children}
); } export function IntegratedProductionAppShell(props: IntegratedShellProps): JSX.Element { if (ExternalShell) { return )} />; } return ; } const ExternalReducedMotionProvider = pickExport>>( ReducedMotionModule, ["ReducedMotionProvider", "MotionPreferenceProvider", "default"], ); const externalUseReducedMotion = pickExport<() => unknown>(ReducedMotionModule, [ "useReducedMotion", "usePrefersReducedMotion", ]); function normalizeReducedMotionHookValue(value: unknown): boolean { if (typeof value === "boolean") { return value; } if (value && typeof value === "object" && "reducedMotion" in value) { return Boolean((value as { reducedMotion: unknown }).reducedMotion); } if (value && typeof value === "object" && "prefersReducedMotion" in value) { return Boolean((value as { prefersReducedMotion: unknown }).prefersReducedMotion); } return false; } function useLocalReducedMotion(): boolean { const subscribe = React.useCallback((listener: () => void) => subscribeMotionPreference(listener), []); const getSnapshot = React.useCallback(() => resolveReducedMotion(readMotionPreference()), []); const reducedMotion = React.useSyncExternalStore(subscribe, getSnapshot, getSnapshot); React.useEffect(() => { applyReducedMotionToDocument(reducedMotion); }, [reducedMotion]); return reducedMotion; } export function useIntegratedReducedMotion(): boolean { const localReducedMotion = useLocalReducedMotion(); const externalReducedMotion = externalUseReducedMotion ? normalizeReducedMotionHookValue(externalUseReducedMotion()) : false; return localReducedMotion || externalReducedMotion; } export function IntegratedReducedMotionProvider({ children, }: { children: React.ReactNode; }): JSX.Element { if (ExternalReducedMotionProvider) { return ( )} /> ); } return <>{children}; }