import { ArrowLeftIcon, Globe2Icon, Maximize2Icon, PlugZapIcon, RefreshCwIcon, SparklesIcon, } from "lucide-react"; import { useCallback, useLayoutEffect, useRef, useState, type ReactElement, type ReactNode, } from "react"; import type { SiteViewer } from "@opengeni/sdk"; import { cn } from "../../lib/cn"; import { useWindowLayerOpen } from "../../lib/window-layer"; import { PortalContainerProvider, usePortalContainer } from "../../ui/portal-container"; import { ArtifactBadge, ArtifactButton, useArtifactLabels } from "./artifact-chrome"; import { PublishedHtmlArtifactFrame, type PublishedHtmlArtifactToolBridge, } from "./published-html-artifact-frame"; /** * What the frame's Live label says when the host watches the Site for new * versions: showing the current version, loading a newer one, or unable to * check for them. */ export type SiteLiveState = "live" | "updating" | "reconnecting"; export type ArtifactSandboxProps = { html: string; height?: number | undefined; /** * Size the frame to its document's reported height (see * `PublishedHtmlArtifactFrame`); `height` is the size until the first report. */ autoHeight?: boolean | undefined; /** Upper bound for `autoHeight`, in CSS pixels; taller content scrolls inside. */ maxAutoHeight?: number | undefined; theme?: "light" | "dark" | undefined; headerControls?: ReactNode | undefined; /** * The toolbar's first control, for example the host's Back to the page the * Site was opened from. Full screen shows its own Back there instead. */ toolbarStart?: ReactNode | undefined; title: string; /** Hide the visible title/icon while retaining the accessible iframe title. */ showTitle?: boolean | undefined; /** * Wraps the visible title, so a host can attach its own menu to it (the * Site view's Copy title). Returns the title itself when absent. */ renderTitle?: ((title: ReactElement) => ReactNode) | undefined; showLiveStatus?: boolean | undefined; /** * Replaces the "Live" indicator, for example with the host's link to the * published Site. `null` shows nothing there; omit it for the default. */ liveStatus?: ReactNode | undefined; /** * The real state of the Site on screen, from a host that watches it for new * versions. `updating` and `reconnecting` show beside the Live indicator * (or the host's replacement); without it the plain "Live" badge shows. */ liveState?: SiteLiveState | undefined; versionLabel?: string | undefined; className?: string | undefined; editDisabled?: boolean | undefined; onEdit?: (() => void) | undefined; toolBridge?: PublishedHtmlArtifactToolBridge | undefined; /** Who is viewing, answered to the Site's `site.viewer` over the bridge. */ viewer?: SiteViewer | undefined; /** * The Site's own address (`/admin?tab=2`), answered to `site.location`. * Omit it to let the frame keep the address itself (starting at `/`, kept * across reloads); pass it with `onSiteLocationChange` to keep it in the * host, for example in its URL. */ siteLocation?: string | undefined; /** The Site moved itself with `site.navigate`. */ onSiteLocationChange?: ((href: string, options: { replace: boolean }) => void) | undefined; connectedToolCount?: number | undefined; sourceFileCount?: number | undefined; fill?: boolean | undefined; /** * Control full screen from the host (for example from its URL). Omit to let * the frame's own expand and back buttons manage it. */ fullScreen?: boolean | undefined; /** Called when the person expands the frame or leaves full screen. */ onFullScreenChange?: ((fullScreen: boolean) => void) | undefined; /** * Hide the frame's toolbar when the host draws its own reload and full * screen controls. Full screen always shows it, for its Back button. */ showToolbar?: boolean | undefined; /** Reload the Site whenever this changes, from a host-drawn reload control. */ reloadToken?: number | undefined; /** * The page the frame shows, for example its version: a new key replaces * the frame in the same render as its HTML, so the Site loads once. */ frameKey?: string | undefined; }; /** * The Site/preview frame chrome shared by the console and embedding hosts: * title, version and tool badges, live status, reload, and a full-screen mode * that escapes the host's scroll containers through the modal top layer. */ export function ArtifactSandbox(props: ArtifactSandboxProps) { const labels = useArtifactLabels(); const [reloadKey, setReloadKey] = useState(0); const [ownFocused, setOwnFocused] = useState(false); const [ownSiteLocation, setOwnSiteLocation] = useState("/"); const siteLocation = props.siteLocation ?? ownSiteLocation; const onSiteLocationChangeRef = useRef(props.onSiteLocationChange); onSiteLocationChangeRef.current = props.onSiteLocationChange; const ownsSiteLocation = props.siteLocation === undefined; const onSiteLocationChange = useCallback( (href: string, options: { replace: boolean }) => { if (ownsSiteLocation) setOwnSiteLocation(href); onSiteLocationChangeRef.current?.(href, options); }, [ownsSiteLocation], ); const focused = props.fullScreen ?? ownFocused; // Full screen, the Site fills the window's content. useWindowLayerOpen(focused); const setFocused = (next: boolean) => { if (props.fullScreen === undefined) setOwnFocused(next); props.onFullScreenChange?.(next); }; const frameRef = useRef(null); const modalRef = useRef(false); // In full screen everything outside the modal is inert, so menus opened // from the header (the version menu) draw inside it. const inheritedPortal = usePortalContainer(); const [frameElement, setFrameElement] = useState(null); const attachFrame = useCallback((element: HTMLDialogElement | null) => { frameRef.current = element; setFrameElement(element); }, []); useLayoutEffect(() => { const frame = frameRef.current; if (!frame) return; if (focused) { // A fixed child of the chat timeline is still clipped and layered by its // scroll pane and the sibling workspace dock. The modal top layer escapes // both while keeping the live Site iframe mounted in the same DOM node. frame.close(); frame.showModal(); modalRef.current = true; } else if (modalRef.current) { frame.close(); frame.show(); modalRef.current = false; } }, [focused]); const reload = () => { setReloadKey((value) => value + 1); }; return ( { event.preventDefault(); setFocused(false); }} className={cn( "og-root static m-0 w-full max-h-none max-w-none overflow-hidden rounded-2xl border border-border/80 bg-white p-0 text-left shadow-sm", props.className, props.fill && "flex min-h-0 flex-col", focused && "fixed inset-0 z-50 flex h-dvh w-dvw flex-col rounded-none border-0 bg-surface shadow-none", )} > ); } /** * Beside a host's replacement for Live: only Updating or Reconnecting shows. * The status region stays mounted and only its text changes, so screen * readers announce each change. */ function LiveStateBeside({ status, labels, }: { status: SiteLiveState; labels: ReturnType; }) { const busy = status === "updating" || status === "reconnecting"; return ( {busy ? ( <> ); } /** * Live, or what the host watching the Site knows: a newer version loading, * or no way to check for one right now. Announced politely when it changes; * on a narrow screen only its dot shows, and it is still announced. */ function LiveLabel({ status, labels, }: { status: SiteLiveState | undefined; labels: ReturnType; }) { const text = status === "updating" ? labels.liveUpdating : status === "reconnecting" ? labels.liveReconnecting : labels.live; return ( ); }