|
2 | 2 |
|
3 | 3 | import React, {useCallback, useState} from "react";
|
4 | 4 | import {
|
5 |
| - addEdge, |
6 |
| - Background, |
7 |
| - BackgroundVariant, |
8 |
| - Controls, |
9 |
| - MiniMap, |
10 |
| - ReactFlow, |
11 |
| - useEdgesState, |
12 |
| - useNodesState, |
| 5 | + addEdge, |
| 6 | + Background, |
| 7 | + BackgroundVariant, |
| 8 | + Controls, |
| 9 | + MiniMap, |
| 10 | + ReactFlow, |
| 11 | + useEdgesState, |
| 12 | + useNodesState, |
13 | 13 | } from "reactflow";
|
14 | 14 |
|
15 | 15 | import "reactflow/dist/style.css";
|
16 | 16 | import {v4 as uuidv4} from "uuid";
|
17 | 17 | import {Button} from "@/components/ui/button";
|
18 | 18 |
|
19 | 19 | const initialNodes = [
|
20 |
| - {id: "1", position: {x: 50, y: 50}, data: {label: "Node 1"}}, |
21 |
| - {id: "2", position: {x: 50, y: 200}, data: {label: "Node 2"}}, |
| 20 | + {id: "1", position: {x: 50, y: 50}, data: {label: "Node 1"}}, |
| 21 | + {id: "2", position: {x: 50, y: 200}, data: {label: "Node 2"}}, |
22 | 22 | ];
|
23 | 23 | const initialEdges = [{id: "e1-2", source: "1", target: "2", animated: true}];
|
24 | 24 |
|
25 | 25 | export default function Page() {
|
26 |
| - const [nodes, setNodes, onNodesChange] = useNodesState(initialNodes); |
27 |
| - const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges); |
28 |
| - const [selectedNode, setSelectedNode] = useState(null); |
| 26 | + const [nodes, setNodes, onNodesChange] = useNodesState(initialNodes); |
| 27 | + const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges); |
| 28 | + const [selectedNode, setSelectedNode] = useState(null); |
29 | 29 |
|
30 |
| - const onConnect = useCallback( |
31 |
| - (params: any) => |
32 |
| - setEdges((eds) => addEdge({...params, animated: true}, eds)), |
33 |
| - [setEdges], |
34 |
| - ); |
| 30 | + const onConnect = useCallback( |
| 31 | + (params: any) => |
| 32 | + setEdges((eds) => addEdge({...params, animated: true}, eds)), |
| 33 | + [setEdges], |
| 34 | + ); |
35 | 35 |
|
36 |
| - const onNodeSelect = useCallback((event: any, node: any) => { |
37 |
| - setSelectedNode(node); |
38 |
| - }, []); |
| 36 | + const onNodeSelect = useCallback((event: any, node: any) => { |
| 37 | + setSelectedNode(node); |
| 38 | + }, []); |
39 | 39 |
|
40 |
| - const addNode = useCallback(() => { |
41 |
| - const newNodeId = uuidv4(); |
42 |
| - const newNode = { |
43 |
| - id: newNodeId, |
44 |
| - data: {label: `Node ${nodes.length + 1}`}, |
45 |
| - position: { |
46 |
| - x: Math.random() * 400, |
47 |
| - y: Math.random() * 400, |
48 |
| - }, |
49 |
| - }; |
50 |
| - setNodes((prevNodes) => [...prevNodes, newNode]); |
| 40 | + const addNode = useCallback(() => { |
| 41 | + const newNodeId = uuidv4(); |
| 42 | + const newNode = { |
| 43 | + id: newNodeId, |
| 44 | + data: {label: `Node ${nodes.length + 1}`}, |
| 45 | + position: { |
| 46 | + x: Math.random() * 400, |
| 47 | + y: Math.random() * 400, |
| 48 | + }, |
| 49 | + }; |
| 50 | + setNodes((prevNodes) => [...prevNodes, newNode]); |
51 | 51 |
|
52 |
| - if (nodes.length > 0) { |
53 |
| - const newEdge = { |
54 |
| - id: `e${nodes[nodes.length - 1].id}-${newNodeId}`, |
55 |
| - source: nodes[nodes.length - 1].id, |
56 |
| - target: newNodeId, |
57 |
| - }; |
58 |
| - setEdges((prevEdges) => |
59 |
| - addEdge({...newEdge, animated: true}, prevEdges), |
60 |
| - ); |
61 |
| - } |
62 |
| - }, [nodes, setNodes, setEdges]); |
| 52 | + if (nodes.length > 0) { |
| 53 | + const newEdge = { |
| 54 | + id: `e${nodes[nodes.length - 1].id}-${newNodeId}`, |
| 55 | + source: nodes[nodes.length - 1].id, |
| 56 | + target: newNodeId, |
| 57 | + }; |
| 58 | + setEdges((prevEdges) => |
| 59 | + addEdge({...newEdge, animated: true}, prevEdges), |
| 60 | + ); |
| 61 | + } |
| 62 | + }, [nodes, setNodes, setEdges]); |
63 | 63 |
|
64 |
| - const clearAll = () => { |
65 |
| - setNodes([]); |
66 |
| - setEdges([]); |
67 |
| - setSelectedNode(null); |
68 |
| - }; |
| 64 | + const clearAll = () => { |
| 65 | + setNodes([]); |
| 66 | + setEdges([]); |
| 67 | + setSelectedNode(null); |
| 68 | + }; |
69 | 69 |
|
70 |
| - const deleteSelectedNode = useCallback(() => { |
71 |
| - if (selectedNode) { |
72 |
| - setNodes((nds) => |
73 |
| - nds.filter( |
74 |
| - (node) => (node as any).id !== (selectedNode as any).id, |
75 |
| - ), |
76 |
| - ); |
77 |
| - setEdges((eds) => |
78 |
| - eds.filter( |
79 |
| - (edge) => |
80 |
| - edge.source !== (selectedNode as any).id && |
81 |
| - edge.target !== (selectedNode as any).id, |
82 |
| - ), |
83 |
| - ); |
84 |
| - setSelectedNode(null); |
85 |
| - } |
86 |
| - }, [selectedNode, setNodes, setEdges]); |
| 70 | + const deleteSelectedNode = useCallback(() => { |
| 71 | + if (selectedNode) { |
| 72 | + setNodes((nds) => |
| 73 | + nds.filter( |
| 74 | + (node) => (node as any).id !== (selectedNode as any).id, |
| 75 | + ), |
| 76 | + ); |
| 77 | + setEdges((eds) => |
| 78 | + eds.filter( |
| 79 | + (edge) => |
| 80 | + edge.source !== (selectedNode as any).id && |
| 81 | + edge.target !== (selectedNode as any).id, |
| 82 | + ), |
| 83 | + ); |
| 84 | + setSelectedNode(null); |
| 85 | + } |
| 86 | + }, [selectedNode, setNodes, setEdges]); |
87 | 87 |
|
88 |
| - return ( |
89 |
| - <div className="h-screen w-screen"> |
90 |
| - <div className="absolute left-0 top-0 z-10 space-x-2 p-2"> |
91 |
| - <Button onClick={addNode}>Add Node</Button> |
92 |
| - <Button onClick={clearAll}>Clear All</Button> |
93 |
| - {selectedNode && ( |
94 |
| - <Button |
95 |
| - onClick={deleteSelectedNode} |
96 |
| - variant="destructive" |
97 |
| - className="ml-2" |
| 88 | + return ( |
| 89 | + <div className="h-screen w-screen"> |
| 90 | + <div className="absolute left-0 top-0 z-10 space-x-2 p-2"> |
| 91 | + <Button onClick={addNode}>Add Node</Button> |
| 92 | + <Button onClick={clearAll}>Clear All</Button> |
| 93 | + {selectedNode && ( |
| 94 | + <Button |
| 95 | + onClick={deleteSelectedNode} |
| 96 | + variant="destructive" |
| 97 | + className="ml-2" |
| 98 | + > |
| 99 | + Delete Node |
| 100 | + </Button> |
| 101 | + )} |
| 102 | + </div> |
| 103 | + <ReactFlow |
| 104 | + nodes={nodes} |
| 105 | + edges={edges} |
| 106 | + onNodesChange={onNodesChange} |
| 107 | + onEdgesChange={onEdgesChange} |
| 108 | + onConnect={onConnect} |
| 109 | + onNodeClick={onNodeSelect} |
98 | 110 | >
|
99 |
| - Delete Node |
100 |
| - </Button> |
101 |
| - )} |
| 111 | + <Controls/> |
| 112 | + <MiniMap/> |
| 113 | + <Background |
| 114 | + variant={BackgroundVariant.Dots} |
| 115 | + gap={12} |
| 116 | + size={1} |
| 117 | + /> |
| 118 | + </ReactFlow> |
102 | 119 | </div>
|
103 |
| - <ReactFlow |
104 |
| - nodes={nodes} |
105 |
| - edges={edges} |
106 |
| - onNodesChange={onNodesChange} |
107 |
| - onEdgesChange={onEdgesChange} |
108 |
| - onConnect={onConnect} |
109 |
| - onNodeClick={onNodeSelect} |
110 |
| - > |
111 |
| - <Controls/> |
112 |
| - <MiniMap/> |
113 |
| - <Background |
114 |
| - variant={BackgroundVariant.Dots} |
115 |
| - gap={12} |
116 |
| - size={1} |
117 |
| - /> |
118 |
| - </ReactFlow> |
119 |
| - </div> |
120 |
| - ); |
| 120 | + ); |
121 | 121 | }
|
0 commit comments