"use client"; import { BaseEdge, EdgeLabelRenderer, useReactFlow, type EdgeProps } from "@xyflow/react"; import React from "react"; import { buildRoutePath, type RouteWaypoint } from "./edgeRoutes"; import { routeEdge, type RoutePoint, type RouteRect } from "./edgeRouting"; export type RoutedEdgeData = { /** User-placed waypoints for this edge (empty = automatic routing). */ waypoints: RouteWaypoint[]; /** "source:target" key used to persist the route. */ routeId: string; /** Technology ids at each end, so the edge may connect to them. */ sourceId: number; targetId: number; /** Node rectangles the edge should route around. */ obstacles: RouteRect[]; /** Where the automatic vertical run sits (0..1). */ stepPosition: number; editable: boolean; onRouteChange: (routeId: string, waypoints: RouteWaypoint[]) => void; }; /** * A prerequisite edge. It draws a polyline from the source handle through any * user waypoints to the target handle, bending around technologies it is not * connected to, with rounded corners at each bend. * * Interaction (only when `editable`): * - Select the edge to reveal "+" handles at each segment's midpoint; click one * to add a bend there. * - Drag a waypoint dot to move it. * - Double-click a waypoint dot to remove it. */ export function RoutedEdge({ id, sourceX, sourceY, targetX, targetY, markerEnd, style, data, selected, }: EdgeProps) { const route = data as unknown as RoutedEdgeData | undefined; const waypoints = route?.waypoints ?? []; const editable = route?.editable ?? false; const { screenToFlowPosition } = useReactFlow(); const dragIndexRef = React.useRef(null); // Shared shape builder (same logic as the dialogue canvas): forward edges // take a stubbed Z-step at the source's lane (stepPosition), back edges try // bus corridors around the cards, and every candidate is detoured around // unrelated technologies with the cleanest result winning. User waypoints, // when present, are routed verbatim. const start: RoutePoint = { x: sourceX, y: sourceY }; const end: RoutePoint = { x: targetX, y: targetY }; const { points: routed, base } = routeEdge({ id: route?.routeId, start, end, sourceDirection: "right", targetDirection: "left", nodes: route?.obstacles ?? [], sourceId: route?.sourceId, targetId: route?.targetId, waypoints: waypoints.length > 0 ? waypoints : undefined, stepPosition: route?.stepPosition, }); const path = routed.length >= 2 ? buildRoutePath(routed) : ""; const commit = (next: RouteWaypoint[]) => route?.onRouteChange(route.routeId, next); const onHandlePointerDown = (index: number) => (event: React.PointerEvent) => { if (!editable) return; event.stopPropagation(); event.preventDefault(); dragIndexRef.current = index; event.currentTarget.setPointerCapture(event.pointerId); }; const onHandlePointerMove = (event: React.PointerEvent) => { const index = dragIndexRef.current; if (index === null) return; const point = screenToFlowPosition({ x: event.clientX, y: event.clientY }); commit( waypoints.map((wp, i) => i === index ? { x: Math.round(point.x), y: Math.round(point.y) } : wp, ), ); }; const onHandlePointerUp = (event: React.PointerEvent) => { if (dragIndexRef.current === null) return; dragIndexRef.current = null; try { event.currentTarget.releasePointerCapture(event.pointerId); } catch { // capture may already be released } }; const onHandleDoubleClick = (index: number) => (event: React.MouseEvent) => { if (!editable) return; event.stopPropagation(); commit(waypoints.filter((_, i) => i !== index)); }; // Midpoints of the UNROUTED segments, for the "add a bend" handles (a routed // detour is not a place the user can insert a waypoint at). const midpoints: { x: number; y: number; insertAt: number }[] = []; for (let i = 0; i < base.length - 1; i += 1) { midpoints.push({ x: (base[i].x + base[i + 1].x) / 2, y: (base[i].y + base[i + 1].y) / 2, insertAt: i, }); } const addAt = (index: number, point: RouteWaypoint) => { const next = [...waypoints]; next.splice(index, 0, point); commit(next); }; return ( <> {selected && editable ? midpoints.map((mid, i) => (
)) : null} {waypoints.map((wp, index) => (
))} ); }