-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathuseEdgeAdd.ts
More file actions
139 lines (119 loc) · 4.17 KB
/
useEdgeAdd.ts
File metadata and controls
139 lines (119 loc) · 4.17 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
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
import { Edge, Node, useReactFlow } from '@xyflow/react';
import useServiceStore from 'store/new-services.store';
import { Step, StepType } from 'types';
import { generateUniqueId, getNodeLabel } from 'utils/flow-utils';
function useEdgeAdd(id: string) {
const { setEdges, setNodes, getNodes, getNode, getEdge } = useReactFlow();
const handleEdgeClick = (step: Step) => {
const edge = getEdge(id);
if (!edge) return;
const targetNode = getNode(edge.target);
if (!targetNode) return;
const nodeLabel = getNodeLabel(step, getNodes());
const stepType: StepType = step.type;
const newNodeId = generateUniqueId();
const insertNode = {
id: newNodeId,
position: { x: targetNode.position.x, y: targetNode.position.y },
data: {
label: nodeLabel,
onDelete: useServiceStore.getState().onDelete,
onEdit: useServiceStore.getState().handleNodeEdit,
type: [StepType.DynamicChoices, StepType.FinishingStepEnd, StepType.FinishingStepRedirect].includes(stepType)
? 'finishing-step'
: 'step',
stepType: stepType,
readonly: [StepType.Auth, StepType.FinishingStepEnd, StepType.FinishingStepRedirect].includes(stepType),
endpoint: step.data,
setClickedNode: useServiceStore.getState().setClickedNode,
},
className: (() => {
if ([StepType.FinishingStepEnd, StepType.FinishingStepRedirect].includes(stepType)) {
return 'finishing-step';
}
if ([StepType.DynamicChoices].includes(stepType)) {
return 'dynamic-choices';
}
return 'step';
})(),
type: 'custom',
};
const sourceEdge = {
id: `${edge.source}->${newNodeId}`,
source: edge.source,
target: newNodeId,
type: 'step',
label: edge.label,
};
let targetEdge: Edge | null = null;
if (
stepType != StepType.DynamicChoices &&
stepType != StepType.FinishingStepEnd &&
stepType != StepType.FinishingStepRedirect
) {
targetEdge = {
id: `${newNodeId}->${edge.target}`,
source: newNodeId,
target: edge.target,
type: 'step',
animated: getNode(edge.target)?.type === 'ghost',
deletable: getNode(edge.target)?.type != 'ghost',
};
}
let ghostNodes: Node[] = [];
let ghostEdges: Edge[] = [];
if (stepType === StepType.MultiChoiceQuestion || stepType === StepType.Condition || stepType === StepType.Input) {
const labels = stepType === StepType.MultiChoiceQuestion ? ['Jah', 'Ei'] : ['Success', 'Failure'];
ghostNodes = labels.slice(1).map((_) => ({
id: generateUniqueId(),
type: 'ghost',
position: {
x: targetNode.position.x,
y: targetNode.position.y,
},
data: { type: 'ghost' },
className: 'ghost',
selectable: false,
draggable: false,
}));
ghostEdges = ghostNodes.map((ghostNode, i) => {
return {
id: `${newNodeId}->${ghostNode.id}`,
source: newNodeId,
target: ghostNode.id,
type: 'step',
animated: true,
deletable: false,
label: labels[i],
};
});
if (targetEdge) {
targetEdge.label = labels[labels.length - 1] ?? '+';
}
}
let newEdges: Edge[] = [];
setEdges((edges) => {
newEdges = edges.filter((e) => e.id !== id).concat([sourceEdge], targetEdge ?? [], ghostEdges);
return newEdges;
});
setNodes((nodes) => {
const targetNodeIndex = nodes.findIndex((node) => node.id === edge.target);
const newNodes = [...nodes.slice(0, targetNodeIndex), insertNode, ...nodes.slice(targetNodeIndex)];
const newNodeIndex = newNodes.findIndex((node) => node.id === newNodeId);
newNodes.splice(newNodeIndex + 1, 0, ...ghostNodes);
return newNodes;
});
const isFinishingStep = [
StepType.DynamicChoices,
StepType.FinishingStepEnd,
StepType.FinishingStepRedirect,
].includes(stepType);
if (!isFinishingStep) {
setTimeout(() => {
useServiceStore.getState().saveToHistory();
}, 0);
}
};
return handleEdgeClick;
}
export default useEdgeAdd;