-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlowchart.jsx
More file actions
56 lines (47 loc) · 1.44 KB
/
Flowchart.jsx
File metadata and controls
56 lines (47 loc) · 1.44 KB
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
// src/Flowchart.jsx
import React, { useEffect, useRef, useState } from "react";
import mermaid from "mermaid";
import { CodeToMermaid } from "./Codetomermaid";
import { Box, Text } from "@chakra-ui/react";
// Initialize mermaid once globally
mermaid.initialize({ startOnLoad: false, theme: "dark" });
const Flowchart = ({ code }) => {
const containerRef = useRef(null);
const [error, setError] = useState(null);
useEffect(() => {
let isCancelled = false;
const handle = setTimeout(() => {
(async () => {
try {
const diagram = CodeToMermaid(code || "");
const id = "flow_" + Math.random().toString(36).slice(2, 9);
const { svg } = await mermaid.render(id, diagram);
if (!isCancelled && containerRef.current) {
containerRef.current.innerHTML = svg;
}
setError(null);
} catch (err) {
if (!isCancelled) {
setError(err.message || String(err));
if (containerRef.current) containerRef.current.innerHTML = "";
}
}
})();
}, 350); // debounce
return () => {
isCancelled = true;
clearTimeout(handle);
};
}, [code]);
return (
<Box>
{error && (
<Text color="red.400" mb={2}>
Parse error: {error}
</Text>
)}
<div ref={containerRef} />
</Box>
);
};
export default Flowchart;