Spaces:
Configuration error
Configuration error
zack
commited on
Commit
·
4e25bbe
1
Parent(s):
664b836
chore: remove src/components/ReactFlow/ReactFlowEnv.js 🧹
Browse files
frontend/src/components/ReactFlow/ReactFlowEnv.js
DELETED
@@ -1,183 +0,0 @@
|
|
1 |
-
import CustomNodeIframe from "../Nodes/Custom.js";
|
2 |
-
import '../../css/dist/output.css'
|
3 |
-
import ReactFlow, { Background,
|
4 |
-
applyNodeChanges,
|
5 |
-
ReactFlowProvider,
|
6 |
-
addEdge,
|
7 |
-
updateEdge,
|
8 |
-
applyEdgeChanges,
|
9 |
-
Controls,
|
10 |
-
MarkerType
|
11 |
-
} from 'react-flow-renderer';
|
12 |
-
import React ,{ useState, useCallback, useRef, useEffect } from 'react';
|
13 |
-
import Navbar from '../Navagation/navbar';
|
14 |
-
import CustomEdge from '../Edges/Custom'
|
15 |
-
import CustomLine from "../Edges/CustomLine.js";
|
16 |
-
import { useThemeDetector } from '../../helper/visual'
|
17 |
-
import {CgMoreVerticalAlt} from 'react-icons/cg'
|
18 |
-
import {BsFillEraserFill} from 'react-icons/bs'
|
19 |
-
import {FaRegSave} from 'react-icons/fa'
|
20 |
-
|
21 |
-
const NODE = {
|
22 |
-
custom : CustomNodeIframe,
|
23 |
-
}
|
24 |
-
|
25 |
-
const EDGE = {
|
26 |
-
custom : CustomEdge
|
27 |
-
}
|
28 |
-
|
29 |
-
|
30 |
-
export default function ReactEnviorment() {
|
31 |
-
|
32 |
-
const [theme, setTheme] = useState(useThemeDetector)
|
33 |
-
const [nodes, setNodes] = useState([]);
|
34 |
-
const [edges, setEdges] = useState([])
|
35 |
-
const [reactFlowInstance, setReactFlowInstance] = useState(null);
|
36 |
-
const reactFlowWrapper = useRef(null);
|
37 |
-
const [tool, setTool] = useState(false)
|
38 |
-
|
39 |
-
const deleteNodeContains = (id) =>{setNodes((nds) => nds.filter(n => !n.id.includes(`${id}-`) ))}
|
40 |
-
const deleteEdge = useCallback((id) => setEdges((eds) => eds.filter(e => e.id !== id)), [setEdges])
|
41 |
-
const deleteNode = useCallback((id) =>{
|
42 |
-
setNodes(() => nodes.filter(n => n.id !== id ))
|
43 |
-
}, [setNodes, nodes])
|
44 |
-
|
45 |
-
|
46 |
-
useEffect(() => {
|
47 |
-
const restore = () => {
|
48 |
-
const flow = JSON.parse(localStorage.getItem('flowkey'));
|
49 |
-
|
50 |
-
if(flow){
|
51 |
-
flow.nodes.map((nds) => nds.data.delete = deleteNode)
|
52 |
-
flow.edges.map((eds) => eds.data.delete = deleteEdge)
|
53 |
-
setNodes(flow.nodes || [])
|
54 |
-
setEdges(flow.edges || [])
|
55 |
-
console.log(flow)
|
56 |
-
}
|
57 |
-
}
|
58 |
-
restore()
|
59 |
-
},[deleteNode, deleteEdge])
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
const onNodesChange = useCallback(
|
64 |
-
(changes) => setNodes((nds) => applyNodeChanges(changes, nds)),
|
65 |
-
[setNodes]
|
66 |
-
);
|
67 |
-
|
68 |
-
const onEdgesChange = useCallback(
|
69 |
-
(changes) => setEdges((eds) => applyEdgeChanges(changes, eds)),
|
70 |
-
[setEdges]
|
71 |
-
);
|
72 |
-
|
73 |
-
const onEdgeUpdate = useCallback(
|
74 |
-
(oldEdge, newConnection) => setEdges((els) => updateEdge(oldEdge, newConnection, els)),
|
75 |
-
[]
|
76 |
-
);
|
77 |
-
|
78 |
-
const onConnect = useCallback(
|
79 |
-
(params) => {
|
80 |
-
console.log(params)
|
81 |
-
setEdges((els) => addEdge({...params, type: "custom", animated : true, style : {stroke : "#00FF4A", strokeWidth : "6"}, markerEnd: {type: MarkerType.ArrowClosed, color : "#00FF4A"}, data : { delete : deleteEdge}}, els))
|
82 |
-
fetch("http://localhost:2000/api/append/connection", {method : "POST", mode : 'cors', headers : { 'Content-Type' : 'application/json' }, body: JSON.stringify({"source": params.source, "target" : params.target})}).then( res => {
|
83 |
-
console.log(res)
|
84 |
-
}).catch(error => {
|
85 |
-
console.log(error)
|
86 |
-
})
|
87 |
-
},
|
88 |
-
[setEdges, deleteEdge]
|
89 |
-
);
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
const onDragOver = useCallback((event) => {
|
94 |
-
event.preventDefault();
|
95 |
-
event.dataTransfer.dropEffect = 'move';
|
96 |
-
}, []);
|
97 |
-
|
98 |
-
|
99 |
-
const onSave = useCallback(() => {
|
100 |
-
if (reactFlowInstance) {
|
101 |
-
const flow = reactFlowInstance.toObject();
|
102 |
-
alert("The current nodes have been saved into the localstorage 💾")
|
103 |
-
localStorage.setItem('flowkey', JSON.stringify(flow));
|
104 |
-
var labels = [];
|
105 |
-
var colour = [];
|
106 |
-
var emoji = [];
|
107 |
-
for(let i = 0; i < flow.nodes.length; i++){
|
108 |
-
if (!labels.includes(flow.nodes[i].data.label))
|
109 |
-
colour.push(flow.nodes[i].data.colour)
|
110 |
-
emoji.push(flow.nodes[i].data.emoji)
|
111 |
-
labels.push(flow.nodes[i].data.label)
|
112 |
-
}
|
113 |
-
localStorage.setItem('colour',JSON.stringify(colour))
|
114 |
-
localStorage.setItem('emoji', JSON.stringify(emoji))
|
115 |
-
}
|
116 |
-
}, [reactFlowInstance]);
|
117 |
-
|
118 |
-
const onErase = useCallback(() => {
|
119 |
-
const flow = localStorage.getItem("flowkey")
|
120 |
-
if (reactFlowInstance && flow){
|
121 |
-
alert("The current nodes have been erased from the localstorage")
|
122 |
-
localStorage.removeItem("flowkey")
|
123 |
-
localStorage.removeItem('colour')
|
124 |
-
localStorage.removeItem('emoji')
|
125 |
-
}
|
126 |
-
},[reactFlowInstance])
|
127 |
-
|
128 |
-
|
129 |
-
const onDrop = useCallback(
|
130 |
-
(event) => {
|
131 |
-
event.preventDefault();
|
132 |
-
|
133 |
-
if(event.dataTransfer.getData('application/reactflow') !== ""){
|
134 |
-
const reactFlowBounds = reactFlowWrapper.current.getBoundingClientRect();
|
135 |
-
const type = event.dataTransfer.getData('application/reactflow');
|
136 |
-
const item = JSON.parse(event.dataTransfer.getData('application/item'));
|
137 |
-
const style = JSON.parse(event.dataTransfer.getData('application/style'));
|
138 |
-
// check if the dropped element is valid
|
139 |
-
if (typeof type === 'undefined' || !type) {
|
140 |
-
return;
|
141 |
-
}
|
142 |
-
|
143 |
-
const position = reactFlowInstance.project({
|
144 |
-
x: event.clientX - reactFlowBounds.left,
|
145 |
-
y: event.clientY - reactFlowBounds.top,
|
146 |
-
});
|
147 |
-
|
148 |
-
const newNode = {
|
149 |
-
id: `${item.name}-${nodes.length+1}`,
|
150 |
-
type,
|
151 |
-
position,
|
152 |
-
dragHandle : `#draggable`,
|
153 |
-
data: { label: `${item.name}`, host : `${item.host}`, colour : `${style.colour}`, emoji : `${style.emoji}`, delete : deleteNode },
|
154 |
-
};
|
155 |
-
|
156 |
-
setNodes((nds) => nds.concat(newNode));
|
157 |
-
}
|
158 |
-
},
|
159 |
-
[reactFlowInstance, nodes, deleteNode]);
|
160 |
-
|
161 |
-
return (
|
162 |
-
<div className={`${theme ? "dark" : ""}`}>
|
163 |
-
<div className={` absolute text-center ${tool ? "h-[203.3333px]" : "h-[41px]"} overflow-hidden w-[41px] text-4xl top-4 right-5 z-50 cursor-default select-none bg-white dark:bg-stone-900 rounded-full border border-black dark:border-white duration-500`} >
|
164 |
-
<CgMoreVerticalAlt className={` text-black dark:text-white ${tool ? "-rotate-0 mr-auto ml-auto mt-1" : " rotate-180 mr-auto ml-auto mt-1"} duration-300`} onClick={() => setTool(!tool)}/>
|
165 |
-
<h1 title={theme ? 'Dark Mode' : 'Light Mode'} className={`p-4 px-1 pb-0 ${tool ? "visible" : "invisible"} text-3xl`} onClick={() => setTheme(!theme)} >{theme ? '🌙' : '☀️'}</h1>
|
166 |
-
<FaRegSave title="Save" className={`mt-6 text-black dark:text-white ${tool ? "visible" : " invisible"} ml-auto mr-auto `} onClick={() => onSave()}/>
|
167 |
-
<BsFillEraserFill title="Erase" className={`mt-6 text-black dark:text-white ml-auto mr-auto ${tool ? "visible" : " invisible"} `} onClick={() => onErase()}/>
|
168 |
-
</div>
|
169 |
-
<div className={`flex h-screen w-screen ${theme ? "dark" : ""} transition-all`}>
|
170 |
-
<ReactFlowProvider>
|
171 |
-
<Navbar onDelete={deleteNodeContains} colour={JSON.parse(localStorage.getItem('colour'))} emoji={JSON.parse(localStorage.getItem('emoji'))}/>
|
172 |
-
<div className="h-screen w-screen" ref={reactFlowWrapper}>
|
173 |
-
<ReactFlow nodes={nodes} edges={edges} nodeTypes={NODE} edgeTypes={EDGE} onNodesChange={onNodesChange} onNodesDelete={deleteNode} onEdgesChange={onEdgesChange} onEdgeUpdate={onEdgeUpdate} onConnect={onConnect} onDragOver={onDragOver} onDrop={onDrop} onInit={setReactFlowInstance} connectionLineComponent={CustomLine} fitView>
|
174 |
-
<Background variant='dots' size={1} className=" bg-white dark:bg-neutral-800"/>
|
175 |
-
<Controls/>
|
176 |
-
</ReactFlow>
|
177 |
-
</div>
|
178 |
-
</ReactFlowProvider>
|
179 |
-
</div>
|
180 |
-
</div>
|
181 |
-
);
|
182 |
-
}
|
183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|