|
| 1 | +import { Minus, Plus, Scan, X } from "lucide-react"; |
| 2 | +import { useEffect, useId, useMemo, useRef, useState } from "react"; |
| 3 | +import { TransformComponent, TransformWrapper } from "react-zoom-pan-pinch"; |
| 4 | +import { renderMermaidDiagram } from "@/lib/mermaid-renderer"; |
| 5 | +import { useTheme } from "@/lib/theme"; |
| 6 | +import { cn } from "@/lib/utils"; |
| 7 | + |
| 8 | +function prepareSvgForDialog(svgHtml: string): string { |
| 9 | + const div = document.createElement("div"); |
| 10 | + div.innerHTML = svgHtml; |
| 11 | + const svgEl = div.querySelector("svg"); |
| 12 | + if (!svgEl) return svgHtml; |
| 13 | + |
| 14 | + const w = svgEl.getAttribute("width"); |
| 15 | + const h = svgEl.getAttribute("height"); |
| 16 | + if (!svgEl.hasAttribute("viewBox") && w && h) { |
| 17 | + svgEl.setAttribute("viewBox", `0 0 ${parseFloat(w)} ${parseFloat(h)}`); |
| 18 | + } |
| 19 | + |
| 20 | + svgEl.removeAttribute("width"); |
| 21 | + svgEl.removeAttribute("height"); |
| 22 | + svgEl.style.removeProperty("max-width"); |
| 23 | + svgEl.setAttribute("preserveAspectRatio", "xMidYMid meet"); |
| 24 | + |
| 25 | + return div.innerHTML; |
| 26 | +} |
| 27 | + |
| 28 | +interface MermaidDiagramProps { |
| 29 | + chart: string; |
| 30 | +} |
| 31 | + |
| 32 | +const ZOOM_CONTROLS_CLASSES = |
| 33 | + "cursor-pointer rounded-md p-1.5 text-muted-foreground transition-colors hover:bg-accent hover:text-foreground"; |
| 34 | + |
| 35 | +export function MermaidDiagram({ chart }: MermaidDiagramProps) { |
| 36 | + const { appTheme } = useTheme(); |
| 37 | + const [svg, setSvg] = useState<string | null>(null); |
| 38 | + const [error, setError] = useState(false); |
| 39 | + const dialogRef = useRef<HTMLDialogElement>(null); |
| 40 | + const titleId = useId(); |
| 41 | + const prevChartRef = useRef(chart); |
| 42 | + const pointerDownTargetRef = useRef<EventTarget | null>(null); |
| 43 | + const dialogSvg = useMemo(() => (svg ? prepareSvgForDialog(svg) : null), [svg]); |
| 44 | + |
| 45 | + useEffect(() => { |
| 46 | + let cancelled = false; |
| 47 | + |
| 48 | + if (prevChartRef.current !== chart) { |
| 49 | + prevChartRef.current = chart; |
| 50 | + setSvg(null); |
| 51 | + setError(false); |
| 52 | + } |
| 53 | + |
| 54 | + renderMermaidDiagram(chart, appTheme) |
| 55 | + .then(({ svg }) => { |
| 56 | + if (cancelled) return; |
| 57 | + setSvg(svg); |
| 58 | + setError(false); |
| 59 | + }) |
| 60 | + .catch(() => { |
| 61 | + if (!cancelled) setError(true); |
| 62 | + }); |
| 63 | + |
| 64 | + return () => { |
| 65 | + cancelled = true; |
| 66 | + }; |
| 67 | + }, [chart, appTheme]); |
| 68 | + |
| 69 | + if (error) { |
| 70 | + return ( |
| 71 | + <pre className="my-2 overflow-x-auto rounded-md border border-border/50 bg-muted/30 p-3 text-xs text-muted-foreground"> |
| 72 | + {chart} |
| 73 | + </pre> |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + return ( |
| 78 | + <> |
| 79 | + <div className="group relative my-3"> |
| 80 | + <div |
| 81 | + className={cn( |
| 82 | + "flex max-h-64 justify-center overflow-hidden [&_svg]:max-w-full", |
| 83 | + !svg && "h-24 animate-pulse rounded-md bg-muted/30", |
| 84 | + )} |
| 85 | + // biome-ignore lint/security/noDangerouslySetInnerHtml: mermaid.render() produces safe SVG |
| 86 | + dangerouslySetInnerHTML={svg ? { __html: svg } : undefined} |
| 87 | + /> |
| 88 | + {svg && ( |
| 89 | + <button |
| 90 | + type="button" |
| 91 | + onClick={() => dialogRef.current?.showModal()} |
| 92 | + className="absolute inset-0 flex cursor-pointer items-end justify-center bg-gradient-to-t from-background/80 to-transparent opacity-0 outline-none transition-opacity group-hover:opacity-100 focus-visible:opacity-100" |
| 93 | + > |
| 94 | + <span className="mb-3 rounded-md border border-border bg-background px-3 py-1.5 text-xs font-medium text-foreground shadow-sm"> |
| 95 | + View full diagram |
| 96 | + </span> |
| 97 | + </button> |
| 98 | + )} |
| 99 | + </div> |
| 100 | + |
| 101 | + {/* biome-ignore lint/a11y/useKeyWithClickEvents: backdrop click-to-close is a standard dialog pattern */} |
| 102 | + <dialog |
| 103 | + ref={dialogRef} |
| 104 | + aria-labelledby={titleId} |
| 105 | + className="fixed inset-0 m-auto hidden h-[90vh] w-[90vw] flex-col overflow-hidden rounded-xl border border-border bg-background p-0 text-foreground shadow-lg open:flex backdrop:bg-black/60 backdrop:backdrop-blur-sm" |
| 106 | + onPointerDown={(e) => { |
| 107 | + pointerDownTargetRef.current = e.target; |
| 108 | + }} |
| 109 | + onClick={(e) => { |
| 110 | + if (e.target === e.currentTarget && pointerDownTargetRef.current === e.currentTarget) |
| 111 | + e.currentTarget.close(); |
| 112 | + }} |
| 113 | + > |
| 114 | + <div className="flex items-center justify-between border-b border-border px-5 py-3.5"> |
| 115 | + <h2 id={titleId} className="text-sm font-semibold"> |
| 116 | + Diagram |
| 117 | + </h2> |
| 118 | + <button |
| 119 | + type="button" |
| 120 | + aria-label="Close diagram" |
| 121 | + className="cursor-pointer rounded-md p-1 text-muted-foreground transition-colors hover:bg-accent hover:text-foreground" |
| 122 | + onClick={() => dialogRef.current?.close()} |
| 123 | + > |
| 124 | + <X className="size-4" /> |
| 125 | + </button> |
| 126 | + </div> |
| 127 | + <div className="min-h-0 flex-1"> |
| 128 | + {dialogSvg && ( |
| 129 | + <TransformWrapper |
| 130 | + initialScale={1} |
| 131 | + minScale={0.5} |
| 132 | + maxScale={4} |
| 133 | + centerOnInit |
| 134 | + wheel={{ step: 0.08 }} |
| 135 | + > |
| 136 | + {({ zoomIn, zoomOut, resetTransform }) => ( |
| 137 | + <> |
| 138 | + <div className="absolute bottom-4 left-1/2 z-10 flex -translate-x-1/2 items-center gap-1 rounded-lg border border-border bg-background/90 px-1.5 py-1 shadow-md backdrop-blur-sm"> |
| 139 | + <button |
| 140 | + type="button" |
| 141 | + aria-label="Zoom out" |
| 142 | + className={ZOOM_CONTROLS_CLASSES} |
| 143 | + onClick={() => zoomOut()} |
| 144 | + > |
| 145 | + <Minus className="size-4" /> |
| 146 | + </button> |
| 147 | + <button |
| 148 | + type="button" |
| 149 | + aria-label="Reset zoom" |
| 150 | + className={ZOOM_CONTROLS_CLASSES} |
| 151 | + onClick={() => resetTransform()} |
| 152 | + > |
| 153 | + <Scan className="size-4" /> |
| 154 | + </button> |
| 155 | + <button |
| 156 | + type="button" |
| 157 | + aria-label="Zoom in" |
| 158 | + className={ZOOM_CONTROLS_CLASSES} |
| 159 | + onClick={() => zoomIn()} |
| 160 | + > |
| 161 | + <Plus className="size-4" /> |
| 162 | + </button> |
| 163 | + </div> |
| 164 | + <TransformComponent wrapperClass="!h-full !w-full" contentClass="!h-full !w-full"> |
| 165 | + <div |
| 166 | + className="flex h-full w-full items-center justify-center p-6 [&_svg]:max-h-full [&_svg]:max-w-full" |
| 167 | + // biome-ignore lint/security/noDangerouslySetInnerHtml: mermaid.render() produces safe SVG |
| 168 | + dangerouslySetInnerHTML={{ __html: dialogSvg }} |
| 169 | + /> |
| 170 | + </TransformComponent> |
| 171 | + </> |
| 172 | + )} |
| 173 | + </TransformWrapper> |
| 174 | + )} |
| 175 | + </div> |
| 176 | + </dialog> |
| 177 | + </> |
| 178 | + ); |
| 179 | +} |
0 commit comments