"use client"; import { useEffect, useRef, useState } from "react"; import mermaid from "mermaid"; interface MermaidDiagramProps { chart: string; } export function MermaidDiagram({ chart }: MermaidDiagramProps) { const containerRef = useRef(null); const [error, setError] = useState(null); const [isReady, setIsReady] = useState(false); useEffect(() => { mermaid.initialize({ startOnLoad: false, theme: "dark", securityLevel: "loose", themeVariables: { darkMode: true, background: "#0a0a0f", primaryColor: "#1e3a5f", primaryTextColor: "#ffffff", primaryBorderColor: "#3b82f6", lineColor: "#6366f1", secondaryColor: "#1f2937", tertiaryColor: "#374151", fontFamily: "ui-monospace, monospace", }, flowchart: { useMaxWidth: true, htmlLabels: true, curve: "basis", }, }); setIsReady(true); }, []); useEffect(() => { if (!isReady || !containerRef.current || !chart) return; const renderChart = async () => { try { const id = `mermaid-${Math.random().toString(36).slice(2, 9)}`; const { svg } = await mermaid.render(id, chart); if (containerRef.current) { containerRef.current.innerHTML = svg; setError(null); } } catch (err) { setError(err instanceof Error ? err.message : "Failed to render diagram"); } }; renderChart(); }, [chart, isReady]); if (error) { return (

Failed to render diagram

{chart}
); } return (
{!isReady && (
)}
); }