/
Visualiser.tsx
313 lines (292 loc) · 8.82 KB
/
Visualiser.tsx
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import { Box, Button, ButtonGroup, Flex, Icon } from "@chakra-ui/react";
import Dagre from "@dagrejs/dagre";
import { RemoveFunctions } from "kurtosis-ui-components";
import { forwardRef, useCallback, useEffect, useImperativeHandle, useRef } from "react";
import { FiShare2 } from "react-icons/fi";
import {
Background,
BackgroundVariant,
Controls,
Edge,
Node,
ReactFlow,
useEdgesState,
useNodesState,
useReactFlow,
XYPosition,
} from "reactflow";
import { v4 as uuidv4 } from "uuid";
import { EnclaveFullInfo } from "../../types";
import { KurtosisArtifactNode } from "./KurtosisArtifactNode";
import { nodeIcons } from "./KurtosisNode";
import { KurtosisPythonNode } from "./KurtosisPythonNode";
import { KurtosisServiceNode } from "./KurtosisServiceNode";
import { KurtosisShellNode } from "./KurtosisShellNode";
import { generateStarlarkFromGraph, getNodeDependencies } from "./utils";
import { useVariableContext } from "./VariableContextProvider";
import "./Visualiser.css";
const g = new Dagre.graphlib.Graph().setDefaultEdgeLabel(() => ({}));
const getLayoutedElements = <T extends object>(nodes: Node<T>[], edges: Edge<any>[]) => {
if (nodes.length === 0) {
return { nodes, edges };
}
g.setGraph({ rankdir: "LR", ranksep: 100 });
edges.forEach((edge) => g.setEdge(edge.source, edge.target));
nodes.forEach((node) =>
g.setNode(node.id, node as Node<{ label: string }, string | undefined> & { width?: number; height?: number }),
);
Dagre.layout(g);
return {
nodes: nodes.map((node) => {
const { x, y } = g.node(node.id);
return { ...node, position: { x, y } };
}),
edges,
};
};
const nodeTypes = {
serviceNode: KurtosisServiceNode,
artifactNode: KurtosisArtifactNode,
shellNode: KurtosisShellNode,
pythonNode: KurtosisPythonNode,
};
export type VisualiserImperativeAttributes = {
getStarlark: () => string;
};
type VisualiserProps = {
initialNodes: Node<any>[];
initialEdges: Edge<any>[];
existingEnclave?: RemoveFunctions<EnclaveFullInfo>;
};
export const Visualiser = forwardRef<VisualiserImperativeAttributes, VisualiserProps>(
({ initialNodes, initialEdges, existingEnclave }, ref) => {
const { data, updateData } = useVariableContext();
const insertOffset = useRef(0);
const { fitView, addNodes, getViewport } = useReactFlow();
const [nodes, setNodes, onNodesChange] = useNodesState(initialNodes || []);
const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges || []);
const onLayout = useCallback(() => {
const layouted = getLayoutedElements(nodes, edges);
setNodes([...layouted.nodes]);
setEdges([...layouted.edges]);
window.requestAnimationFrame(() => {
fitView();
});
}, [nodes, edges, fitView, setEdges, setNodes]);
const getNewNodePosition = (): XYPosition => {
const viewport = getViewport();
insertOffset.current += 1;
return { x: -viewport.x + insertOffset.current * 20 + 400, y: -viewport.y + insertOffset.current * 20 };
};
const handleAddServiceNode = () => {
const id = uuidv4();
updateData(id, {
type: "service",
serviceName: "",
image: {
image: "",
type: "image",
buildContextDir: "",
flakeLocationDir: "",
flakeOutput: "",
registry: "",
registryPassword: "",
registryUsername: "",
targetStage: "",
},
ports: [],
env: [],
files: [],
execStepEnabled: "false",
execStepCommand: "",
execStepAcceptableCodes: [],
isValid: false,
});
addNodes({
id,
position: getNewNodePosition(),
width: 650,
style: { width: "650px" },
type: "serviceNode",
data: {},
});
};
const handleAddArtifactNode = () => {
const id = uuidv4();
updateData(id, { type: "artifact", artifactName: "", files: {}, isValid: false });
addNodes({
id,
position: getNewNodePosition(),
width: 400,
style: { width: "400px" },
type: "artifactNode",
data: {},
});
};
const handleAddShellNode = () => {
const id = uuidv4();
updateData(id, {
type: "shell",
shellName: "",
command: "",
image: {
image: "",
type: "image",
buildContextDir: "",
flakeLocationDir: "",
flakeOutput: "",
registry: "",
registryPassword: "",
registryUsername: "",
targetStage: "",
},
env: [],
files: [],
store: "",
wait_enabled: "true",
wait: "",
isValid: false,
});
addNodes({
id,
position: getNewNodePosition(),
width: 650,
style: { width: "650px" },
type: "shellNode",
data: {},
});
};
const handleAddPythonNode = () => {
const id = uuidv4();
updateData(id, {
type: "python",
pythonName: "",
command: "",
packages: [],
image: {
image: "",
type: "image",
buildContextDir: "",
flakeLocationDir: "",
flakeOutput: "",
registry: "",
registryPassword: "",
registryUsername: "",
targetStage: "",
},
args: [],
files: [],
store: "",
wait_enabled: "true",
wait: "",
isValid: false,
});
addNodes({
id,
position: getNewNodePosition(),
width: 650,
style: { width: "650px" },
type: "pythonNode",
data: {},
});
};
const handleNodeDoubleClick = useCallback(
(e: React.MouseEvent, node: Node) => {
fitView({ nodes: [node], maxZoom: 1, duration: 500 });
},
[fitView],
);
useEffect(() => {
setEdges((prevState) => {
return Object.entries(getNodeDependencies(data)).flatMap(([to, froms]) =>
[...froms].map((from) => ({
id: `${from}-${to}`,
source: from,
target: to,
animated: true,
type: "straight",
style: { strokeWidth: "3px" },
})),
);
});
}, [setEdges, data]);
// Remove the resizeObserver error
useEffect(() => {
const errorHandler = (e: any) => {
if (
e.message.includes(
"ResizeObserver loop completed with undelivered notifications" || "ResizeObserver loop limit exceeded",
)
) {
const resizeObserverErr = document.getElementById("webpack-dev-server-client-overlay");
if (resizeObserverErr) {
resizeObserverErr.style.display = "none";
}
}
};
window.addEventListener("error", errorHandler);
return () => {
window.removeEventListener("error", errorHandler);
};
}, []);
useImperativeHandle(
ref,
() => ({
getStarlark: () => {
return generateStarlarkFromGraph(nodes, edges, data, existingEnclave);
},
}),
[nodes, edges, data, existingEnclave],
);
return (
<Flex position="relative" flexDirection={"column"} h={"100%"} gap={"8px"}>
<Box
borderRadius={"5px"}
position={"absolute"}
zIndex={"99999"}
top={"20px"}
left={"20px"}
bg={"gray.800"}
p={"8px"}
>
<ButtonGroup size={"sm"}>
<Button leftIcon={<Icon as={FiShare2} />} onClick={onLayout}>
Auto-Layout
</Button>
<Button leftIcon={<Icon as={nodeIcons["service"]} />} onClick={handleAddServiceNode}>
Add Service Node
</Button>
<Button leftIcon={<Icon as={nodeIcons["artifact"]} />} onClick={handleAddArtifactNode}>
Add Files Node
</Button>
<Button leftIcon={<Icon as={nodeIcons["shell"]} />} onClick={handleAddShellNode}>
Add Shell Node
</Button>
<Button leftIcon={<Icon as={nodeIcons["python"]} />} onClick={handleAddPythonNode}>
Add Python Node
</Button>
</ButtonGroup>
</Box>
<Box bg={"gray.900"} flex={"1"}>
<ReactFlow
minZoom={0.1}
maxZoom={1}
nodes={nodes}
edges={edges}
proOptions={{ hideAttribution: true }}
onMove={() => (insertOffset.current = 1)}
onNodesChange={onNodesChange}
onEdgesChange={onEdgesChange}
onNodeDoubleClick={handleNodeDoubleClick}
nodeTypes={nodeTypes}
fitView
>
<Controls />
<Background variant={BackgroundVariant.Dots} gap={12} size={1} />
</ReactFlow>
</Box>
</Flex>
);
},
);
Visualiser.displayName = "ForwardRef Visualiser";