This repository was archived by the owner on Jun 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathJobNode.tsx
More file actions
219 lines (207 loc) · 6.61 KB
/
JobNode.tsx
File metadata and controls
219 lines (207 loc) · 6.61 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import { Job, WorkflowJob } from '@circleci/circleci-config-sdk';
import { WorkflowJobParameters } from '@circleci/circleci-config-sdk/dist/src/lib/Components/Workflow/types/WorkflowJob.types';
import React, { useRef } from 'react';
import { Handle, isNode, NodeProps, Position } from 'react-flow-renderer';
import JobIcon from '../../../icons/components/JobIcon';
import DeleteItemIcon from '../../../icons/ui/DeleteItemIcon';
import PlusIcon from '../../../icons/ui/PlusIcon';
import JobMapping from '../../../mappings/JobMapping';
import { useStoreActions, useStoreState } from '../../../state/Hooks';
const JobNode: React.FunctionComponent<NodeProps & { data: WorkflowJob }> = (
props,
) => {
const elements = useStoreState(
(state) => state.workflows[state.selectedWorkflow].elements,
);
const dragging = useStoreState((state) => state.dragging);
// const setWorkflowElements = useStoreActions(
// (actions) => actions.setWorkflowElements,
// );
const updateJob = useStoreActions((actions) => actions.updateJob);
const setConnecting = useStoreActions((actions) => actions.setConnecting);
const removeWorkflowElement = useStoreActions((actions) => actions.removeWorkflowElement);
const connecting = useStoreState((state) => state.connecting);
const updateConnecting = useStoreActions(
(actions) => actions.updateConnecting,
);
const updateWorkflowJob = (
workflowJob: WorkflowJob,
applyToData: { job?: Job; parameters?: WorkflowJobParameters },
) =>
elements.map((element) =>
isNode(element) && element.data.job.name === workflowJob.job.name
? { ...element, data: { ...workflowJob, ...applyToData } }
: element,
);
const [hovering, setHovering] = React.useState({
handles: false,
node: false,
requiredBy: false,
remove: false,
requires: false,
});
const trackHovering = (
entering: string[],
leaving: string[],
postEnter?: () => void,
postLeave?: () => void,
) => {
return {
onMouseEnter: () => {
setHovering(
entering.reduce(
(previous, n) => ({ ...previous, [n]: true }),
hovering,
),
);
postEnter && postEnter();
},
onMouseLeave: () => {
setHovering(
leaving.reduce(
(previous, n) => ({ ...previous, [n]: false }),
hovering,
),
);
postLeave && postLeave();
},
};
};
const nodeRef = useRef(null);
return (
<div
className={`p-8 flex flex-row cursor-default`}
{...trackHovering(
['handles'],
['handles', 'node', 'requires', 'requiredBy'],
() => {
updateConnecting({
ref: nodeRef,
id: {
connectionNodeId: props.id,
connectionHandleType: 'target',
connectionHandleId: `${props.id}_target`,
},
name: props.data.parameters?.name || props.data.job.name,
});
},
() => {
updateConnecting(undefined);
},
)}
onDragOver={(e) => {
if (dragging && dragging.dataType?.dragTarget === JobMapping.type) {
e.preventDefault();
}
}}
onDrop={(e) => {
if (
dragging &&
dragging.dataType?.dragTarget === JobMapping.type &&
dragging.dataType.applyToNode
) {
const applyToData = dragging.dataType.applyToNode(
dragging.data,
props.data,
);
if (JobMapping.type in applyToData) {
updateJob({ old: props.data.job, new: applyToData.job });
}
if ('parameters' in applyToData) {
updateWorkflowJob(props.data, applyToData);
}
}
}}
>
<button
className={`opacity-${
hovering['handles'] && !hovering['node'] && !connecting?.start
? 100
: 0
} transition-opacity duration-300 w-4 h-4 my-auto mr-5`}
id={`${props.id}_source`}
{...trackHovering(['requires', 'handles'], ['requires'])}
>
<PlusIcon
filled={hovering['requires']}
color="rgb(0, 120, 202)"
className="bg-white rounded-full w-full border border-white"
/>
</button>
<button
className={`p-2 bg-white node flex flex-row text-black rounded-md border cursor-pointer
${
(hovering['node'] && !hovering['remove']) ||
(hovering['handles'] && connecting?.start)
? 'border-circle-blue'
: 'border-circle-gray-300'
}`}
ref={nodeRef}
{...trackHovering(['node'], ['node'])}
>
<div className="flex w-full">
<JobIcon className="w-5 mr-2" />
{props.data.parameters?.name || props.data.job.name}
</div>
<button
className={`my-auto
opacity-${hovering['node'] ? 100 : 0}
transition-opacity duration-150 w-8 h-full flex`}
{...trackHovering(['remove'], ['remove'])}
onClick={() => {
removeWorkflowElement(props.id);
}}
>
<DeleteItemIcon
className="w-3 cursor-pointer m-auto"
color={hovering['remove'] ? 'red' : '#AAAAAA'}
/>
</button>
</button>
<button
className={`opacity-${
hovering['handles'] && !hovering['node'] && !connecting?.start
? 100
: 0
} source transition-opacity duration-300 w-4 h-4 my-auto ml-5`}
{...trackHovering(['requiredBy', 'handles'], ['requiredBy'])}
id={`${props.id}_target`}
// onClick={() => {
// TODO: Implement 'add job' menu functionality
// }}
draggable
onDragStart={(e) => {
setConnecting({
ref: nodeRef,
id: {
connectionNodeId: props.id,
connectionHandleType: 'source',
connectionHandleId: `${props.id}_source`,
},
name: props.data.parameters?.name || props.data.job.name,
});
e.preventDefault();
}}
>
<PlusIcon
className="bg-white rounded-full w-full border border-white"
color="rgb(0, 120, 202)"
filled={hovering['requiredBy']}
/>
</button>
<Handle
type="source"
className="opacity-0 cursor-default"
position={Position.Right}
id={`${props.id}_source`}
></Handle>
<Handle
className="opacity-0 cursor-default"
id={`${props.id}_target`}
type="target"
position={Position.Left}
/>
</div>
);
};
export default JobNode;