Raw
1 "use client"
2
3 import React, {
4 Suspense,
5 useCallback,
6 useEffect,
7 useMemo,
8 useRef,
9 useState,
10 } from "react"
11 import { Loading } from "@/components/Loading"
12 import type { RenderResult } from "mermaid"
13 import { Controlled as ControlledZoom } from "react-medium-image-zoom"
14 import "react-medium-image-zoom/dist/styles.css"
15 import clsx from "clsx"
16 import icons from "./icons.json"
17
18 type MermaidDiagramProps = {
19 diagramContent: string
20 }
21
22 const VIEWBOX_REGEX =
23 /viewBox="([0-9.-]+)\s+([0-9.-]+)\s+([0-9.-]+)\s+([0-9.-]+)"/
24
25 export const MermaidDiagram = ({ diagramContent }: MermaidDiagramProps) => {
26 const [result, setResult] = useState<RenderResult | null>(null)
27 const [isZoomed, setIsZoomed] = useState(false)
28 const mermaidId = useRef(
29 `mermaid-svg-${Math.round(Math.random() * 10000000)}`
30 ).current!
31
32 useEffect(() => {
33 const renderDiagram = async () => {
34 const { default: mermaid } = await import("mermaid")
35
36 mermaid.registerIconPacks([
37 {
38 name: icons.prefix,
39 loader: async () => icons,
40 },
41 ])
42
43 mermaid.initialize({
44 theme: "base",
45 themeVariables: {
46 primaryColor: "#FFF",
47 primaryBorderColor: "#D4D4D8",
48 secondaryColor: "#FFF",
49 tertiaryColor: "#FFF",
50 nodeBorder: "#D4D4D8",
51 mainBkg: "#FFF",
52 secondBkg: "#FFF",
53 tertiaryBkg: "#FFF",
54 lineColor: "#71717A",
55 primaryTextColor: "#18181B",
56 secondaryTextColor: "#18181B",
57 tertiaryTextColor: "#18181B",
58 edgeLabelBackground: "#FAFAFA",
59 textColor: "rgba(82, 82, 91, 1)",
60 fontFamily: "Inter, sans-serif",
61 fontSize: "14px",
62 },
63 sequence: {
64 mirrorActors: false,
65 },
66 })
67
68 await mermaid
69 .render(mermaidId, diagramContent)
70 .then(setResult)
71 .catch((e) =>
72 console.error(
73 `An error occurred while rendering Mermaid.js diagram. Content: \n ${diagramContent}\n Error: ${e}`
74 )
75 )
76 }
77
78 void renderDiagram()
79 }, [mermaidId, diagramContent])
80
81 const viewBox = useMemo(() => {
82 const matchedRegex = result ? VIEWBOX_REGEX.exec(result.svg) : undefined
83
84 if (!matchedRegex) {
85 return undefined
86 }
87
88 const [, x, y, width, height] = matchedRegex
89
90 return { value: `${x} ${y} ${width} ${height}`, width, height }
91 }, [result])
92
93 const handleZoomChange = useCallback((shouldZoom: boolean) => {
94 setIsZoomed(shouldZoom)
95 }, [])
96
97 return (
98 <Suspense fallback={<Loading />}>
99 <ControlledZoom
100 isZoomed={isZoomed}
101 onZoomChange={handleZoomChange}
102 classDialog={clsx(
103 "[&_data-rmiz-modal-img]:!top-0 [&_data-rmiz-modal-img]:!left-0 [&_data-rmiz-modal-img]:!transform-x-0",
104 ["[&_data-rmiz-modal-img]:!transform-y-0 [&_data-rmiz-modal-img]:"]
105 )}
106 >
107 <div
108 className={clsx(
109 "bg-medusa-bg-subtle rounded-docs_DEFAULT my-docs_1",
110 "flex items-center justify-center overflow-hidden",
111 isZoomed ? "h-screen w-screen" : "aspect-video w-full"
112 )}
113 >
114 <svg
115 dangerouslySetInnerHTML={
116 result ? { __html: result.svg } : undefined
117 }
118 viewBox={viewBox?.value}
119 preserveAspectRatio="xMidYMid meet"
120 style={{ width: viewBox ? `${viewBox.width}px` : "100%" }}
121 className="h-auto max-h-full max-w-full"
122 />
123 </div>
124 </ControlledZoom>
125 </Suspense>
126 )
127 }