import { BaseEdge, Edge, EdgeLabelRenderer, EdgeProps, getBezierPath, } from '@xyflow/react'; import { memo } from 'react'; import useGraphStore from '../../store'; import { useFetchAgent } from '@/hooks/use-agent-request'; import { cn } from '@/lib/utils'; import { useMemo } from 'react'; import { NodeHandleId, Operator } from '../../constant'; function InnerButtonEdge({ id, sourceX, sourceY, targetX, targetY, sourcePosition, targetPosition, source, target, style = {}, markerEnd, selected, data, sourceHandleId, }: EdgeProps>) { const deleteEdgeById = useGraphStore((state) => state.deleteEdgeById); const highlightedPlaceholderEdgeId = useGraphStore( (state) => state.highlightedPlaceholderEdgeId, ); const [edgePath, labelX, labelY] = getBezierPath({ sourceX, sourceY, sourcePosition, targetX, targetY, targetPosition, }); const selectedStyle = useMemo(() => { return selected ? { strokeWidth: 1, stroke: 'rgb(var(--accent-primary))' } : {}; }, [selected]); const placeholderHighlightStyle = useMemo(() => { const isHighlighted = highlightedPlaceholderEdgeId === id; return isHighlighted ? { strokeWidth: 2, stroke: 'var(--accent-primary)' } : {}; }, [highlightedPlaceholderEdgeId, id]); const onEdgeClick = () => { deleteEdgeById(id); }; // highlight the nodes that the workflow passes through const { data: flowDetail } = useFetchAgent(); const showHighlight = useMemo(() => { const path = flowDetail?.dsl?.path ?? []; const idx = path.findIndex((x) => x === target); if (idx !== -1) { let index = idx - 1; while (index >= 0) { if (path[index] === source) { return { strokeWidth: 1, stroke: 'rgb(var(--accent-primary))' }; } index--; } return {}; } return {}; }, [flowDetail?.dsl?.path, source, target]); const visible = useMemo(() => { return ( data?.isHovered && sourceHandleId !== NodeHandleId.Tool && sourceHandleId !== NodeHandleId.AgentBottom && // The connection between the agent node and the tool node does not need to display the delete button !target.startsWith(Operator.Tool) ); }, [data?.isHovered, sourceHandleId, target]); return ( <>
); } export const ButtonEdge = memo(InnerButtonEdge);