-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathCustomTest.tsx
More file actions
128 lines (115 loc) · 3.68 KB
/
CustomTest.tsx
File metadata and controls
128 lines (115 loc) · 3.68 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
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
import { useState, useEffect } from "react";
import { MermaidDiagram } from "./MermaidDiagram.tsx";
import type { ActiveTestCaseIndex, MermaidData } from "./index.tsx";
import { usePersistedSectionState } from "./usePersistedSectionState.ts";
interface CustomTestProps {
onChange: (
definition: MermaidData["definition"],
activeTestCaseIndex: ActiveTestCaseIndex
) => void;
mermaidData: MermaidData;
activeTestCaseIndex: ActiveTestCaseIndex;
}
const STORAGE_KEY = "mermaid-to-excalidraw-definition";
const CustomTest = ({
onChange,
mermaidData,
activeTestCaseIndex,
}: CustomTestProps) => {
const isActive = activeTestCaseIndex === "custom";
const {
isExpanded: isParsedDataExpanded,
handleToggle: handleParsedDataToggle,
} = usePersistedSectionState("custom:parsed-data");
const [textareaValue, setTextareaValue] = useState(() => {
// Load from localStorage on initial mount
try {
return localStorage.getItem(STORAGE_KEY) || "";
} catch {
return "";
}
});
// Update textarea when mermaidData changes from external source
useEffect(() => {
if (mermaidData.definition && activeTestCaseIndex !== "custom") {
setTextareaValue(mermaidData.definition);
}
}, [mermaidData.definition, activeTestCaseIndex]);
return (
<>
<form
className="custom-test-form"
onSubmit={(e) => {
e.preventDefault();
const formData = new FormData(e.target as HTMLFormElement);
const definition = formData.get("mermaid-input")?.toString() || "";
// Save to localStorage
try {
localStorage.setItem(STORAGE_KEY, definition);
} catch (error) {
console.error("Failed to save to localStorage:", error);
}
onChange(definition, "custom");
}}
>
<label className="field-label" htmlFor="mermaid-input">
{"Mermaid definition"}
</label>
<textarea
id="mermaid-input"
rows={10}
cols={50}
name="mermaid-input"
value={textareaValue}
onChange={(e) => {
const value = e.target.value;
setTextareaValue(value);
if (!isActive) {
return;
}
onChange(value, "custom");
}}
placeholder="Paste or type Mermaid syntax here"
/>
<div className="custom-test-actions">
<button
className="playground-button"
type="submit"
id="render-excalidraw-btn"
>
{"Render to Excalidraw"}
</button>
<p className="custom-test-hint">
{"The live Excalidraw canvas updates on the right."}
</p>
</div>
</form>
{isActive && (
<>
<section className="custom-preview-card">
<div className="preview-badge">{"Live Mermaid SVG"}</div>
<div className="diagram-preview-surface custom-diagram-surface">
<MermaidDiagram definition={textareaValue} id="custom-diagram" />
</div>
</section>
<details
id="parsed-data-details"
open={isParsedDataExpanded}
onToggle={handleParsedDataToggle}
>
<summary>{"Parsed data from parseMermaid"}</summary>
{isParsedDataExpanded ? (
<>
<pre id="custom-parsed-data">
{JSON.stringify(mermaidData.output, null, 2)}
</pre>
{mermaidData.error && <div id="error">{mermaidData.error}</div>}
</>
) : null}
</details>
</>
)}
</>
);
};
export default CustomTest;