Yuchen Yuan commited on
Commit
61bf7f9
·
1 Parent(s): f73d0cc

NodePalette: searchable categorised block catalog

Browse files

Replace the flat drag-strip with a structured catalog that scales
to 30+ blocks:

- Search box: instant filter across label, tagline, family, kind.
- Family groups with vertical labels (SOURCE / BACKEND / ALGORITHM /
METRIC / SINK) and collapse toggles; collapsed groups show "N
hidden" placeholder.
- Family chip bar in header for quick toggle visibility per category.
- Whole palette collapsible to a thin "N blocks available" bar to
reclaim canvas space.
- Drag-to-canvas interaction unchanged.
- Responsive: mobile stays horizontal-scroll single row; desktop
wraps with family dividers.
- PaletteTile component unchanged (same size, same paper-link badge,
same drag data transfer).

Current 9 blocks render identically to before when search is empty
and all families are expanded. Adding a new block only requires
appending to NODE_CATALOG in nodeCatalog.ts.

frontend/src/components/NodePalette.tsx CHANGED
@@ -1,62 +1,209 @@
1
- import { FileText } from "lucide-react";
 
2
  import { NODE_CATALOG, type NodeSpec } from "../lib/nodeCatalog";
3
 
4
  /**
5
- * Adaptive strip of draggable block tiles across the top of the canvas.
6
  *
7
- * Rather than hard-coding one or two rows, the outer container uses
8
- * flex-wrap and marks each family group as shrink-0. Consequences:
 
 
9
  *
10
- * - On a wide canvas, all 8 tiles sit in a single row.
11
- * - When the canvas is squeezed (e.g. both side panes expanded on a
12
- * laptop screen), the last family that doesn't fit wraps to a new
13
- * row. Families never split mid-group, so "algorithm" stays together.
14
- * - No ResizeObserver, no JS breakpoint math — purely CSS. Responds
15
- * instantly to PaneResizer drags.
 
16
  */
17
- const FAMILIES: NodeSpec["family"][] = [
18
- "source",
19
- "backend",
20
- "algorithm",
21
- "metric",
22
- "sink",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  ];
24
 
