// Optional surfaces the host turns on. The web app supplies its // bundler-specific implementations (PDF.js worker, editable-artifact kernels) // and its console-only account management as lazy components; a host that // supplies nothing gets a plain fallback. import { lazy, Suspense, useEffect, type ComponentType } from "react"; import { LoadingPanel } from "../components/common"; import { Link } from "./navigation"; import { useAppHost } from "./app-host"; export type PdfPreviewProps = { url: string; title: string }; export type EditableArtifactSurfaceProps = { workspaceId: string; artifactId: string; embedded?: boolean | undefined; }; export type CodexSubscriptionsSurfaceProps = { workspaceId: string; canManage: boolean }; export type AppSurfaces = { pdfPreview: ComponentType; editableArtifact: ComponentType; /** The workspace's ChatGPT subscription accounts, managed in the console. */ codexSubscriptions: ComponentType; }; type Preloadable = { preload?: () => void }; /** * A lazy surface that can start loading before it renders, so a host's * split chunk does not add a second loading flash behind the panel that * shows it (see `usePreloadAppSurface`). */ export function lazySurface

( load: () => Promise<{ default: ComponentType

}>, ): ComponentType

& Required { let pending: Promise<{ default: ComponentType

}> | null = null; const loadOnce = () => (pending ??= load()); const component = lazy(loadOnce) as unknown as ComponentType

& Required; component.preload = () => { void loadOnce().catch(() => { // Rendering retries the same import and reports its failure. pending = null; }); }; return component; } function useAppSurface(name: K): AppSurfaces[K] | undefined { return useAppHost().surfaces?.[name]; } /** Starts loading a registered lazy surface once `when` holds. */ export function usePreloadAppSurface(name: keyof AppSurfaces, when: boolean): void { const surface = useAppSurface(name) as (AppSurfaces[typeof name] & Preloadable) | undefined; useEffect(() => { if (when) surface?.preload?.(); }, [surface, when]); } export function PdfPreviewSurface(props: PdfPreviewProps) { const Registered = useAppSurface("pdfPreview"); if (Registered) return ; return ( {props.title} ); } export function EditableArtifactSurface(props: EditableArtifactSurfaceProps) { const Registered = useAppSurface("editableArtifact"); if (Registered) { // Its own boundary, so a cold load keeps the surrounding header and tabs. return ( }> ); } // A host without the editor opens the artifact in the web app. return (

This artifact opens in the Opengeni web app.

Open artifact
); } export function CodexSubscriptionsSurface(props: CodexSubscriptionsSurfaceProps) { const Registered = useAppSurface("codexSubscriptions"); if (Registered) return ; return null; }