|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useEffect, useState, useRef } from 'react'; |
| 4 | + |
| 5 | +interface MermaidDiagramProps { |
| 6 | + chart: string; |
| 7 | + className?: string; |
| 8 | +} |
| 9 | + |
| 10 | +export const MermaidDiagram = ({ |
| 11 | + chart, |
| 12 | + className = '', |
| 13 | +}: MermaidDiagramProps) => { |
| 14 | + const [isLoading, setIsLoading] = useState(true); |
| 15 | + const [error, setError] = useState<string | null>(null); |
| 16 | + const [svgContent, setSvgContent] = useState<string>(''); |
| 17 | + const [isExpanded, setIsExpanded] = useState(false); |
| 18 | + const isMountedRef = useRef(true); |
| 19 | + |
| 20 | + useEffect(() => { |
| 21 | + // Reset mounted flag when component mounts |
| 22 | + isMountedRef.current = true; |
| 23 | + |
| 24 | + const renderDiagram = async () => { |
| 25 | + if (!chart.trim()) { |
| 26 | + if (isMountedRef.current) { |
| 27 | + setIsLoading(false); |
| 28 | + } |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + try { |
| 33 | + if (isMountedRef.current) { |
| 34 | + setIsLoading(true); |
| 35 | + setError(null); |
| 36 | + setSvgContent(''); |
| 37 | + } |
| 38 | + |
| 39 | + const mermaid = (await import('mermaid')).default; |
| 40 | + |
| 41 | + // Check if component is still mounted after async import |
| 42 | + if (!isMountedRef.current) return; |
| 43 | + |
| 44 | + // Configure mermaid with base theme |
| 45 | + mermaid.initialize({ |
| 46 | + startOnLoad: false, |
| 47 | + theme: 'base', |
| 48 | + suppressErrorRendering: true, |
| 49 | + }); |
| 50 | + |
| 51 | + // Generate unique ID |
| 52 | + const id = `mermaid-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`; |
| 53 | + |
| 54 | + // Render the diagram |
| 55 | + const { svg } = await mermaid.render(id, chart); |
| 56 | + |
| 57 | + // Check if component is still mounted after async render |
| 58 | + if (!isMountedRef.current) return; |
| 59 | + |
| 60 | + setSvgContent(svg); |
| 61 | + setIsLoading(false); |
| 62 | + } catch (err) { |
| 63 | + // Only update state if component is still mounted |
| 64 | + if (isMountedRef.current) { |
| 65 | + setError( |
| 66 | + err instanceof Error ? err.message : 'Failed to render diagram', |
| 67 | + ); |
| 68 | + setIsLoading(false); |
| 69 | + } |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + renderDiagram(); |
| 74 | + |
| 75 | + // Cleanup function to mark component as unmounted |
| 76 | + return () => { |
| 77 | + isMountedRef.current = false; |
| 78 | + }; |
| 79 | + }, [chart]); |
| 80 | + |
| 81 | + return ( |
| 82 | + <div |
| 83 | + className={`mermaid-diagram bg-background border border-border rounded-md p-4 overflow-x-auto ${className}`} |
| 84 | + style={{ |
| 85 | + color: 'inherit', |
| 86 | + minHeight: '100px', |
| 87 | + }} |
| 88 | + > |
| 89 | + {error && ( |
| 90 | + <div className="bg-yellow-50 border border-yellow-200 rounded-md p-4"> |
| 91 | + <div className="text-yellow-800 text-sm font-medium mb-2"> |
| 92 | + Mermaid Diagram Error |
| 93 | + </div> |
| 94 | + <div className="text-yellow-700 text-xs font-mono">{error}</div> |
| 95 | + <details className="mt-2"> |
| 96 | + <summary className="text-yellow-600 text-xs cursor-pointer hover:text-yellow-800"> |
| 97 | + Show diagram source |
| 98 | + </summary> |
| 99 | + <pre className="mt-2 text-xs bg-yellow-50 p-2 rounded border border-yellow-200 overflow-x-auto"> |
| 100 | + <code>{chart}</code> |
| 101 | + </pre> |
| 102 | + </details> |
| 103 | + </div> |
| 104 | + )} |
| 105 | + |
| 106 | + {isLoading && !error && ( |
| 107 | + <div className="flex items-center justify-center min-h-[100px]"> |
| 108 | + <div className="flex items-center gap-2 text-muted-foreground"> |
| 109 | + <div className="animate-spin h-4 w-4 border-2 border-current border-t-transparent rounded-full" /> |
| 110 | + <span className="text-sm">Rendering diagram...</span> |
| 111 | + </div> |
| 112 | + </div> |
| 113 | + )} |
| 114 | + |
| 115 | + {svgContent && !isLoading && !error && ( |
| 116 | + <div className="relative"> |
| 117 | + <div |
| 118 | + dangerouslySetInnerHTML={{ __html: svgContent }} |
| 119 | + className={`mermaid-svg-container transition-all duration-300 cursor-pointer ${ |
| 120 | + isExpanded ? '' : 'max-h-96 overflow-hidden' |
| 121 | + }`} |
| 122 | + onClick={() => setIsExpanded(!isExpanded)} |
| 123 | + title={isExpanded ? 'Click to collapse' : 'Click to expand'} |
| 124 | + /> |
| 125 | + {!isExpanded && ( |
| 126 | + <div className="absolute bottom-0 left-0 right-0 h-16 bg-gradient-to-t from-background to-transparent pointer-events-none" /> |
| 127 | + )} |
| 128 | + <div className="mt-2 text-center"> |
| 129 | + <button |
| 130 | + onClick={() => setIsExpanded(!isExpanded)} |
| 131 | + className="text-xs text-muted-foreground hover:text-foreground transition-colors px-2 py-1 rounded border border-border hover:bg-muted" |
| 132 | + > |
| 133 | + {isExpanded ? '↑ Collapse diagram' : '↓ Expand diagram'} |
| 134 | + </button> |
| 135 | + </div> |
| 136 | + </div> |
| 137 | + )} |
| 138 | + </div> |
| 139 | + ); |
| 140 | +}; |
0 commit comments