Spaces:
Paused
Paused
File size: 3,547 Bytes
4e7644b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | import { useEffect, useState } from "react";
import mermaid from "mermaid";
import { CircleAlert } from "lucide-react";
import { useTheme } from "../../contexts/ThemeContext";
import styles from "./index.module.less";
let initializedTheme: "light" | "dark" | null = null;
let idCounter = 0;
function ensureMermaidInit(isDark: boolean) {
const theme = isDark ? "dark" : "light";
if (initializedTheme === theme) return;
mermaid.initialize({
startOnLoad: false,
theme: "base",
securityLevel: "loose",
themeVariables: isDark
? {
background: "#17120f",
clusterBkg: "#211914",
clusterBorder: "#7a4728",
edgeLabelBackground: "#17120f",
lineColor: "#d19a78",
mainBkg: "#2b211c",
nodeBorder: "#f36b21",
primaryBorderColor: "#f36b21",
primaryColor: "#2b211c",
primaryTextColor: "#fff4ec",
secondaryColor: "#35251c",
tertiaryColor: "#241b17",
textColor: "#fff4ec",
titleColor: "#fff4ec",
}
: {
background: "#fffaf6",
clusterBkg: "#fff3e9",
clusterBorder: "#e8793b",
edgeLabelBackground: "#fffaf6",
lineColor: "#9a5a35",
mainBkg: "#fff7f0",
nodeBorder: "#e35f18",
primaryBorderColor: "#e35f18",
primaryColor: "#fff7f0",
primaryTextColor: "#3b2416",
secondaryColor: "#fff0e4",
tertiaryColor: "#fffaf6",
textColor: "#3b2416",
titleColor: "#3b2416",
},
});
initializedTheme = theme;
}
interface MermaidCodeBlockProps {
chart: string;
}
export function MermaidCodeBlock({ chart }: MermaidCodeBlockProps) {
const { isDark } = useTheme();
const trimmedChart = chart.trim();
const [svg, setSvg] = useState<string>("");
const [error, setError] = useState<string>("");
const [isRendering, setIsRendering] = useState<boolean>(!!trimmedChart);
useEffect(() => {
if (!trimmedChart) {
setSvg("");
setError("");
setIsRendering(false);
return;
}
ensureMermaidInit(isDark);
let cancelled = false;
const id = `mermaid-${Date.now()}-${idCounter++}`;
setSvg("");
setError("");
setIsRendering(true);
mermaid
.render(id, trimmedChart)
.then(({ svg: rendered }) => {
if (!cancelled) {
setSvg(rendered);
setError("");
setIsRendering(false);
}
})
.catch((renderError) => {
if (!cancelled) {
setError(String(renderError));
setSvg("");
setIsRendering(false);
}
const orphan = document.getElementById("d" + id);
orphan?.remove();
});
return () => {
cancelled = true;
};
}, [isDark, trimmedChart]);
if (error) {
return (
<div className={styles.mermaidError} role="alert">
<CircleAlert aria-hidden="true" size={18} />
<div>
<strong>Unable to render diagram</strong>
<span>{error}</span>
</div>
</div>
);
}
return (
<div
className={`${styles.mermaidDiagram}${
isRendering ? ` ${styles.isLoading}` : ""
}`}
>
{isRendering ? (
<div className={styles.placeholder} aria-hidden="true">
Loading diagram…
</div>
) : null}
{svg ? (
<div
className={styles.content}
dangerouslySetInnerHTML={{ __html: svg }}
/>
) : null}
</div>
);
}
|