|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import * as React from 'react'; |
| 4 | +import { useTheme } from 'next-themes'; |
| 5 | +import { Skeleton } from '@/components/ui/skeleton'; |
| 6 | +import { Button } from '@/components/ui/button'; |
| 7 | +import { Code2, AlertCircle } from 'lucide-react'; |
| 8 | + |
| 9 | +interface MermaidDiagramProps { |
| 10 | + code: string; |
| 11 | +} |
| 12 | + |
| 13 | +// Counter for generating unique IDs |
| 14 | +let diagramIdCounter = 0; |
| 15 | + |
| 16 | +export function MermaidDiagram({ code }: MermaidDiagramProps) { |
| 17 | + const containerRef = React.useRef<HTMLDivElement>(null); |
| 18 | + const [svg, setSvg] = React.useState<string>(''); |
| 19 | + const [error, setError] = React.useState<string | null>(null); |
| 20 | + const [isLoading, setIsLoading] = React.useState(true); |
| 21 | + const [showSource, setShowSource] = React.useState(false); |
| 22 | + const { resolvedTheme } = useTheme(); |
| 23 | + const [mounted, setMounted] = React.useState(false); |
| 24 | + |
| 25 | + // Track mount state for SSR safety |
| 26 | + React.useEffect(() => { |
| 27 | + setMounted(true); |
| 28 | + }, []); |
| 29 | + |
| 30 | + // Render the mermaid diagram |
| 31 | + React.useEffect(() => { |
| 32 | + if (!mounted) return; |
| 33 | + |
| 34 | + const renderDiagram = async () => { |
| 35 | + setIsLoading(true); |
| 36 | + setError(null); |
| 37 | + |
| 38 | + try { |
| 39 | + // Dynamically import mermaid to avoid SSR issues |
| 40 | + const mermaid = (await import('mermaid')).default; |
| 41 | + |
| 42 | + mermaid.initialize({ |
| 43 | + startOnLoad: false, |
| 44 | + theme: resolvedTheme === 'dark' ? 'dark' : 'default', |
| 45 | + // Use 'strict' for security - specs are developer-authored |
| 46 | + // but we still want to prevent potential XSS from code blocks |
| 47 | + securityLevel: 'strict', |
| 48 | + fontFamily: 'inherit', |
| 49 | + }); |
| 50 | + |
| 51 | + // Generate unique ID for the diagram using counter |
| 52 | + const id = `mermaid-${Date.now()}-${++diagramIdCounter}`; |
| 53 | + const cleanedCode = code.trim(); |
| 54 | + |
| 55 | + const { svg: renderedSvg } = await mermaid.render(id, cleanedCode); |
| 56 | + setSvg(renderedSvg); |
| 57 | + setError(null); |
| 58 | + } catch (err) { |
| 59 | + const errorMessage = err instanceof Error ? err.message : 'Diagram render failed'; |
| 60 | + setError(errorMessage); |
| 61 | + setSvg(''); |
| 62 | + } finally { |
| 63 | + setIsLoading(false); |
| 64 | + } |
| 65 | + }; |
| 66 | + |
| 67 | + renderDiagram(); |
| 68 | + }, [code, resolvedTheme, mounted]); |
| 69 | + |
| 70 | + // Show skeleton during SSR or initial mount |
| 71 | + if (!mounted) { |
| 72 | + return <DiagramSkeleton />; |
| 73 | + } |
| 74 | + |
| 75 | + // Show loading state |
| 76 | + if (isLoading) { |
| 77 | + return <DiagramSkeleton />; |
| 78 | + } |
| 79 | + |
| 80 | + // Show error state with fallback to source |
| 81 | + if (error) { |
| 82 | + return ( |
| 83 | + <div className="my-4 border border-destructive/50 bg-destructive/10 rounded-md overflow-hidden"> |
| 84 | + <div className="flex items-center gap-2 px-4 py-2 bg-destructive/20 border-b border-destructive/30"> |
| 85 | + <AlertCircle className="h-4 w-4 text-destructive" /> |
| 86 | + <span className="text-sm font-medium text-destructive">Diagram error</span> |
| 87 | + </div> |
| 88 | + <div className="p-4"> |
| 89 | + <p className="text-sm text-destructive mb-3">{error}</p> |
| 90 | + <pre className="text-xs overflow-x-auto bg-muted/50 p-3 rounded border"> |
| 91 | + <code>{code}</code> |
| 92 | + </pre> |
| 93 | + </div> |
| 94 | + </div> |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + // Show rendered diagram with source toggle |
| 99 | + return ( |
| 100 | + <div className="my-4 border rounded-md overflow-hidden"> |
| 101 | + <div className="flex items-center justify-end px-3 py-1.5 bg-muted/30 border-b"> |
| 102 | + <Button |
| 103 | + variant="ghost" |
| 104 | + size="sm" |
| 105 | + onClick={() => setShowSource(!showSource)} |
| 106 | + className="h-7 text-xs" |
| 107 | + > |
| 108 | + <Code2 className="h-3.5 w-3.5 mr-1.5" /> |
| 109 | + {showSource ? 'View Diagram' : 'View Source'} |
| 110 | + </Button> |
| 111 | + </div> |
| 112 | + |
| 113 | + {showSource ? ( |
| 114 | + <pre className="text-xs overflow-x-auto p-4 bg-muted/20"> |
| 115 | + <code className="language-mermaid">{code}</code> |
| 116 | + </pre> |
| 117 | + ) : ( |
| 118 | + <div |
| 119 | + ref={containerRef} |
| 120 | + className="flex justify-center overflow-x-auto p-4 bg-background" |
| 121 | + dangerouslySetInnerHTML={{ __html: svg }} |
| 122 | + /> |
| 123 | + )} |
| 124 | + </div> |
| 125 | + ); |
| 126 | +} |
| 127 | + |
| 128 | +function DiagramSkeleton() { |
| 129 | + return ( |
| 130 | + <div className="my-4 border rounded-md overflow-hidden"> |
| 131 | + <div className="flex items-center justify-end px-3 py-1.5 bg-muted/30 border-b"> |
| 132 | + <Skeleton className="h-7 w-24" /> |
| 133 | + </div> |
| 134 | + <div className="flex justify-center items-center p-8"> |
| 135 | + <Skeleton className="h-48 w-full max-w-md" /> |
| 136 | + </div> |
| 137 | + </div> |
| 138 | + ); |
| 139 | +} |
0 commit comments