"use client"; import React from "react"; export function Connections({ connections, nodes, scale, onDeleteConnection, connectingFrom, mousePos, panOffset, }) { const getNodePosition = (nodeId, handle = "right") => { const node = nodes.find((n) => n.id === nodeId); if (!node) return { x: 0, y: 0 }; const nodeWidth = 180; const nodeHeight = 56; const labelHeight = 24; let x = node.position.x * scale; let y = (node.position.y + labelHeight) * scale; switch (handle) { case "left": y += (nodeHeight * scale) / 2; break; case "right": x += nodeWidth * scale; y += (nodeHeight * scale) / 2; break; default: x += nodeWidth * scale; y += (nodeHeight * scale) / 2; } return { x, y }; }; const createBezierPath = (from, to) => { const dx = Math.abs(to.x - from.x); const controlOffset = Math.min(dx * 0.5, 100); return `M ${from.x} ${from.y} C ${from.x + controlOffset} ${from.y}, ${to.x - controlOffset} ${to.y}, ${to.x} ${to.y}`; }; const getConnectingLineEnd = () => { if (!connectingFrom) return null; const fromPos = getNodePosition( connectingFrom.nodeId, connectingFrom.handle ); return { from: fromPos, to: { x: mousePos.x - panOffset.x, y: mousePos.y - panOffset.y, }, }; }; const connectingLine = getConnectingLineEnd(); return ( {/* Existing connections */} {connections.map((conn) => { const fromPos = getNodePosition(conn.from, conn.fromHandle || "right"); const toPos = getNodePosition(conn.to, conn.toHandle || "left"); const path = createBezierPath(fromPos, toPos); return ( {/* Invisible wider path for easier clicking */} onDeleteConnection(conn.id)} /> {/* Visible path */} {/* Hover state path */} ); })} {/* Active connecting line */} {connectingLine && ( )} ); } export default Connections;