"use client"; /* Console screen to manage the embeddable chat widget (Phase 3b/4): enable it, choose the workflow, allow-list embedding origins, and copy the iframe snippet + publishable key. */ import { useEffect, useState } from "react"; import { Icon } from "../icons"; import { api, Workflow } from "@/lib/api"; /* Reusable settings body (no page chrome) so it can live standalone OR as a section inside the Connect screen's secondary nav. The container is expected to supply the header. */ export function EmbedPanel({ project }: { project: any }) { const [enabled, setEnabled] = useState(false); const [origins, setOrigins] = useState(""); const [workflowId, setWorkflowId] = useState(""); const [pubKey, setPubKey] = useState(null); const [wfs, setWfs] = useState([]); const [saving, setSaving] = useState(false); const [status, setStatus] = useState(null); const [origin, setOrigin] = useState(""); useEffect(() => { if (typeof window !== "undefined") setOrigin(window.location.origin); }, []); useEffect(() => { if (!project?.id) return; api.getEmbed(project.id).then((e) => { setEnabled(e.enabled); setOrigins((e.allowed_origins || []).join("\n")); setWorkflowId(e.workflow_id || ""); setPubKey(e.publishable_key || null); }).catch(() => {}); api.listWorkflows(project.id).then(setWfs).catch(() => {}); }, [project?.id]); async function save() { setSaving(true); setStatus(null); try { const e = await api.setEmbed(project.id, { enabled, allowed_origins: origins.split(/\s+/).map((s) => s.trim()).filter(Boolean), workflow_id: workflowId || null, }); setPubKey(e.publishable_key || null); setStatus("Saved."); setTimeout(() => setStatus(null), 1600); } catch (e: any) { setStatus(`Save failed: ${e.message || e}`); } finally { setSaving(false); } } const src = pubKey && enabled ? `${origin}/embed?key=${pubKey}` : null; const launcherSnippet = src ? `` : null; const iframeSnippet = src ? `` : null; return (
Which workflow the widget runs.