@charcuterie/ui 4.28.4 → 4.30.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/dist/CatalogPicker/CatalogPicker.d.ts +110 -0
  2. package/dist/CatalogPicker/CatalogPicker.d.ts.map +1 -0
  3. package/dist/CatalogPicker/CatalogPicker.js +155 -0
  4. package/dist/CatalogPicker/CatalogPicker.js.map +1 -0
  5. package/dist/CatalogPicker/CatalogPickerMark.d.ts +29 -0
  6. package/dist/CatalogPicker/CatalogPickerMark.d.ts.map +1 -0
  7. package/dist/CatalogPicker/CatalogPickerMark.js +35 -0
  8. package/dist/CatalogPicker/CatalogPickerMark.js.map +1 -0
  9. package/dist/CatalogPicker/CatalogPickerOption.d.ts +40 -0
  10. package/dist/CatalogPicker/CatalogPickerOption.d.ts.map +1 -0
  11. package/dist/CatalogPicker/CatalogPickerOption.js +43 -0
  12. package/dist/CatalogPicker/CatalogPickerOption.js.map +1 -0
  13. package/dist/CatalogPicker/CatalogPickerPanel.d.ts +60 -0
  14. package/dist/CatalogPicker/CatalogPickerPanel.d.ts.map +1 -0
  15. package/dist/CatalogPicker/CatalogPickerPanel.js +409 -0
  16. package/dist/CatalogPicker/CatalogPickerPanel.js.map +1 -0
  17. package/dist/CatalogPicker/CatalogPickerRail.d.ts +50 -0
  18. package/dist/CatalogPicker/CatalogPickerRail.d.ts.map +1 -0
  19. package/dist/CatalogPicker/CatalogPickerRail.js +114 -0
  20. package/dist/CatalogPicker/CatalogPickerRail.js.map +1 -0
  21. package/dist/CatalogPicker/catalogPickerDrawings.d.ts +10 -0
  22. package/dist/CatalogPicker/catalogPickerDrawings.d.ts.map +1 -0
  23. package/dist/CatalogPicker/catalogPickerDrawings.js +29 -0
  24. package/dist/CatalogPicker/catalogPickerDrawings.js.map +1 -0
  25. package/dist/CatalogPicker/catalogPickerView.d.ts +185 -0
  26. package/dist/CatalogPicker/catalogPickerView.d.ts.map +1 -0
  27. package/dist/CatalogPicker/catalogPickerView.js +245 -0
  28. package/dist/CatalogPicker/catalogPickerView.js.map +1 -0
  29. package/dist/index.d.ts +2 -0
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.js +1 -0
  32. package/dist/index.js.map +1 -1
  33. package/package.json +1 -1
  34. package/src/CatalogPicker/CatalogPicker.mdx +192 -0
  35. package/src/CatalogPicker/CatalogPicker.stories.tsx +218 -0
  36. package/src/CatalogPicker/CatalogPicker.test.tsx +1592 -0
  37. package/src/CatalogPicker/CatalogPicker.tsx +387 -0
  38. package/src/CatalogPicker/CatalogPickerMark.tsx +89 -0
  39. package/src/CatalogPicker/CatalogPickerOption.tsx +151 -0
  40. package/src/CatalogPicker/CatalogPickerPanel.tsx +780 -0
  41. package/src/CatalogPicker/CatalogPickerRail.tsx +214 -0
  42. package/src/CatalogPicker/catalogPicker.storyHelpers.tsx +276 -0
  43. package/src/CatalogPicker/catalogPickerDrawings.tsx +51 -0
  44. package/src/CatalogPicker/catalogPickerView.test.ts +365 -0
  45. package/src/CatalogPicker/catalogPickerView.ts +476 -0
  46. package/src/Dialog/Dialog.test.tsx +20 -289
  47. package/src/Field/Field.test.tsx +11 -405
  48. package/src/index.ts +6 -0
  49. package/src/sourceRules.test.ts +5 -1
