-
-
Notifications
You must be signed in to change notification settings - Fork 24.8k
Expand file tree
/
Copy pathValidationFeedback.tsx
More file actions
282 lines (256 loc) · 12.6 KB
/
Copy pathValidationFeedback.tsx
File metadata and controls
282 lines (256 loc) · 12.6 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import { memo, useCallback, useEffect, useRef, useState } from 'react'
import { Alert, Box, Button, ClickAwayListener, Fab, Paper, Snackbar, Typography } from '@mui/material'
import { alpha, darken, lighten, useTheme } from '@mui/material/styles'
import { IconChecklist, IconExclamationCircle, IconX } from '@tabler/icons-react'
import validateEmptyImage from '@/assets/images/validate_empty.svg'
import { getAgentflowIcon } from '@/core/node-config'
import { tokens } from '@/core/theme/tokens'
import type { FlowEdge, FlowNode, NodeDataSchema, ValidationError } from '@/core/types'
import { applyValidationErrorsToNodes, validateFlow } from '@/core/validation'
import { useConfigContext } from '@/infrastructure/store'
const validationColor = tokens.colors.border.validation
/** Validation result grouped by node */
interface NodeValidationResult {
id: string
label: string
name: string
issues: string[]
}
export interface ValidationFeedbackProps {
nodes: FlowNode[]
edges: FlowEdge[]
availableNodes?: NodeDataSchema[]
setNodes: React.Dispatch<React.SetStateAction<FlowNode[]>>
}
function groupErrorsByNode(errors: ValidationError[], nodes: FlowNode[]): NodeValidationResult[] {
const nodeMap = new Map<string, NodeValidationResult>()
for (const error of errors) {
const id = error.nodeId || error.edgeId || 'flow'
if (!nodeMap.has(id)) {
const node = nodes.find((n) => n.id === id)
let label = `Edge ${id}`
let name = 'edge'
if (node) {
label = node.data.label || node.data.name
name = node.data.name
} else if (id === 'flow') {
label = 'Flow'
name = 'flow'
}
nodeMap.set(id, {
id,
label,
name,
issues: []
})
}
nodeMap.get(id)!.issues.push(error.message)
}
return Array.from(nodeMap.values())
}
function ValidationFeedbackComponent({ nodes, edges, availableNodes, setNodes }: ValidationFeedbackProps) {
const theme = useTheme()
const { isDarkMode } = useConfigContext()
const [open, setOpen] = useState(false)
const [results, setResults] = useState<NodeValidationResult[]>([])
const [hasValidated, setHasValidated] = useState(false)
const [successSnackbar, setSuccessSnackbar] = useState(false)
const containerRef = useRef<HTMLDivElement>(null)
const handleToggle = useCallback(() => {
setOpen((prev) => !prev)
}, [])
const handleClose = useCallback(() => {
setOpen(false)
}, [])
const handleValidate = useCallback(() => {
const result = validateFlow(nodes, edges, availableNodes)
const grouped = groupErrorsByNode(result.errors, nodes)
setResults(grouped)
setHasValidated(true)
// Show green success toast when no issues found
if (grouped.length === 0) {
setSuccessSnackbar(true)
}
// Push validation errors to node data for border highlighting
setNodes((prev) => applyValidationErrorsToNodes(prev, result.errors))
}, [nodes, edges, availableNodes, setNodes])
const getNodeIcon = (item: NodeValidationResult) => {
const foundIcon = getAgentflowIcon(item.name)
if (foundIcon) {
const IconComp = foundIcon.icon
return (
<Box
sx={{
width: 28,
height: 28,
borderRadius: '4px',
backgroundColor: foundIcon.color,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
color: 'white',
flexShrink: 0
}}
>
<IconComp size={16} />
</Box>
)
}
return (
<Box
sx={{
width: 28,
height: 28,
borderRadius: '4px',
backgroundColor: '#9e9e9e',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
color: 'white',
flexShrink: 0
}}
>
<IconExclamationCircle size={16} />
</Box>
)
}
// Reset stale validation state when flow changes
useEffect(() => {
if (hasValidated) {
setHasValidated(false)
setResults([])
}
}, [nodes.length, edges.length]) // eslint-disable-line react-hooks/exhaustive-deps
return (
<>
<ClickAwayListener onClickAway={handleClose}>
<div ref={containerRef} style={{ position: 'relative' }}>
<Fab
size='small'
aria-label='validation'
title='Validate flow'
onClick={handleToggle}
sx={{
color: 'white',
backgroundColor: 'primary.main',
'&:hover': {
backgroundColor: 'primary.main',
backgroundImage: 'linear-gradient(rgb(0 0 0/10%) 0 0)'
}
}}
>
{open ? <IconX /> : <IconChecklist />}
</Fab>
{open && (
<Paper
elevation={16}
sx={{
position: 'absolute',
top: '100%',
right: 0,
mt: 1.5,
width: 400,
zIndex: tokens.zIndex.canvasPanel
}}
>
<Box sx={{ p: 2 }}>
<Typography variant='h6' sx={{ mt: 1, mb: 2, fontWeight: 600 }}>
Checklist ({results.reduce((sum, r) => sum + r.issues.length, 0)})
</Typography>
<Box sx={{ maxHeight: '60vh', overflowY: 'auto', pr: 1, mr: -1 }}>
{results.length > 0 ? (
results.map((item, index) => (
<Paper
key={index}
elevation={0}
sx={{
p: 2,
mb: 2,
backgroundColor: isDarkMode ? theme.palette.background.paper : theme.palette.grey[50],
borderRadius: '8px',
border: `1px solid ${alpha(validationColor, isDarkMode ? 0.3 : 0.5)}`
}}
>
<Box sx={{ display: 'flex', alignItems: 'center', gap: 1, mb: 1 }}>
{getNodeIcon(item)}
<Typography sx={{ fontWeight: 500 }}>{item.label || item.name}</Typography>
</Box>
{item.issues.map((issue, issueIndex) => (
<Box
key={issueIndex}
sx={{
pt: 1.5,
px: 2,
pb: issueIndex === item.issues.length - 1 ? 1.5 : 0.5,
backgroundColor: isDarkMode
? darken(validationColor, 0.85)
: lighten(validationColor, 0.9),
display: 'flex',
alignItems: 'center',
gap: 1.5,
borderTopLeftRadius: issueIndex === 0 ? '8px' : 0,
borderTopRightRadius: issueIndex === 0 ? '8px' : 0,
borderBottomLeftRadius: issueIndex === item.issues.length - 1 ? '8px' : 0,
borderBottomRightRadius: issueIndex === item.issues.length - 1 ? '8px' : 0
}}
>
<IconExclamationCircle
color={validationColor}
size={20}
style={{ minWidth: 20, flexShrink: 0 }}
/>
<Typography variant='body2'>{issue}</Typography>
</Box>
))}
</Paper>
))
) : (
<Box sx={{ p: 3, textAlign: 'center' }}>
<img
style={{ objectFit: 'cover', height: '15vh', width: 'auto' }}
src={validateEmptyImage}
alt='Illustration of a checklist with no items, indicating no issues found'
/>
{hasValidated ? (
<Typography variant='body2' color='success.main' sx={{ mt: 2, fontWeight: 500 }}>
No issues found in your flow!
</Typography>
) : (
<Typography variant='body2' color='text.secondary' sx={{ mt: 2 }}>
Click "Validate flow" to check for issues
</Typography>
)}
</Box>
)}
</Box>
<Box sx={{ display: 'flex', justifyContent: 'center', mt: 2, mb: 1 }}>
<Button
variant='contained'
color='primary'
onClick={handleValidate}
startIcon={<IconChecklist size={18} />}
sx={{ minWidth: 120, textTransform: 'none' }}
>
Validate flow
</Button>
</Box>
</Box>
</Paper>
)}
</div>
</ClickAwayListener>
{/* Success toast */}
<Snackbar
open={successSnackbar}
autoHideDuration={3000}
onClose={() => setSuccessSnackbar(false)}
anchorOrigin={{ vertical: 'bottom', horizontal: 'left' }}
>
<Alert onClose={() => setSuccessSnackbar(false)} severity='success' variant='filled' sx={{ width: '100%' }}>
No issues found in your flow!
</Alert>
</Snackbar>
</>
)
}
export const ValidationFeedback = memo(ValidationFeedbackComponent)