"use client"; import { useEffect, useMemo, useRef, useState } from "react"; import { Code2, Copy, Check } from "lucide-react"; import { useTranslation } from "react-i18next"; import type { VisualizeResult } from "@/lib/visualize-types"; function ChartJsRenderer({ config }: { config: string }) { 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 : "Failed to render chart"); } } } void render(); return () => { cancelled = true; if (chartRef.current) { (chartRef.current as { destroy: () => void }).destroy(); chartRef.current = null; } }; }, [config]); if (error) { return (

Chart rendering error

{error}
); } return (
); } function SvgRenderer({ svg }: { svg: string }) { const containerRef = useRef(null); const [error, setError] = useState(null); const sanitizedSvg = useMemo(() => { const trimmed = svg.trim(); if (!trimmed.startsWith("

SVG rendering error

{error}
); } return (
); } export default function VisualizationViewer({ result, }: { result: VisualizeResult; }) { const { t } = useTranslation(); const [showCode, setShowCode] = useState(false); const [copied, setCopied] = useState(false); const handleCopy = async () => { try { await navigator.clipboard.writeText(result.code.content); setCopied(true); setTimeout(() => setCopied(false), 2000); } catch { /* clipboard API may be unavailable */ } }; return (
{/* Visualization area */}
{result.render_type === "svg" ? ( ) : ( )}
{/* Toolbar */}
{result.render_type === "svg" ? "SVG" : `Chart.js ยท ${result.analysis.chart_type || "chart"}`}
{/* Code panel */} {showCode && (
{result.code.language}
            {result.code.content}
          
)} {/* Review notes */} {result.review.changed && result.review.review_notes && (

{t("Review")}: {result.review.review_notes}

)}
); }