-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathWorkflow.tsx
205 lines (184 loc) · 8.28 KB
/
Workflow.tsx
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
/*
* Copyright (c) 2024. Devtron Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { useEffect, useState } from 'react'
import {
APIResponseHandler,
GenericEmptyState,
GraphVisualizer,
GraphVisualizerEdge,
GraphVisualizerNode,
GraphVisualizerProps,
Icon,
useAsync,
} from '@devtron-labs/devtron-fe-common-lib'
import { getCreateWorkflows } from '@Components/app/details/triggerView/workflow.service'
import { getEnvironmentListMin } from '@Services/service'
import { HandleNodeUpdateActionProps, NodeUpdateActionType, WorkflowProps } from './types'
import {
getCDNodeIcon,
getPipelineIdFromNodeId,
getValidatedNodes,
getWorkflowGraphVisualizerEdges,
getWorkflowGraphVisualizerNodes,
getWorkflowLinkedCDNodes,
} from './utils'
export const Workflow = ({ templateId, onChange, workflowIdToErrorMessageMap }: WorkflowProps) => {
// STATES
const [nodes, setNodes] = useState<Record<string, GraphVisualizerNode[]>>({})
const [edges, setEdges] = useState<Record<string, GraphVisualizerEdge[]>>({})
// ASYNC CALL - FETCH WORKFLOWS
const [isWorkflowDataLoading, workflowData, workflowDataErr, reloadWorkflowData] = useAsync(
() => Promise.all([getCreateWorkflows(templateId, false, '', true), getEnvironmentListMin()]),
[templateId],
)
// METHODS
const setNodesHandler =
(wfId: string): GraphVisualizerProps['setNodes'] =>
(newState) =>
setNodes((prev) => ({
...prev,
[wfId]: typeof newState === 'function' ? newState(prev[wfId] || []) : newState,
}))
const setEdgesHandler =
(wfId: string): GraphVisualizerProps['setEdges'] =>
(newState) =>
setEdges((prev) => ({
...prev,
[wfId]: typeof newState === 'function' ? newState(prev[wfId] || []) : newState,
}))
// CENTRAL NODE UPDATE HANDLER
const handleNodeUpdateAction = (nodeAction: HandleNodeUpdateActionProps) => {
const { actionType } = nodeAction
switch (actionType) {
case NodeUpdateActionType.UPDATE_CD_PIPELINE:
{
const { id, value, wfId } = nodeAction
setNodes((prev) => {
const changedNodes: Parameters<WorkflowProps['onChange']>[0]['cd'] = []
const updatedNodes = {
...prev,
[wfId]: prev[wfId].map((node) => {
const nodeId = getPipelineIdFromNodeId(node.id)
if (nodeId === id && node.type === 'dropdownNode') {
changedNodes.push({
environmentId: Number(value.value),
pipelineId: Number(nodeId),
})
return {
...node,
data: {
...node.data,
value,
icon: getCDNodeIcon({
isVirtualEnvironment: value.isVirtualEnvironment,
showPluginWarning: false,
}),
},
}
}
return node
}),
}
const linkedCDNodesMap = getWorkflowLinkedCDNodes(workflowData[0].workflows, id)
if (linkedCDNodesMap.size) {
Array.from(linkedCDNodesMap.entries()).forEach(([parentWfId, linkedCDNode]) => {
updatedNodes[parentWfId] = updatedNodes[parentWfId].map((node) =>
getPipelineIdFromNodeId(node.id) === linkedCDNode.id && node.type === 'textNode'
? {
...node,
data: { ...node.data, text: value.label as string },
}
: node,
)
})
}
const { validatedNodes, workflowIdToErrorMessageMap: _workflowIdToErrorMessageMap } =
getValidatedNodes(updatedNodes)
onChange({ cd: changedNodes }, _workflowIdToErrorMessageMap)
return validatedNodes
})
}
break
default:
break
}
}
// UPDATE NODES & EDGES AFTER API RESPONSE
useEffect(() => {
if (!isWorkflowDataLoading && workflowData) {
const updatedNodes = getWorkflowGraphVisualizerNodes({
workflows: workflowData[0].workflows,
environmentList: workflowData[1].result,
handleNodeUpdateAction,
})
setNodes(updatedNodes)
setEdges(getWorkflowGraphVisualizerEdges(workflowData[0].workflows))
onChange(
{
cd: workflowData[0].workflows
.flatMap(({ nodes: workflowNodes }) => workflowNodes)
.filter(({ type }) => type === 'CD')
.map(({ id, environmentId }) => ({
pipelineId: +id,
environmentId,
})),
},
{},
)
}
}, [isWorkflowDataLoading, workflowData])
return (
<APIResponseHandler
progressingProps={{ pageLoader: true }}
isLoading={isWorkflowDataLoading}
error={workflowDataErr}
errorScreenManagerProps={{
code: workflowDataErr?.code,
reload: reloadWorkflowData,
}}
>
{Array.isArray(workflowData?.[0]?.workflows) && workflowData[0].workflows.length ? (
workflowData[0].workflows.map(
({ id, name }) =>
Object.keys(nodes).length &&
Object.keys(edges).length && (
<div key={id} className="flexbox-col dc__gap-4">
<div className="flexbox-col dc__gap-6">
<p className="m-0 fs-13 lh-20 fw-6">{name}</p>
<GraphVisualizer
nodes={nodes[id]}
setNodes={setNodesHandler(id)}
edges={edges[id]}
setEdges={setEdgesHandler(id)}
/>
</div>
{workflowIdToErrorMessageMap[id] && (
<div className="flexbox dc__gap-4 fs-11 lh-16 fw-4">
<Icon name="ic-error" size={16} color={null} />
<span className="dc__ellipsis-right__2nd-line cr-5">
{workflowIdToErrorMessageMap[id]}
</span>
</div>
)}
</div>
),
)
) : (
<GenericEmptyState title="No Workflows" />
)}
</APIResponseHandler>
)
}