import React, { forwardRef, useMemo } from 'react'; import NetBox from './NetBox'; import NodeCard from './NodeCard'; import type { Net, MazeNode, Edge } from './types'; import type { Selection } from './Inspector'; import type { ResizeHandle } from './useMazeInteraction'; interface Props { nets: Net[]; nodes: MazeNode[]; edges: Edge[]; deployed: boolean; selection: Selection; setSelection: (s: Selection) => void; pan: { x: number; y: number }; dropTargetId: string | null; dragging: boolean; edgeDraw: { fromX: number; fromY: number; toX: number; toY: number; hoverTarget: string | null } | null; onCanvasMouseDown: (e: React.MouseEvent) => void; onNodeMouseDown: (id: string) => (e: React.MouseEvent) => void; onNetMouseDown: (id: string) => (e: React.MouseEvent) => void; onNetResizeMouseDown: (id: string, handle: ResizeHandle) => (e: React.MouseEvent) => void; onPortMouseDown: (id: string) => (e: React.MouseEvent) => void; onNodeContextMenu?: (id: string) => (e: React.MouseEvent) => void; onNetContextMenu?: (id: string) => (e: React.MouseEvent) => void; onEdgeContextMenu?: (id: string) => (e: React.MouseEvent) => void; onCanvasContextMenu?: (e: React.MouseEvent) => void; } const NODE_W = 140; const NODE_HEAD_H = 22; const Canvas = forwardRef(function Canvas( { nets, nodes, edges, deployed, selection, setSelection, pan, dropTargetId, dragging, edgeDraw, onCanvasMouseDown, onNodeMouseDown, onNetMouseDown, onNetResizeMouseDown, onPortMouseDown, onNodeContextMenu, onNetContextMenu, onEdgeContextMenu, onCanvasContextMenu }, ref, ) { const netById = useMemo(() => new Map(nets.map((n) => [n.id, n])), [nets]); const absPos = (node: MazeNode) => { const net = netById.get(node.netId); return { x: (net?.x ?? 0) + node.x, y: (net?.y ?? 0) + node.y }; }; const activeNetIds = useMemo(() => { const nodeNet = new Map(nodes.map((n) => [n.id, n.netId])); const ids = new Set(); for (const e of edges) { const a = nodeNet.get(e.from); const b = nodeNet.get(e.to); if (a) ids.add(a); if (b) ids.add(b); } return ids; }, [nodes, edges]); const selNetId = selection?.type === 'net' ? selection.id : null; const selNodeId = selection?.type === 'node' ? selection.id : null; const selEdgeId = selection?.type === 'edge' ? selection.id : null; return (
{ if (e.target === e.currentTarget) setSelection(null); onCanvasMouseDown(e); }} onContextMenu={(e) => { if (e.target === e.currentTarget && onCanvasContextMenu) onCanvasContextMenu(e); }} style={{ cursor: dragging ? 'grabbing' : 'grab' }} >
{edges.map((e) => { const from = nodes.find((n) => n.id === e.from); const to = nodes.find((n) => n.id === e.to); if (!from || !to) return null; const a = absPos(from); const b = absPos(to); const x1 = a.x + NODE_W, y1 = a.y + NODE_HEAD_H; const x2 = b.x, y2 = b.y + NODE_HEAD_H; const cx = (x1 + x2) / 2; const d = `M${x1},${y1} C${cx},${y1} ${cx},${y2} ${x2},${y2}`; const klass = e.traffic === 'hot' ? 'hot' : e.traffic === 'active' ? 'active' : ''; const marker = e.traffic === 'hot' ? 'arrow-alert' : e.traffic === 'active' ? 'arrow-violet' : 'arrow-matrix'; const isSel = e.id === selEdgeId; return ( { ev.stopPropagation(); setSelection({ type: 'edge', id: e.id }); }} onContextMenu={onEdgeContextMenu?.(e.id)}> {e.label && ( {e.label} )} ); })} {edgeDraw && (() => { const cx = (edgeDraw.fromX + edgeDraw.toX) / 2; const d = `M${edgeDraw.fromX},${edgeDraw.fromY} C${cx},${edgeDraw.fromY} ${cx},${edgeDraw.toY} ${edgeDraw.toX},${edgeDraw.toY}`; return ; })()}
{nets.map((net) => { const inactive = net.kind !== 'internet' && !activeNetIds.has(net.id); return ( setSelection({ type: 'net', id })} onHeaderMouseDown={onNetMouseDown} onResizeMouseDown={onNetResizeMouseDown} onContextMenu={onNetContextMenu?.(net.id)} /> ); })} {nodes.map((n) => { const p = absPos(n); return ( setSelection({ type: 'node', id })} onMouseDown={onNodeMouseDown} onPortMouseDown={onPortMouseDown} onContextMenu={onNodeContextMenu} /> ); })}
HOT
ACTIVE
IDLE
); }); export default Canvas;