-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlowCanvas.tsx
More file actions
89 lines (80 loc) · 2.13 KB
/
FlowCanvas.tsx
File metadata and controls
89 lines (80 loc) · 2.13 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
import React, { useRef, useState } from 'react';
import ReactFlow, {
MiniMap,
Controls,
Background,
ReactFlowInstance,
Node,
Edge,
} from 'reactflow';
import 'reactflow/dist/style.css';
import { useFlowState } from '../../hooks/useFlowState';
import { useDragAndDrop } from '../../hooks/useDragAndDrop';
import { EditableNode } from './EditableNode';
const nodeTypes = { editable: EditableNode };
interface FlowCanvasProps {
onDeploy?: (nodes: Node[], edges: Edge[]) => void;
sidebarOpen?: boolean;
}
export function FlowCanvas({ onDeploy, sidebarOpen = true }: FlowCanvasProps) {
const reactFlowWrapper = useRef<HTMLDivElement>(null);
const [reactFlowInstance, setReactFlowInstance] = useState<ReactFlowInstance | null>(null);
const {
nodes,
edges,
onNodesChange,
onEdgesChange,
onConnect,
addNode,
updateNodeLabel,
} = useFlowState();
const { onDrop, onDragOver } = useDragAndDrop({
reactFlowInstance,
reactFlowWrapper,
addNode,
});
const handleLabelChange = (id: string, newLabel: string) => {
updateNodeLabel(id, newLabel);
};
// const handleDeploy = () => {
// if (onDeploy) {
// onDeploy(nodes, edges);
// } else {
// console.log('DEPLOY:', { nodes, edges });
// alert('Model data printed to console!\n(Next step: send to Vitruvius backend)');
// }
// };
const containerStyle: React.CSSProperties = {
flexGrow: 1,
height: '100%',
width: '100%',
position: 'relative',
};
return (
<div
ref={reactFlowWrapper}
style={containerStyle}
>
<ReactFlow
nodes={nodes.map(node => ({
...node,
data: { ...node.data, onLabelChange: handleLabelChange }
}))}
edges={edges}
onNodesChange={onNodesChange}
onEdgesChange={onEdgesChange}
onConnect={onConnect}
fitView
onDrop={onDrop}
onDragOver={onDragOver}
onInit={setReactFlowInstance}
nodeTypes={nodeTypes}
style={{ width: '100%', height: '100%' }}
>
<MiniMap />
<Controls />
<Background />
</ReactFlow>
</div>
);
}