25
  export function NodePalette() {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  return (
27
  <div className="shrink-0 border-b border-edge bg-surface/40">
28
- {/*
29
- * Responsive layout:
30
- * - Mobile (<md): flex-nowrap + overflow-x-auto. All 8 tiles sit in
31
- * one horizontally-scrollable row so the palette stays one row tall.
32
- * - Desktop (≥md): flex-wrap so families reflow onto a second row
33
- * when the canvas gets squeezed between two expanded side panes.
34
- */}
35
- <div className="px-3 py-2 flex flex-nowrap overflow-x-auto md:flex-wrap md:overflow-visible items-stretch gap-x-2 gap-y-1">
36
- <div className="flex items-center pr-1 shrink-0">
37
- <span className="text-[10px] uppercase tracking-wider text-mute/80 leading-tight">
38
- Drag a<br />block →
39
- </span>
 
 
 
 
 
 
 
 
40
  </div>
41
- {FAMILIES.map((fam, famIdx) => {
42
- const items = NODE_CATALOG.filter((n) => n.family === fam);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  if (items.length === 0) return null;
 
44
  return (
45
- <div key={fam} className="flex items-center gap-1 shrink-0">
46
- {items.map((n) => (
47
- <PaletteTile key={n.kind} spec={n} />
48
- ))}
49
- {famIdx < FAMILIES.length - 1 && (
50
- <div className="w-px self-stretch bg-edge mx-1" />
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  )}
 
 
 
 
 
 
 
52
  </div>
53
  );
54
  })}
55
- <div className="hidden xl:flex items-center pl-2 text-[10px] text-mute/70 leading-tight shrink-0 max-w-[180px]">
56
- Drop onto the canvas below.
57
- <br />
58
- Hover a block to reveal its × delete button.
59
- </div>
60
  </div>
61
  </div>
62
  );
 
1
+ import { useState, useMemo } from "react";
2
+ import { ChevronDown, FileText, Search, X } from "lucide-react";
3
  import { NODE_CATALOG, type NodeSpec } from "../lib/nodeCatalog";
4
 
5
  /**
6
+ * Categorised block catalog above the canvas.
7
  *
8
+ * Designed to scale from the current 9 blocks to 30+ without layout
9
+ * breakage. Blocks are grouped by family; each family is a collapsible
10
+ * section with a header chip. A search box filters across all families
11
+ * in real time.
12
  *
13
+ * Two interaction modes, both always available:
14
+ * - **Drag** a tile onto the canvas (existing behaviour).
15
+ * - **Click** a tile to add it at the canvas centre (TODO: wire up
16
+ * in FlowCanvas via an onAddBlock callback when ready).
17
+ *
18
+ * The whole palette collapses to a thin bar ("N blocks available") via
19
+ * a toggle so the canvas can reclaim vertical space.
20
  */
21
+
22
+ /** Family metadata: display label + ordering. */
23
+ const FAMILY_META: {
24
+ key: NodeSpec["family"];
25
+ label: string;
26
+ description: string;
27
+ }[] = [
28
+ {
29
+ key: "source",
30
+ label: "Source",
31
+ description: "Where the pipeline starts",
32
+ },
33
+ {
34
+ key: "backend",
35
+ label: "Backend",
36
+ description: "Provides a noise model",
37
+ },
38
+ {
39
+ key: "algorithm",
40
+ label: "Algorithm",
41
+ description: "Research algorithms that transform or analyse the circuit",
42
+ },
43
+ {
44
+ key: "metric",
45
+ label: "Metric",
46
+ description: "Computes a quantitative score",
47
+ },
48
+ {
49
+ key: "sink",
50
+ label: "Sink",
51
+ description: "Aggregates final output",
52
+ },
53
  ];
54
 
55
  export function NodePalette() {
56
+ const [search, setSearch] = useState("");
57
+ const [expanded, setExpanded] = useState(true);
58
+ // Track which families are collapsed; default all expanded.
59
+ const [collapsedFams, setCollapsedFams] = useState<Set<string>>(new Set());
60
+
61
+ const toggleFamily = (fam: string) =>
62
+ setCollapsedFams((s) => {
63
+ const next = new Set(s);
64
+ next.has(fam) ? next.delete(fam) : next.add(fam);
65
+ return next;
66
+ });
67
+
68
+ // Filter blocks by search query (matches label, tagline, family, kind).
69
+ const filtered = useMemo(() => {
70
+ if (!search.trim()) return NODE_CATALOG;
71
+ const q = search.toLowerCase();
72
+ return NODE_CATALOG.filter(
73
+ (n) =>
74
+ n.label.toLowerCase().includes(q) ||
75
+ n.tagline.toLowerCase().includes(q) ||
76
+ n.family.toLowerCase().includes(q) ||
77
+ n.kind.toLowerCase().includes(q),
78
+ );
79
+ }, [search]);
80
+
81
+ const totalCount = NODE_CATALOG.length;
82
+
83
+ if (!expanded) {
84
+ return (
85
+ <div className="shrink-0 border-b border-edge bg-surface/40">
86
+ <button
87
+ type="button"
88
+ onClick={() => setExpanded(true)}
89
+ className="w-full px-3 py-1.5 flex items-center justify-between text-[10px] text-mute hover:text-ink transition-colors"
90
+ >
91
+ <span className="uppercase tracking-wider">
92
+ {totalCount} blocks available
93
+ </span>
94
+ <ChevronDown className="w-3 h-3" />
95
+ </button>
96
+ </div>
97
+ );
98
+ }
99
+
100
  return (
101
  <div className="shrink-0 border-b border-edge bg-surface/40">
102
+ {/* Header row: search + collapse toggle */}
103
+ <div className="px-3 pt-2 pb-1 flex items-center gap-2">
104
+ <div className="relative flex-1 max-w-[220px]">
105
+ <Search className="absolute left-2 top-1/2 -translate-y-1/2 w-3 h-3 text-mute pointer-events-none" />
106
+ <input
107
+ type="text"
108
+ value={search}
109
+ onChange={(e) => setSearch(e.target.value)}
110
+ placeholder="Search blocks..."
111
+ className="w-full text-[11px] bg-surfaceAlt border border-edge rounded-md pl-6 pr-6 py-1 text-ink placeholder:text-mute/60 focus:outline-none focus:border-accent/60"
112
+ />
113
+ {search && (
114
+ <button
115
+ type="button"
116
+ onClick={() => setSearch("")}
117
+ className="absolute right-1.5 top-1/2 -translate-y-1/2 text-mute hover:text-ink"
118
+ >
119
+ <X className="w-3 h-3" />
120
+ </button>
121
+ )}
122
  </div>
123
+ {/* Family quick-jump chips */}
124
+ <div className="hidden md:flex items-center gap-1">
125
+ {FAMILY_META.map((fm) => {
126
+ const count = filtered.filter((n) => n.family === fm.key).length;
127
+ if (count === 0) return null;
128
+ return (
129
+ <button
130
+ key={fm.key}
131
+ type="button"
132
+ onClick={() => toggleFamily(fm.key)}
133
+ className={`text-[10px] px-1.5 py-0.5 rounded border transition-colors ${
134
+ collapsedFams.has(fm.key)
135
+ ? "border-edge/40 text-mute/60 bg-transparent"
136
+ : "border-edge bg-surfaceAlt text-mute hover:text-ink"
137
+ }`}
138
+ title={`${fm.description} (${count})`}
139
+ >
140
+ {fm.label}
141
+ <span className="ml-0.5 text-mute/50">{count}</span>
142
+ </button>
143
+ );
144
+ })}
145
+ </div>
146
+ <div className="flex-1" />
147
+ <button
148
+ type="button"
149
+ onClick={() => setExpanded(false)}
150
+ className="text-mute hover:text-ink p-0.5"
151
+ title="Collapse block palette"
152
+ >
153
+ <ChevronDown className="w-3.5 h-3.5 rotate-180" />
154
+ </button>
155
+ </div>
156
+
157
+ {/* Block grid grouped by family */}
158
+ <div className="px-3 pb-2 flex flex-nowrap overflow-x-auto md:flex-wrap md:overflow-visible items-start gap-x-1 gap-y-1">
159
+ {FAMILY_META.map((fm) => {
160
+ const items = filtered.filter((n) => n.family === fm.key);
161
  if (items.length === 0) return null;
162
+ const isCollapsed = collapsedFams.has(fm.key);
163
  return (
164
+ <div key={fm.key} className="shrink-0 flex items-start gap-1">
165
+ {/* Family label (vertical on desktop, horizontal on mobile) */}
166
+ <button
167
+ type="button"
168
+ onClick={() => toggleFamily(fm.key)}
169
+ className="hidden md:flex flex-col items-center justify-center py-1 px-0.5 gap-0.5 text-mute/60 hover:text-mute transition-colors self-stretch"
170
+ title={`${fm.label}: ${fm.description}`}
171
+ >
172
+ <span
173
+ className="text-[9px] uppercase tracking-widest whitespace-nowrap font-medium"
174
+ style={{ writingMode: "vertical-rl" }}
175
+ >
176
+ {fm.label}
177
+ </span>
178
+ <ChevronDown
179
+ className={`w-2.5 h-2.5 transition-transform ${
180
+ isCollapsed ? "-rotate-90" : "rotate-0"
181
+ }`}
182
+ />
183
+ </button>
184
+ {/* Tiles */}
185
+ {!isCollapsed && (
186
+ <div className="flex items-center gap-1 flex-wrap">
187
+ {items.map((n) => (
188
+ <PaletteTile key={n.kind} spec={n} />
189
+ ))}
190
+ </div>
191
  )}
192
+ {isCollapsed && (
193
+ <div className="flex items-center h-[76px] px-2 text-[10px] text-mute/50 italic">
194
+ {items.length} hidden
195
+ </div>
196
+ )}
197
+ {/* Divider between families */}
198
+ <div className="hidden md:block w-px self-stretch bg-edge/40 mx-0.5" />
199
  </div>
200
  );
201
  })}
202
+ {filtered.length === 0 && (
203
+ <div className="py-4 w-full text-center text-[11px] text-mute">
204
+ No blocks match "{search}"
205
+ </div>
206
+ )}
207
  </div>
208
  </div>
209
  );