import { useEffect, useState } from "react"; import { Check, Copy, Download } from "lucide-react"; import { SHIKI_THEMES } from "#shared/shiki-langs"; import { getEditorHighlighter, loadLanguage } from "../../editor/editor-highlighter"; interface EmbedCodeViewProps { html: string; filename: string; /** Rendered instead of the code when set (diff view). */ children?: React.ReactNode; } // Above this size Shiki takes long enough to stall typing in the chat. const MAX_HIGHLIGHT_CHARS = 200_000; export function EmbedCodeView({ html, filename, children }: EmbedCodeViewProps) { const [highlighted, setHighlighted] = useState(null); const [copied, setCopied] = useState(false); useEffect(() => { let cancelled = false; setHighlighted(null); if (!html || html.length > MAX_HIGHLIGHT_CHARS) return; getEditorHighlighter() .then(async (hl) => { await loadLanguage(hl, "html"); if (cancelled) return; setHighlighted( hl.codeToHtml(html, { lang: "html", themes: { light: SHIKI_THEMES.light, dark: SHIKI_THEMES.dark }, defaultColor: false, }), ); }) .catch(() => {}); return () => { cancelled = true; }; }, [html]); const copy = async () => { try { await navigator.clipboard.writeText(html); setCopied(true); setTimeout(() => setCopied(false), 1500); } catch { // Clipboard blocked (permissions, insecure context): nothing to do. } }; const download = () => { const blob = new Blob([html], { type: "text/html" }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = filename || "embed.html"; a.click(); setTimeout(() => URL.revokeObjectURL(url), 1000); }; return (
{html ? `${html.split("\n").length} lines` : "No code yet"}
{children ?? (highlighted ? (
) : (
              {html || "// No chart HTML yet"}
            
))}
); }