File size: 10,867 Bytes
57f0b3e
f152600
74b7708
 
 
 
 
 
 
f152600
57f0b3e
f152600
 
 
 
 
 
 
 
 
 
 
 
 
7a9f56e
 
 
 
 
 
 
 
 
 
f152600
 
7a9f56e
 
 
 
f152600
 
 
 
 
 
 
 
7a9f56e
f152600
 
 
 
57f0b3e
f152600
7a9f56e
f152600
7a9f56e
 
 
 
 
f152600
 
74b7708
 
 
 
 
 
 
 
f152600
74b7708
f152600
74b7708
f152600
 
 
 
 
 
74b7708
f152600
 
 
 
 
 
 
 
 
 
74b7708
 
 
 
 
f152600
 
 
 
 
 
 
 
 
 
 
51365eb
 
 
f152600
 
 
 
 
 
51365eb
 
 
 
 
f152600
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a9f56e
 
 
 
 
 
 
f152600
 
74b7708
 
 
f152600
 
 
 
 
74b7708
f152600
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74b7708
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f152600
 
 
 
74b7708
2e22380
74b7708
 
 
f152600
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
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<NodeKind>;
}) {
  const [open, setOpen] = useState(false);
  const [search, setSearch] = useState("");
  const [checked, setChecked] = useState<Set<NodeKind>>(new Set(DEFAULT_CHECKED));
  const rootRef = useRef<HTMLDivElement>(null);
  const searchRef = useRef<HTMLInputElement>(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 (
    <div ref={rootRef} className="relative">
      <button
        type="button"
        onClick={() => setOpen((v) => !v)}
        className="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-md text-[11px] font-medium border border-accent/50 text-accent hover:bg-accent/10 transition-colors whitespace-nowrap"
        aria-haspopup="menu"
        aria-expanded={open}
        aria-label="Add blocks to canvas"
      >
        <Plus className="w-3 h-3" />
        Add blocks
      </button>

      {open && (
        <div
          role="menu"
          aria-label="Block catalog"
          className="fixed left-3 top-14 sm:absolute sm:left-0 sm:top-full sm:mt-1 z-40 w-[min(320px,calc(100vw-1.5rem))] max-h-[70vh] rounded-lg border border-edge bg-surface shadow-xl flex flex-col"
        >
          {/* Search */}
          <div className="p-2 border-b border-edge">
            <div className="relative">
              <Search className="absolute left-2 top-1/2 -translate-y-1/2 w-3 h-3 text-mute pointer-events-none" />
              <input
                ref={searchRef}
                type="text"
                value={search}
                onChange={(e) => 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 && (
                <button
                  type="button"
                  onClick={() => setSearch("")}
                  className="absolute right-1.5 top-1/2 -translate-y-1/2 text-mute hover:text-ink"
                >
                  <X className="w-3 h-3" />
                </button>
              )}
            </div>
          </div>

          {/* Categorised list */}
          <div className="flex-1 overflow-y-auto p-1">
            {FAMILY_META.map((fm) => {
              const items = filtered.filter((n) => n.family === fm.key);
              if (items.length === 0) return null;
              return (
                <div key={fm.key} className="mb-1">
                  <div className="px-2 pt-1.5 pb-0.5">
                    <div className="text-[9px] uppercase tracking-widest text-mute/70 font-medium">
                      {fm.label}
                    </div>
                    <div className="text-[9px] text-mute/50 leading-snug">
                      {fm.hint}
                    </div>
                  </div>
                  {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 (
                      <button
                        key={n.kind}
                        type="button"
                        onClick={() => toggle(n.kind as NodeKind)}
                        className={`w-full flex items-center gap-2 px-2 py-1.5 rounded-md text-left transition-colors ${
                          isChecked
                            ? "bg-accent/10"
                            : "hover:bg-surfaceAlt"
                        }`}
                      >
                        {/* Checkbox */}
                        <span
                          className={`shrink-0 w-4 h-4 rounded flex items-center justify-center ${
                            isChecked
                              ? "bg-accent border border-accent text-canvas"
                              : "border border-edge bg-surface"
                          }`}
                        >
                          {isChecked && (
                            <Check className="w-2.5 h-2.5" strokeWidth={3} />
                          )}
                        </span>
                        {/* Icon — built-in uses Lucide; plugin uses
                            initials badge with custom color so users
                            can tell theirs apart. */}
                        {isPlugin ? (
                          <span
                            className="shrink-0 w-5 h-5 rounded flex items-center justify-center text-[9px] font-bold text-white"
                            style={{
                              backgroundColor: (n as PluginNodeSpec).pluginColor,
                            }}
                          >
                            {(n as PluginNodeSpec).initials}
                          </span>
                        ) : (
                          <span
                            className={`shrink-0 w-5 h-5 rounded border ${n.accentRing} bg-surface flex items-center justify-center ${n.accent}`}
                          >
                            <Icon className="w-2.5 h-2.5" strokeWidth={2} />
                          </span>
                        )}
                        {/* Label + tagline */}
                        <span className="flex-1 min-w-0">
                          <span className="text-[11px] text-ink block truncate">
                            {n.label}
                            {isPlugin && (
                              <span className="ml-1 text-[9px] uppercase tracking-wider text-mute/60">
                                plugin
                              </span>
                            )}
                          </span>
                          <span className="text-[9px] text-mute block truncate">
                            {n.tagline}
                          </span>
                        </span>
                        {/* On-canvas badge */}
                        {onCanvas && (
                          <span className="shrink-0 text-[9px] text-mute/50 border border-edge/50 rounded px-1 py-0.5">
                            on canvas
                          </span>
                        )}
                      </button>
                    );
                  })}
                </div>
              );
            })}
            {filtered.length === 0 && (
              <div className="py-6 text-center text-[11px] text-mute">
                No blocks match "{search}"
              </div>
            )}
          </div>

          {/* Footer with Add button */}
          <div className="p-2 border-t border-edge flex items-center justify-between gap-2">
            <span className="text-[10px] text-mute">
              {checked.size > 0
                ? `${checked.size} selected`
                : "Check blocks to add"}
            </span>
            <button
              type="button"
              onClick={addSelected}
              disabled={checked.size === 0}
              className={`inline-flex items-center gap-1 px-3 py-1 rounded-md text-[11px] font-medium transition-colors ${
                checked.size > 0
                  ? "bg-accent text-canvas hover:bg-accent/90"
                  : "bg-surfaceAlt text-mute cursor-not-allowed"
              }`}
            >
              <Plus className="w-3 h-3" />
              Add to canvas
            </button>
          </div>
        </div>
      )}
    </div>
  );
}