forked from lightningpixel/modly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkflowEdge.tsx
More file actions
73 lines (64 loc) · 2.58 KB
/
Copy pathWorkflowEdge.tsx
File metadata and controls
73 lines (64 loc) · 2.58 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
import { getBezierPath, useReactFlow, useEdges } from '@xyflow/react'
import type { EdgeProps } from '@xyflow/react'
import { useExtensionsStore } from '@shared/stores/extensionsStore'
import { buildAllWorkflowExtensions } from '../mockExtensions'
const HANDLE_COLOR: Record<string, string> = {
audio: '#34d399',
image: '#38bdf8',
mesh: '#a78bfa',
text: '#fbbf24',
}
export default function WorkflowEdge({
id, source, target,
sourceX, sourceY, targetX, targetY,
sourcePosition, targetPosition,
}: EdgeProps) {
const { getNode } = useReactFlow()
const edges = useEdges()
const { modelExtensions, processExtensions } = useExtensionsStore()
const allExtensions = buildAllWorkflowExtensions(modelExtensions, processExtensions)
const sourceNode = getNode(source)
const targetNode = getNode(target)
// Read targetHandle directly from edge store — reliable regardless of EdgeProps version
const thisEdge = edges.find((e) => e.id === id)
const targetHandle = thisEdge?.targetHandle
const sourceColor = sourceNode?.type === 'imageNode'
? HANDLE_COLOR.image
: sourceNode?.type === 'textNode'
? HANDLE_COLOR.text
: sourceNode?.type === 'meshNode'
? HANDLE_COLOR.mesh
: (HANDLE_COLOR[allExtensions.find((e) => e.id === sourceNode?.data?.extensionId)?.output ?? ''] ?? '#52525b')
// For multi-input nodes pick the color of the specific connected handle
const targetExt = allExtensions.find((e) => e.id === targetNode?.data?.extensionId)
const targetInputType = (() => {
if (targetExt?.inputs && targetExt.inputs.length > 1 && targetHandle) {
const idx = parseInt(targetHandle.replace('input-', ''), 10)
return targetExt.inputs[isNaN(idx) ? 0 : idx] ?? targetExt.input
}
return targetExt?.input
})()
const targetColor = targetNode?.type === 'outputNode'
? HANDLE_COLOR.mesh
: targetNode?.type === 'previewNode'
? HANDLE_COLOR.image
: (HANDLE_COLOR[targetInputType ?? ''] ?? '#52525b')
const [edgePath] = getBezierPath({ sourceX, sourceY, sourcePosition, targetX, targetY, targetPosition })
const gradientId = `wf-edge-${id}`
return (
<>
<defs>
<linearGradient id={gradientId} gradientUnits="userSpaceOnUse" x1={sourceX} y1={sourceY} x2={targetX} y2={targetY}>
<stop offset="0%" stopColor={sourceColor} />
<stop offset="100%" stopColor={targetColor} />
</linearGradient>
</defs>
<path
d={edgePath}
fill="none"
style={{ stroke: `url(#${gradientId})`, strokeWidth: 2.5 }}
className="react-flow__edge-path"
/>
</>
)
}