import { useRef } from "react"; import { NODE_CATALOG, synthesizePluginSpec, type NodeKind, type NodeSpec, type PluginNodeSpec, } from "../lib/nodeCatalog"; import type { Family } from "../lib/autoConnect"; import { useApp } from "../lib/store"; import { useDismissOn } from "../lib/useDismissOn"; /** * Mini contextual chooser for on-canvas insertion (marker: * canvas-insert-menu) — a compact form of the Add-block popover's * stage-section list. FlowCanvas opens it two ways: * * - connect-end on empty pane: `families` = nextFamilies() of the * dragged-from node's family, so the menu offers ONLY blocks that * can legally follow per the auto-connect grammar; picking also * wires the pending connection. * - double-click on empty pane: `families` = null → all five * stages, no wiring. * * ~240px wide, internal scroll past ~6 rows, Escape / outside-click * dismiss via the useDismissOn idiom. Positioned by the caller inside * the canvas wrapper (absolute, clamped to fit). */ const STAGE_ORDER: { key: Family; label: string }[] = [ { key: "source", label: "Source" }, { key: "backend", label: "Backend" }, { key: "algorithm", label: "Algorithm" }, { key: "metric", label: "Metric" }, { key: "sink", label: "Sink" }, ]; export function CanvasInsertMenu({ left, top, families, hasSource, onPick, onClose, }: { left: number; top: number; /** Allowed families; null = unfiltered (double-click path). */ families: Family[] | null; /** True when picking will also wire an edge (connect-end path). */ hasSource: boolean; onPick: (kind: NodeKind) => void; onClose: () => void; }) { const rootRef = useRef(null); useDismissOn(true, rootRef, onClose); const plugins = useApp((s) => s.plugins); const combined: (NodeSpec | PluginNodeSpec)[] = [ ...NODE_CATALOG, ...plugins.map(synthesizePluginSpec), ]; const allowed = families ? combined.filter((n) => families.includes(n.family)) : combined; return (
{hasSource ? "Add & connect what follows" : "Add block here"}
{STAGE_ORDER.map((st) => { const items = allowed.filter((n) => n.family === st.key); if (items.length === 0) return null; return (
{st.label}
{items.map((n) => { const isPlugin = "isPlugin" in n && n.isPlugin; const Icon = n.icon; return ( ); })}
); })}
); }