"use client"; import { useEffect, useMemo, useRef, useState } from "react"; import { Code2, Copy, Check, ExternalLink, Maximize2, X } from "lucide-react"; import { useTranslation } from "react-i18next"; import { Mermaid } from "@/components/Mermaid"; import { prepareIframeHtml } from "@/lib/iframe-html"; import type { VisualizeResult } from "@/lib/visualize-types"; function ChartJsRenderer({ config }: { config: string }) { const { t } = useTranslation(); const canvasRef = useRef(null); const chartRef = useRef(null); const [error, setError] = useState(null); useEffect(() => { let cancelled = false; async function render() { if (!canvasRef.current) return; try { const ChartModule = await import("chart.js/auto"); const Chart = ChartModule.default; if (chartRef.current) { (chartRef.current as InstanceType).destroy(); chartRef.current = null; } // eslint-disable-next-line no-new-func const parsedConfig = new Function( `"use strict"; return (${config});`, )(); if (cancelled) return; chartRef.current = new Chart(canvasRef.current, parsedConfig); setError(null); } catch (err) { if (!cancelled) { setError( err instanceof Error ? err.message : t("Failed to render chart"), ); } } } void render(); return () => { cancelled = true; if (chartRef.current) { (chartRef.current as { destroy: () => void }).destroy(); chartRef.current = null; } }; }, [config, t]); if (error) { return (

{t("Chart rendering error")}

          {error}
        
); } return (
); } function HtmlRenderer({ html }: { html: string }) { const iframeRef = useRef(null); const prepared = useMemo(() => prepareIframeHtml(html || ""), [html]); useEffect(() => { const iframe = iframeRef.current; if (!iframe) return; iframe.srcdoc = prepared; }, [prepared]); const handleOpenInNewTab = () => { try { const blob = new Blob([prepared], { type: "text/html" }); const url = URL.createObjectURL(blob); window.open(url, "_blank", "noopener,noreferrer"); // Best-effort cleanup; the new tab keeps its own reference. setTimeout(() => URL.revokeObjectURL(url), 60_000); } catch { /* no-op */ } }; return (