import { useEffect, useRef, useState } from 'react'; interface Props { spaceSlug: string; height?: number; title?: string; // Tenant context propagated to the iframe via the handshake. Defaults // to VITE_A11OY_TENANT (build-time) or 'szl'. The iframe echoes this // value into recordAtelierRun() so persisted runs carry the host's // tenant; the host's X-Tenant-Id header (not this value) remains the // authorization source of truth on /api/atelier/proofs/:id. tenantId?: string; } // Live embed of an A11oy Atelier Space inside conduit. Loads the Atelier // embed host via iframe and records telemetry via /api/atelier/embed-events // so leaderboards reflect real cross-artifact usage. export function AtelierEmbedFrame({ spaceSlug, height = 380, title, tenantId }: Props) { const resolvedTenantId = tenantId ?? (import.meta.env.VITE_A11OY_TENANT as string | undefined) ?? 'szl'; const ref = useRef(null); const [lines, setLines] = useState([]); const [done, setDone] = useState(false); const [proofRef, setProofRef] = useState(null); // A11oy origin is configurable via VITE_A11OY_ORIGIN so the embed // works when the host artifact and A11oy are deployed on different // origins. Defaults to same-origin (preview pane shares a hostname // and routes by path prefix, so /embed/* hits the A11oy artifact). const atelierOrigin = (import.meta.env.VITE_A11OY_ORIGIN as string | undefined) ?? window.location.origin; const embedSrc = `${atelierOrigin}/embed/${spaceSlug}`; const [proofPacketId, setProofPacketId] = useState(null); useEffect(() => { function onMessage(e: MessageEvent) { if (!e.data || typeof e.data !== 'object') return; if (e.data.spaceSlug !== spaceSlug) return; if (e.data.type === 'a11oy-space-line') { setLines((p) => [...p, String(e.data.line)]); } else if (e.data.type === 'a11oy-space-done') { setDone(true); if (e.data.proofRef) setProofRef(String(e.data.proofRef)); if (e.data.proofPacketId) setProofPacketId(String(e.data.proofPacketId)); void fetch('/api/atelier/embed-events', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ spaceSlug, origin: window.location.origin, event: 'completed' }), }).catch(() => {}); } } window.addEventListener('message', onMessage); void fetch('/api/atelier/embed-events', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ spaceSlug, origin: window.location.origin, event: 'handshake' }), }).catch(() => {}); return () => window.removeEventListener('message', onMessage); }, [spaceSlug]); function runSpace() { setLines([]); setDone(false); setProofRef(null); ref.current?.contentWindow?.postMessage( { type: 'a11oy-space-handshake', spaceSlug, tenantId: resolvedTenantId }, atelierOrigin, ); setTimeout(() => { ref.current?.contentWindow?.postMessage( { type: 'a11oy-space-run', spaceSlug, tenantId: resolvedTenantId }, atelierOrigin, ); void fetch('/api/atelier/embed-events', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ spaceSlug, origin: window.location.origin, event: 'run' }), }).catch(() => {}); }, 250); } return (
{title ?? `Atelier Space · ${spaceSlug}`} Live embed