"use client";
import type { MouseEvent as ReactMouseEvent } from "react";
import type { CanvasConnection, CanvasNode, CanvasPosition } from "@/lib/canvas/types";
import { getActiveConnectionPath, getConnectionPath } from "@/lib/canvas/geometry";
type CanvasConnectionsProps = {
connections: CanvasConnection[];
nodes: CanvasNode[];
selectedConnectionId: string | null;
activeConnection?: {
fromNodeId: string;
mouseWorld: CanvasPosition;
} | null;
onSelectConnection: (id: string) => void;
};
export function CanvasConnections({
connections,
nodes,
selectedConnectionId,
activeConnection,
onSelectConnection,
}: CanvasConnectionsProps) {
const byId = new Map(nodes.map((node) => [node.id, node]));
const activeFrom = activeConnection
? byId.get(activeConnection.fromNodeId)
: undefined;
return (
);
}
function ConnectionPath({
connection,
from,
to,
active,
onSelect,
}: {
connection: CanvasConnection;
from: CanvasNode;
to: CanvasNode;
active: boolean;
onSelect: () => void;
}) {
const path = getConnectionPath(from, to);
const stroke = active ? "#18181b" : "#a1a1aa";
function handleContextMenu(event: ReactMouseEvent) {
event.preventDefault();
event.stopPropagation();
onSelect();
}
return (
{
event.stopPropagation();
onSelect();
}}
onContextMenu={handleContextMenu}
/>
);
}