Skip to content

Commit f52fade

Browse files
Add example dropdown
1 parent 5e5301e commit f52fade

File tree

6 files changed

+71
-21
lines changed

6 files changed

+71
-21
lines changed

.gitmodules

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[submodule "examples/extraction-gym"]
2+
path = examples/extraction-gym
3+
url = https://github.com/egraphs-good/extraction-gym
4+
[submodule "examples/egraph-serialize"]
5+
path = examples/egraph-serialize
6+
url = https://github.com/egraphs-good/egraph-serialize

examples/egraph-serialize

Submodule egraph-serialize added at 5838c03

examples/extraction-gym

Submodule extraction-gym added at ac30499

src/Monaco.tsx

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,64 @@
1+
/// <reference types="react/canary" />
2+
3+
import { startTransition, use, useEffect, useMemo, useState } from "react";
14
import MonacoEditor from "react-monaco-editor";
25

6+
const modules = {
7+
...import.meta.glob("/examples/egraph-serialize/tests/*.json"),
8+
...import.meta.glob("/examples/extraction-gym/data/*/*.json"),
9+
...import.meta.glob("/examples/extraction-gym/test-data/*/*.json"),
10+
};
11+
312
function Monaco({ code, setCode }: { code: string; setCode: (code: string) => void }) {
13+
const [selectedPreset, setSelectedPreset] = useState("");
14+
const [loadPreset, setLoadPreset] = useState(false);
15+
const handlePresetChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
16+
const preset = event.target.value;
17+
startTransition(() => {
18+
setSelectedPreset(preset);
19+
setLoadPreset(true);
20+
});
21+
};
22+
const presetPromise = useMemo(() => (loadPreset ? modules[selectedPreset]() : null), [selectedPreset, loadPreset]);
23+
const loadedPreset = presetPromise ? use(presetPromise) : null;
24+
25+
useEffect(() => {
26+
if (loadedPreset) {
27+
startTransition(() => {
28+
setCode(JSON.stringify(loadedPreset, null, 2));
29+
setLoadPreset(false);
30+
});
31+
}
32+
}, [loadedPreset, setCode, setLoadPreset]);
33+
434
return (
5-
<MonacoEditor
6-
language="json"
7-
theme="vs-dark"
8-
value={code}
9-
onChange={setCode}
10-
/// include all default props bc they are now deprecated
11-
width="100%"
12-
height="100%"
13-
defaultValue=""
14-
options={{}}
15-
overrideServices={{}}
16-
editorWillMount={() => {}}
17-
editorDidMount={() => {}}
18-
editorWillUnmount={() => {}}
19-
className={null}
20-
/>
35+
<div className="flex flex-col h-full w-full">
36+
<select value={selectedPreset} onChange={handlePresetChange} className="m-1 p-2 border border-gray-300 rounded">
37+
<option value="" disabled>
38+
Select a preset
39+
</option>
40+
{Object.keys(modules).map((preset) => (
41+
<option key={preset} value={preset}>
42+
{preset}
43+
</option>
44+
))}
45+
</select>
46+
<MonacoEditor
47+
language="json"
48+
theme="vs-dark"
49+
value={code}
50+
onChange={setCode}
51+
width="100%"
52+
height="100%"
53+
defaultValue=""
54+
options={{}}
55+
overrideServices={{}}
56+
editorWillMount={() => {}}
57+
editorDidMount={() => {}}
58+
editorWillUnmount={() => {}}
59+
className={null}
60+
/>
61+
</div>
2162
);
2263
}
2364

src/Visualizer.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ type EGraphClassData = {
6363
};
6464
type EGraph = {
6565
nodes: { [id: EGraphNodeID]: EGraphNode };
66-
root_eclasses: EGraphClassID[];
67-
class_data: { [id: EGraphClassID]: EGraphClassData };
66+
root_eclasses?: EGraphClassID[];
67+
class_data?: { [id: EGraphClassID]: EGraphClassData };
6868
};
6969

7070
type Color = string;
@@ -154,9 +154,9 @@ function toELKNode(
154154
}
155155
}
156156
}
157-
157+
const class_data = egraph.class_data || {};
158158
const type_to_color = new Map<string | undefined, Color>();
159-
for (const { type } of Object.values(egraph.class_data)) {
159+
for (const { type } of Object.values(class_data)) {
160160
if (!type_to_color.has(type)) {
161161
type_to_color.set(type, colorScheme[type_to_color.size % colorScheme.length]);
162162
}
@@ -165,7 +165,7 @@ function toELKNode(
165165
const children = [...classToNodes.entries()].map(([id, nodes]) => {
166166
return {
167167
id: `class-${id}`,
168-
data: { color: type_to_color.get(egraph.class_data[id]?.type) || null, port: `port-${id}`, id },
168+
data: { color: type_to_color.get(class_data[id]?.type) || null, port: `port-${id}`, id },
169169
type: "class" as const,
170170
children: nodes.map(([id, node]) => {
171171
// compute the size of the text by setting a dummy node element then measureing it

src/examples.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const examples = import.meta.glob("/examples/egraph-serialize/tests/*.json");

0 commit comments

Comments
 (0)