import { useMemo, useState, useRef, useEffect } from 'react'; import { BaseEdge, getBezierPath } from '@xyflow/react'; import { useFlowStoreActions } from '@/stores/flow-store'; const AnimatedMessageEdge = ({ id, source, target, sourceX, sourceY, targetX, targetY, sourcePosition, targetPosition, data, label = '', markerEnd, }: any) => { const [edgePath, labelX, labelY] = getBezierPath({ sourceX, sourceY, sourcePosition, targetX, targetY, targetPosition, }); const { deleteEdge, updateEdge } = useFlowStoreActions(); const [isEditing, setIsEditing] = useState(false); const [editValue, setEditValue] = useState(label || ''); const inputRef = useRef(null); useEffect(() => { if (isEditing && inputRef.current) { inputRef.current.focus(); inputRef.current.select(); } }, [isEditing]); const handleDeleteEdge = (e: React.MouseEvent) => { e.stopPropagation(); deleteEdge(id); }; const handleLabelDoubleClick = (e: React.MouseEvent) => { e.stopPropagation(); setIsEditing(true); setEditValue(label || ''); }; const handleInputChange = (e: React.ChangeEvent) => { setEditValue(e.target.value); }; const handleInputKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter') { handleSaveLabel(); } else if (e.key === 'Escape') { setIsEditing(false); setEditValue(label || ''); } }; const handleInputBlur = () => { handleSaveLabel(); }; const handleSaveLabel = () => { setIsEditing(false); if (editValue !== label) { updateEdge({ id, source, target, sourceHandle: null, targetHandle: null, type: 'animatedMessage', animated: true, label: editValue, data, markerEnd, }); } }; const opacity = data?.opacity ?? 1; const messageColor = useMemo( () => (collection: string) => { switch (collection) { case 'event': return 'orange'; case 'command': return 'blue'; case 'query': return 'green'; default: return 'gray'; } }, [] ); const randomDelay = useMemo(() => Math.random() * 1, []); return ( // @ts-ignore <> {/* Circle Icon */} {/* Label background and text */} {/* Background rectangle */} {/* Input or text element */} {isEditing ? ( ) : ( {label || 'Double-click to edit'} )} {/* Delete button - always visible */} {/* Delete button background */} {/* X icon */} × {/* Label */} {/* {label} */} ); }; export default AnimatedMessageEdge;