forked from LangGraph-GUI/LangGraph-GUI-reactflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConditionEdge.js
62 lines (53 loc) · 1.59 KB
/
ConditionEdge.js
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
// ConditionEdge.js
import { addEdge } from 'reactflow';
export const createConditionEdge = (edges, setEdges, params, nodes, setNodes) => {
const { source, target, sourceHandle } = params;
const newEdge = {
...params,
id: `e${source}-${target}`,
animated: false,
style: { stroke: sourceHandle === 'true' ? 'green' : 'red', strokeWidth: 2 },
markerEnd: {
type: 'arrowclosed',
width: 20,
height: 20,
},
};
setEdges((eds) => addEdge(newEdge, eds));
setNodes((nds) =>
nds.map((node) => {
if (node.id === source) {
const newData = {
...node.data,
...(sourceHandle === 'true' ? { true_next: target } : { false_next: target })
};
return { ...node, data: newData };
} else if (node.id === target) {
return { ...node, prevs: [...(node.prevs || []), source] };
}
return node;
})
);
return newEdge;
};
export const deleteConditionEdge = (edges, setEdges, edgeId, nodes, setNodes) => {
const edge = edges.find((e) => e.id === edgeId);
if (!edge) return;
setEdges((eds) => eds.filter((edge) => edge.id !== edgeId));
setNodes((nds) =>
nds.map((node) => {
if (node.id === edge.source) {
const newData = {
...node.data,
...(edge.sourceHandle === 'true'
? { true_next: null }
: { false_next: null })
};
return { ...node, data: newData };
} else if (node.id === edge.target) {
return { ...node, prevs: node.prevs.filter((id) => id !== edge.source) };
}
return node;
})
);
};