import React, { useContext, useMemo } from 'react' import { BaseEdge, getBezierPath, type EdgeProps, } from '@xyflow/react' import FlowContext from '../../lib/flowContext' import { getConnectionLabelForTarget } from '../../lib/nodeRegistry' const EDGE_STROKE_WIDTH = 2 const DOT_MARKER_R = 1.5 const EMPTY_PATH_NODE_IDS = new Set() export function AnimatedEdge({ id, source, sourceX, sourceY, targetX, targetY, sourcePosition, targetPosition, style, label: labelProp, interactionWidth, target, }: EdgeProps) { const ctx = useContext(FlowContext) const nodes = ctx?.nodes ?? [] const pathNodeIds = ctx?.connectionPathNodeIds ?? EMPTY_PATH_NODE_IDS const targetNode = useMemo(() => nodes.find((n: any) => n.id === target), [nodes, target]) const derivedLabel = useMemo( () => (targetNode?.type != null ? getConnectionLabelForTarget(targetNode.type) : undefined), [targetNode?.type] ) const label = labelProp ?? derivedLabel const isOnUpdatingPath = pathNodeIds.has(source) && pathNodeIds.has(target) const [edgePath, edgeLabelX, edgeLabelY] = getBezierPath({ sourceX, sourceY, targetX, targetY, sourcePosition, targetPosition, }) const startId = `animated-edge-dot-start-${id}` const endId = `animated-edge-dot-end-${id}` return ( <> {label != null && ( {label} )} ) }