@@ -0,0 +1,476 @@
1
+ import type { ReactNode } from "react"
2
+
3
+ /**
4
+ * One rail tab's subject: a provider, a vendor, an account — whatever
5
+ * the catalog is browsed by.
6
+ */
7
+ export type CatalogPickerGroup = {
8
+ /**
9
+ * Draws the initials badge (when the group has no `icon`) and rings
10
+ * the icon. Any CSS color. It is a ring, never the text or the fill
11
+ * behind text, so it cannot fail the contrast gate.
12
+ */
13
+ accentColor?: string
14
+ /** The app's own icon. Charcuterie ships none; without one the group gets an initials badge. */
15
+ icon?: ReactNode
16
+ /** Unique across `groups`. A later group with an `id` already used is dropped, with a console warning. */
17
+ id: string
18
+ /** The rail tab's accessible name and tooltip, and the heading over this group's search results. */
19
+ label: string
20
+ }
21
+
22
+ export type CatalogPickerItem = {
23
+ /** Secondary text beside the label — an account, a slug. Searched. */
24
+ description?: string
25
+ /**
26
+ * The group this item is browsed under, or `null` for none. An item
27
+ * with no group — or with a `groupId` that names no group — is
28
+ * listed on every tab, after the pinned-start rows, so it is never
29
+ * out of reach.
30
+ */
31
+ groupId: string | null
32
+ /** Overrides the group's icon or badge for this row. */
33
+ icon?: ReactNode
34
+ isDisabled?: boolean
35
+ /**
36
+ * The row's visible label and the trigger's text, and the first part
37
+ * of the row's accessible name (then the description, then the
38
+ * group). Searched.
39
+ */
40
+ label: string
41
+ /**
42
+ * Shown at the top (`"start"`) or the bottom (`"end"`) of every rail
43
+ * view, and of the search results when it matches — a "Default"
44
+ * choice that belongs to no tab.
45
+ */
46
+ pinned?: "end" | "start"
47
+ /** Unique across `items`. A later item with a `value` already used is dropped, with a console warning. */
48
+ value: string
49
+ }
50
+
51
+ /**
52
+ * A tab on the rail. Keys are prefixed so a group whose `id` is
53
+ * `"favorites"` cannot collide with the favorites tab.
54
+ */
55
+ export type CatalogPickerTab =
56
+ | {
57
+ key: typeof FAVORITES_TAB_KEY
58
+ kind: "favorites"
59
+ label: string
60
+ }
61
+ | {
62
+ group: CatalogPickerGroup
63
+ key: string
64
+ kind: "group"
65
+ label: string
66
+ }
67
+
68
+ /**
69
+ * One run of rows. A section with a `heading` is a labelled
70
+ * `role="group"` inside the listbox; one without is a plain run of
71
+ * options (pinned rows, and a browsing tab's own rows).
72
+ */
73
+ export type CatalogPickerSection = {
74
+ heading?: string
75
+ items: readonly CatalogPickerItem[]
76
+ key: string
77
+ }
78
+
79
+ export const FAVORITES_TAB_KEY = "favorites:"
80
+
81
+ export const toGroupTabKey = (groupId: string) =>
82
+ `group:${groupId}`
83
+
84
+ /**
85
+ * The catalog with every repeated item `value` and group `id` dropped:
86
+ * **the first one wins**. A value names one row, so two rows with one
87
+ * value would both read as chosen, and the trigger, the Favorites tab
88
+ * and the highlight could each settle on a different one. The dropped
89
+ * values and ids are returned so the picker can warn about them.
90
+ */
91
+ export const toUniqueCatalog = ({
92
+ groups,
93
+ items,
94
+ }: {
95
+ groups: readonly CatalogPickerGroup[]
96
+ items: readonly CatalogPickerItem[]
97
+ }) => {
98
+ const groupIds = new Set<string>()
99
+ const duplicateGroupIds: string[] = []
100
+
101
+ const uniqueGroups = groups.filter((group) => {
102
+ if (groupIds.has(group.id)) {
103
+ duplicateGroupIds.push(group.id)
104
+
105
+ return false
106
+ }
107
+
108
+ groupIds.add(group.id)
109
+
110
+ return true
111
+ })
112
+
113
+ const itemValues = new Set<string>()
114
+ const duplicateItemValues: string[] = []
115
+
116
+ const uniqueItems = items.filter((item) => {
117
+ if (itemValues.has(item.value)) {
118
+ duplicateItemValues.push(item.value)
119
+
120
+ return false
121
+ }
122
+
123
+ itemValues.add(item.value)
124
+
125
+ return true
126
+ })
127
+
128
+ return {
129
+ duplicateGroupIds,
130
+ duplicateItemValues,
131
+ groups:
132
+ duplicateGroupIds.length === 0
133
+ ? groups
134
+ : uniqueGroups,
135
+ items:
136
+ duplicateItemValues.length === 0
137
+ ? items
138
+ : uniqueItems,
139
+ }
140
+ }
141
+
142
+ /**
143
+ * Whether a row is listed on every tab rather than under one: a pinned
144
+ * row, and an unpinned row with no group (or a `groupId` that names no
145
+ * group). Without this an ungrouped row would be on no tab at all, and
146
+ * reachable only by search.
147
+ */
148
+ export const isOnEveryTab = ({
149
+ groupIds,
150
+ item,
151
+ }: {
152
+ groupIds: ReadonlySet<string>
153
+ item: CatalogPickerItem
154
+ }) =>
155
+ item.pinned !== undefined ||
156
+ item.groupId === null ||
157
+ !groupIds.has(item.groupId)
158
+
159
+ /**
160
+ * The words of a query, lowercased. Whitespace separates them; an
161
+ * all-space query has none, and is treated as no query.
162
+ */
163
+ export const toQueryWords = (query: string) =>
164
+ query
165
+ .toLowerCase()
166
+ .split(/\s+/u)
167
+ .filter((word) => word.length > 0)
168
+
169
+ /**
170
+ * Substring match, case-insensitive, every word required: "opus 5"
171
+ * finds "Claude Opus 5.5", and so does "5 opus". The text searched is
172
+ * the label, the description and the group's label, so typing a
173
+ * provider name lists that provider's rows.
174
+ */
175
+ export const isCatalogItemMatch = ({
176
+ groupLabel,
177
+ item,
178
+ words,
179
+ }: {
180
+ groupLabel: string | undefined
181
+ item: CatalogPickerItem
182
+ words: readonly string[]
183
+ }) => {
184
+ const haystack = [
185
+ item.label,
186
+ item.description ?? "",
187
+ groupLabel ?? "",
188
+ ]
189
+ .join("\n")
190
+ .toLowerCase()
191
+
192
+ return words.every((word) => haystack.includes(word))
193
+ }
194
+
195
+ /**
196
+ * The favorites, resolved to items in `favorites` order, once each. A
197
+ * value that names no item is skipped. A pinned or ungrouped favorite
198
+ * is included here — it counts toward showing the Favorites tab — but
199
+ * the tab lists it only once, where every tab lists it.
200
+ */
201
+ export const resolveFavoriteItems = ({
202
+ favorites,
203
+ items,
204
+ }: {
205
+ favorites: readonly string[]
206
+ items: readonly CatalogPickerItem[]
207
+ }) => {
208
+ const byValue = new Map<string, CatalogPickerItem>()
209
+
210
+ for (const item of items) {
211
+ if (!byValue.has(item.value)) {
212
+ byValue.set(item.value, item)
213
+ }
214
+ }
215
+
216
+ const seen = new Set<string>()
217
+
218
+ return favorites.flatMap((value) => {
219
+ const item = byValue.get(value)
220
+
221
+ if (item === undefined || seen.has(value)) {
222
+ return []
223
+ }
224
+
225
+ seen.add(value)
226
+
227
+ return [item]
228
+ })
229
+ }
230
+
231
+ /**
232
+ * The rail, top to bottom: the favorites tab when any favorite
233
+ * resolves, then one tab per group that has a row of its own, in
234
+ * `groups` order.
235
+ */
236
+ export const buildCatalogTabs = ({
237
+ favorites,
238
+ favoritesLabel,
239
+ groups,
240
+ items,
241
+ }: {
242
+ favorites: readonly string[]
243
+ favoritesLabel: string
244
+ groups: readonly CatalogPickerGroup[]
245
+ items: readonly CatalogPickerItem[]
246
+ }): CatalogPickerTab[] => {
247
+ const groupIdsWithRows = new Set(
248
+ items
249
+ .filter((item) => item.pinned === undefined)
250
+ .map((item) => item.groupId),
251
+ )
252
+
253
+ const groupTabs = groups
254
+ .filter((group) => groupIdsWithRows.has(group.id))
255
+ .map(
256
+ (group): CatalogPickerTab => ({
257
+ group,
258
+ key: toGroupTabKey(group.id),
259
+ kind: "group",
260
+ label: group.label,
261
+ }),
262
+ )
263
+
264
+ const hasFavorites =
265
+ resolveFavoriteItems({ favorites, items }).length > 0
266
+
267
+ return hasFavorites
268
+ ? [
269
+ {
270
+ key: FAVORITES_TAB_KEY,
271
+ kind: "favorites",
272
+ label: favoritesLabel,
273
+ },
274
+ ...groupTabs,
275
+ ]
276
+ : groupTabs
277
+ }
278
+
279
+ /**
280
+ * Where a fresh open lands: Favorites when the chosen value is a
281
+ * favorite or a row on every tab (and there are favorites), else the
282
+ * chosen row's group, else Favorites, else the first group.
283
+ * `undefined` only for an empty rail.
284
+ */
285
+ export const pickInitialTabKey = ({
286
+ favorites,
287
+ items,
288
+ tabs,
289
+ value,
290
+ }: {
291
+ favorites: readonly string[]
292
+ items: readonly CatalogPickerItem[]
293
+ tabs: readonly CatalogPickerTab[]
294
+ value: string | undefined
295
+ }): string | undefined => {
296
+ const hasFavoritesTab = tabs.some(
297
+ (tab) => tab.kind === "favorites",
298
+ )
299
+
300
+ const groupIds = new Set(
301
+ tabs.flatMap((tab) =>
302
+ tab.kind === "group" ? [tab.group.id] : [],
303
+ ),
304
+ )
305
+
306
+ const chosen =
307
+ value === undefined
308
+ ? undefined
309
+ : items.find((item) => item.value === value)
310
+
311
+ if (
312
+ hasFavoritesTab &&
313
+ chosen !== undefined &&
314
+ (isOnEveryTab({ groupIds, item: chosen }) ||
315
+ favorites.includes(chosen.value))
316
+ ) {
317
+ return FAVORITES_TAB_KEY
318
+ }
319
+
320
+ if (chosen?.groupId != null) {
321
+ const groupKey = toGroupTabKey(chosen.groupId)
322
+
323
+ if (tabs.some((tab) => tab.key === groupKey)) {
324
+ return groupKey
325
+ }
326
+ }
327
+
328
+ return tabs[0]?.key
329
+ }
330
+
331
+ /**
332
+ * The rows the list shows, as sections. Each row appears once.
333
+ *
334
+ * **Browsing** (empty query): the pinned-start rows, the ungrouped
335
+ * rows, the tab's own rows (favorites in `favorites` order, a group's
336
+ * rows in `items` order), the pinned-end rows. The Favorites tab's own
337
+ * rows leave out a favorite that is already pinned or ungrouped, since
338
+ * every tab lists those. An empty rail browses every unpinned row.
339
+ *
340
+ * **Searching**: matches across every item, whatever tab is current —
341
+ * the pinned-start matches, the ungrouped matches, one headed section
342
+ * per group in `groups` order, the pinned-end matches. Favorites are
343
+ * not their own section in search: a favorite is found under its
344
+ * group, once.
345
+ */
346
+ export const buildCatalogSections = ({
347
+ favorites,
348
+ groups,
349
+ items,
350
+ query,
351
+ tab,
352
+ }: {
353
+ favorites: readonly string[]
354
+ groups: readonly CatalogPickerGroup[]
355
+ items: readonly CatalogPickerItem[]
356
+ query: string
357
+ tab: CatalogPickerTab | undefined
358
+ }): CatalogPickerSection[] => {
359
+ const words = toQueryWords(query)
360
+
361
+ const groupById = new Map(
362
+ groups.map((group) => [group.id, group] as const),
363
+ )
364
+
365
+ const groupIds = new Set(groupById.keys())
366
+
367
+ const groupLabelOf = (item: CatalogPickerItem) =>
368
+ item.groupId === null
369
+ ? undefined
370
+ : groupById.get(item.groupId)?.label
371
+
372
+ const isSearching = words.length > 0
373
+
374
+ const shown = isSearching
375
+ ? items.filter((item) =>
376
+ isCatalogItemMatch({
377
+ groupLabel: groupLabelOf(item),
378
+ item,
379
+ words,
380
+ }),
381
+ )
382
+ : items
383
+
384
+ const pinnedStart = shown.filter(
385
+ (item) => item.pinned === "start",
386
+ )
387
+
388
+ const pinnedEnd = shown.filter(
389
+ (item) => item.pinned === "end",
390
+ )
391
+
392
+ const unpinned = shown.filter(
393
+ (item) => item.pinned === undefined,
394
+ )
395
+
396
+ const ungrouped: CatalogPickerSection = {
397
+ items: unpinned.filter((item) =>
398
+ isOnEveryTab({ groupIds, item }),
399
+ ),
400
+ key: "ungrouped",
401
+ }
402
+
403
+ const middle = ((): CatalogPickerSection[] => {
404
+ if (isSearching) {
405
+ return [
406
+ ungrouped,
407
+ ...groups.map(
408
+ (group): CatalogPickerSection => ({
409
+ heading: group.label,
410
+ items: unpinned.filter(
411
+ (item) => item.groupId === group.id,
412
+ ),
413
+ key: toGroupTabKey(group.id),
414
+ }),
415
+ ),
416
+ ]
417
+ }
418
+
419
+ if (tab === undefined) {
420
+ return [{ items: unpinned, key: "all" }]
421
+ }
422
+
423
+ return [
424
+ ungrouped,
425
+ {
426
+ items:
427
+ tab.kind === "favorites"
428
+ ? resolveFavoriteItems({
429
+ favorites,
430
+ items,
431
+ }).filter(
432
+ (item) => !isOnEveryTab({ groupIds, item }),
433
+ )
434
+ : unpinned.filter(
435
+ (item) => item.groupId === tab.group.id,
436
+ ),
437
+ key: tab.key,
438
+ },
439
+ ]
440
+ })()
441
+
442
+ return [
443
+ { items: pinnedStart, key: "pinned-start" },
444
+ ...middle,
445
+ { items: pinnedEnd, key: "pinned-end" },
446
+ ].filter((section) => section.items.length > 0)
447
+ }
448
+
449
+ /**
450
+ * A row's or the trigger's accessible name: its text, then the group
451
+ * in words — `"Opus, Team Two"`. The text's trailing spaces go, so the
452
+ * comma this adds never follows a space; no group, or a group whose
453
+ * label is blank, leaves the text as it is, so this never adds a bare
454
+ * comma.
455
+ */
456
+ export const toGroupedName = (
457
+ text: string,
458
+ group: CatalogPickerGroup | undefined,
459
+ ) =>
460
+ group === undefined || group.label.trim() === ""
461
+ ? text
462
+ : `${text.trimEnd()}, ${group.label}`
463
+
464
+ /**
465
+ * A badge's text: the first letter or digit of each of the first two
466
+ * words that have one, so "Northwind" is "N", "Contoso Labs" is "CL",
467
+ * and "Contoso (Work)" is "CW" rather than "C(".
468
+ */
469
+ export const toInitials = (label: string) =>
470
+ label
471
+ .split(/\s+/u)
472
+ .map((word) => word.match(/[\p{L}\p{N}]/u)?.[0])
473
+ .filter((initial) => initial !== undefined)
474
+ .slice(0, 2)
475
+ .join("")
476
+ .toUpperCase()