import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { Check, Plus, Search, X } from "lucide-react"; import { NODE_CATALOG, synthesizePluginSpec, type NodeKind, type NodeSpec, type PluginNodeSpec, } from "../lib/nodeCatalog"; import { useApp } from "../lib/store"; import { useDismissOn } from "../lib/useDismissOn"; /** * Dropdown multi-select block picker. * * Opens a categorised list of all available blocks. The user checks the * ones they want, then clicks "Add to canvas" to batch-create them. * Blocks already on the canvas are marked with a subtle badge. * * Intended as the primary "browse & select" workflow for users who * don't want to drag tiles one by one. Lives in the NodePalette header * row, triggered by a button. */ const FAMILY_META: { key: NodeSpec["family"]; label: string; hint: string; }[] = [ { key: "source", label: "Source", hint: "Required. Feeds a quantum circuit into the pipeline." }, { key: "backend", label: "Backend", hint: "Provides a noise model. Required for QuCAD; others fall back to a default." }, { key: "algorithm", label: "Algorithm", hint: "Pick one or more research algorithms to apply." }, { key: "metric", label: "Metric", hint: "Optional. Scores the output circuit (e.g. fidelity)." }, { key: "sink", label: "Sink", hint: "Required. Collects final metrics and the resulting circuit." }, ]; /** Blocks that are pre-checked when the picker opens, because almost * every pipeline needs them. The user can still uncheck them. */ const DEFAULT_CHECKED: NodeKind[] = ["input_circuit", "output"]; export function BlockPicker({ canvasKinds, }: { /** Kinds already present on the canvas, for "on canvas" badges. */ canvasKinds: Set; }) { const [open, setOpen] = useState(false); const [search, setSearch] = useState(""); const [checked, setChecked] = useState>(new Set(DEFAULT_CHECKED)); const rootRef = useRef(null); const searchRef = useRef(null); const addBlocksToCanvas = useApp((s) => s.addBlocksToCanvas); useDismissOn(open, rootRef, useCallback(() => setOpen(false), [])); // Reset to defaults + focus search on open. useEffect(() => { if (open) { setChecked(new Set(DEFAULT_CHECKED)); setSearch(""); searchRef.current?.focus(); } }, [open]); // Merge built-in catalog with the user's plugin manifests so the // picker shows everything in one list, grouped by family. const plugins = useApp((s) => s.plugins); const combined: (NodeSpec | PluginNodeSpec)[] = useMemo( () => [...NODE_CATALOG, ...plugins.map(synthesizePluginSpec)], [plugins], ); const filtered = useMemo(() => { if (!search.trim()) return combined; const q = search.toLowerCase(); return combined.filter( (n) => n.label.toLowerCase().includes(q) || n.tagline.toLowerCase().includes(q) || n.family.toLowerCase().includes(q) || n.kind.toLowerCase().includes(q), ); }, [search, combined]); const toggle = (kind: NodeKind) => setChecked((s) => { const next = new Set(s); next.has(kind) ? next.delete(kind) : next.add(kind); return next; }); const addSelected = () => { if (checked.size === 0) return; // Preserve catalog order (built-ins first, then plugins) so // auto-connect lays them out left-to-right sensibly. const ordered = combined .filter((n) => checked.has(n.kind as NodeKind)) .map((n) => n.kind as NodeKind); addBlocksToCanvas(ordered); setChecked(new Set()); setOpen(false); }; return (
{open && (
{/* Search */}
setSearch(e.target.value)} placeholder="Filter blocks..." className="w-full text-[11px] bg-surfaceAlt border border-edge rounded-md pl-6 pr-6 py-1.5 text-ink placeholder:text-mute/60 focus:outline-none focus:border-accent/60" /> {search && ( )}
{/* Categorised list */}
{FAMILY_META.map((fm) => { const items = filtered.filter((n) => n.family === fm.key); if (items.length === 0) return null; return (
{fm.label}
{fm.hint}
{items.map((n) => { const isChecked = checked.has(n.kind as NodeKind); const onCanvas = canvasKinds.has(n.kind as NodeKind); const isPlugin = "isPlugin" in n && n.isPlugin; const Icon = n.icon; return ( ); })}
); })} {filtered.length === 0 && (
No blocks match "{search}"
)}
{/* Footer with Add button */}
{checked.size > 0 ? `${checked.size} selected` : "Check blocks to add"}
)}
); }