torch-glare 2.4.5 → 2.5.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 (142) hide show
  1. package/apps/lib/components/DataViews/{badgeAdapter.ts → badge.ts} +2 -2
  2. package/apps/lib/components/DataViews/cell.tsx +324 -0
  3. package/apps/lib/components/DataViews/context.ts +144 -0
  4. package/apps/lib/components/DataViews/data-views.tsx +383 -0
  5. package/apps/lib/components/DataViews/filters/children.tsx +98 -0
  6. package/apps/lib/components/DataViews/filters/custom.tsx +34 -0
  7. package/apps/lib/components/DataViews/filters/filters.tsx +163 -0
  8. package/apps/lib/components/DataViews/filters/index.ts +4 -0
  9. package/apps/lib/components/DataViews/filters/labelled.tsx +20 -0
  10. package/apps/lib/components/DataViews/filters/presets.tsx +65 -0
  11. package/apps/lib/components/DataViews/filters/summary.tsx +65 -0
  12. package/apps/lib/components/DataViews/filters/sync.tsx +35 -0
  13. package/apps/lib/components/DataViews/filters/values.ts +173 -0
  14. package/apps/lib/components/DataViews/header.tsx +217 -0
  15. package/apps/lib/components/DataViews/hooks/index.ts +5 -0
  16. package/apps/lib/components/DataViews/hooks/useActiveRow.ts +22 -0
  17. package/apps/lib/components/DataViews/hooks/useControllable.ts +52 -0
  18. package/apps/lib/components/DataViews/index.ts +74 -26
  19. package/apps/lib/components/DataViews/panel/columns.tsx +153 -0
  20. package/apps/lib/components/DataViews/panel/controls.tsx +106 -0
  21. package/apps/lib/components/DataViews/panel/index.ts +3 -0
  22. package/apps/lib/components/DataViews/panel/panel.tsx +164 -0
  23. package/apps/lib/components/DataViews/panel/saved-views.tsx +67 -0
  24. package/apps/lib/components/DataViews/panel/section.tsx +79 -0
  25. package/apps/lib/components/DataViews/panel/sort.tsx +42 -0
  26. package/apps/lib/components/DataViews/panel/tab.tsx +31 -0
  27. package/apps/lib/components/DataViews/slots.ts +63 -0
  28. package/apps/lib/components/DataViews/states.tsx +38 -0
  29. package/apps/lib/components/DataViews/types.ts +485 -178
  30. package/apps/lib/components/DataViews/views/board-view.tsx +379 -0
  31. package/apps/lib/components/DataViews/views/card-rows.tsx +36 -0
  32. package/apps/lib/components/DataViews/views/inbox-view.tsx +257 -0
  33. package/apps/lib/components/DataViews/views/pane-views.tsx +192 -0
  34. package/apps/lib/components/DataViews/views/table-view.tsx +426 -0
  35. package/apps/lib/components/DataViews/views/tree-view.tsx +365 -0
  36. package/apps/lib/components/FormBuilder/context.ts +20 -6
  37. package/apps/lib/components/FormBuilder/field-kind.ts +28 -0
  38. package/apps/lib/components/FormBuilder/fields/DateField.tsx +3 -3
  39. package/apps/lib/components/FormBuilder/fields/FieldShell.tsx +7 -6
  40. package/apps/lib/components/FormBuilder/fields/PhoneField.tsx +7 -6
  41. package/apps/lib/components/FormBuilder/fields/SelectField.tsx +7 -7
  42. package/apps/lib/components/FormBuilder/fields/TableField.tsx +1 -1
  43. package/apps/lib/components/FormBuilder/fields/TextField.tsx +9 -9
  44. package/apps/lib/components/FormBuilder/form-builder.tsx +55 -3
  45. package/apps/lib/components/FormBuilder/index.ts +3 -1
  46. package/apps/lib/components/FormBuilder/types.ts +40 -0
  47. package/apps/lib/components/TabSwitch.tsx +16 -4
  48. package/apps/lib/components/TreeFolder/TreeFolder.tsx +6 -3
  49. package/apps/lib/components/TreeFolder/TreeFolderRow.tsx +16 -14
  50. package/apps/lib/components/TreeFolder/index.ts +1 -1
  51. package/apps/lib/components/TreeFolder/useTreeFolderDnD.ts +70 -207
  52. package/apps/lib/hooks/useDragDrop.tsx +365 -0
  53. package/apps/lib/hooks/useInfiniteScroll.ts +108 -0
  54. package/apps/lib/registry.json +159 -4
  55. package/apps/lib/tsconfig.tsbuildinfo +1 -1
  56. package/apps/lib/utils/dataViews/path.ts +67 -0
  57. package/apps/lib/utils/dataViews/query.ts +73 -0
  58. package/apps/lib/utils/dataViews/types.ts +187 -0
  59. package/docs/components/breadcrumb.md +1 -1
  60. package/docs/components/button-group.md +1 -1
  61. package/docs/components/button.md +1 -1
  62. package/docs/components/card.md +1 -1
  63. package/docs/components/checkbox.md +1 -1
  64. package/docs/components/data-views/backend-response.md +324 -0
  65. package/docs/components/data-views/examples/a11y-rtl.md +250 -0
  66. package/docs/components/data-views/examples/api-orders-route.md +130 -0
  67. package/docs/components/data-views/examples/fields.md +362 -0
  68. package/docs/components/data-views/examples/filters.md +308 -0
  69. package/docs/components/data-views/examples/inbox-routing.md +218 -0
  70. package/docs/components/data-views/examples/index.md +29 -0
  71. package/docs/components/data-views/examples/overview.md +244 -0
  72. package/docs/components/data-views/examples/panel.md +212 -0
  73. package/docs/components/data-views/examples/scale.md +231 -0
  74. package/docs/components/data-views/examples/server-side.md +210 -0
  75. package/docs/components/data-views/examples/state.md +250 -0
  76. package/docs/components/data-views/examples/tree-custom.md +388 -0
  77. package/docs/components/data-views/examples/view-registry.md +313 -0
  78. package/docs/components/data-views/examples/views.md +534 -0
  79. package/docs/components/data-views/guide.md +405 -0
  80. package/docs/components/data-views/index.md +1504 -0
  81. package/docs/components/data-views/migration.md +79 -0
  82. package/docs/components/date-picker.md +0 -1
  83. package/docs/components/form-builder.md +3 -2
  84. package/docs/components/form-renderer.md +2 -1
  85. package/docs/components/form.md +1 -1
  86. package/docs/components/input-field.md +1 -1
  87. package/docs/components/input-otp.md +1 -1
  88. package/docs/components/input.md +1 -1
  89. package/docs/components/labeled-check-box.md +1 -1
  90. package/docs/components/labeled-radio.md +1 -1
  91. package/docs/components/radio-card.md +1 -1
  92. package/docs/components/radio.md +1 -1
  93. package/docs/components/search-field.md +1 -1
  94. package/docs/components/section-block.md +1 -1
  95. package/docs/components/select.md +1 -1
  96. package/docs/components/simple-select.md +1 -1
  97. package/docs/components/switch.md +1 -1
  98. package/docs/components/tab-switch.md +1 -1
  99. package/docs/components/table.md +1 -1
  100. package/docs/components/text-editor.md +1 -1
  101. package/docs/components/textarea.md +1 -1
  102. package/docs/components/toggle-button.md +1 -1
  103. package/docs/components/toggle.md +1 -1
  104. package/docs/components/tree-folder.md +110 -0
  105. package/docs/how-to/forms-with-form-builder.md +2 -2
  106. package/docs/reference/components.md +16 -6
  107. package/docs/tutorials/component-composition.md +11 -13
  108. package/package.json +3 -2
  109. package/apps/lib/components/DataViews/DataViewRadio.tsx +0 -49
  110. package/apps/lib/components/DataViews/DataViewsConfigPanel.tsx +0 -393
  111. package/apps/lib/components/DataViews/DataViewsHeader.tsx +0 -207
  112. package/apps/lib/components/DataViews/DataViewsLayout.tsx +0 -332
  113. package/apps/lib/components/DataViews/FilterPanel.tsx +0 -493
  114. package/apps/lib/components/DataViews/HeaderSearch.tsx +0 -93
  115. package/apps/lib/components/DataViews/InboxView.tsx +0 -463
  116. package/apps/lib/components/DataViews/InboxViewCard.tsx +0 -127
  117. package/apps/lib/components/DataViews/KanbanView.tsx +0 -336
  118. package/apps/lib/components/DataViews/PanelControls.tsx +0 -39
  119. package/apps/lib/components/DataViews/SettingsPanel.tsx +0 -279
  120. package/apps/lib/components/DataViews/TableView.tsx +0 -212
  121. package/apps/lib/components/DataViews/TreeView.tsx +0 -364
  122. package/apps/lib/components/DataViews/fieldRenderers.tsx +0 -299
  123. package/apps/lib/components/DataViews/filters/DatePickerRangeFilter.tsx +0 -87
  124. package/apps/lib/components/DataViews/filters/DateRangePopover.tsx +0 -120
  125. package/apps/lib/components/DataViews/filters/PresetChips.tsx +0 -45
  126. package/apps/lib/components/DataViews/filters/RangeSliderWithInputs.tsx +0 -165
  127. package/apps/lib/components/DataViews/tree/TreeDrawer.tsx +0 -50
  128. package/apps/lib/components/DataViews/tree/TreeSidebar.tsx +0 -74
  129. package/apps/lib/hooks/useDataViewsState.ts +0 -175
  130. package/apps/lib/utils/dataViews/columnUtils.ts +0 -132
  131. package/apps/lib/utils/dataViews/fieldUtils.ts +0 -197
  132. package/apps/lib/utils/dataViews/nestedDataUtils.tsx +0 -371
  133. package/apps/lib/utils/dataViews/pathUtils.ts +0 -139
  134. package/apps/lib/utils/dataViews/rangeUtils.ts +0 -234
  135. package/apps/lib/utils/dataViews/treeUtils.ts +0 -396
  136. package/docs/components/data-views-config-panel.md +0 -208
  137. package/docs/components/data-views-layout.md +0 -291
  138. package/docs/components/inbox-view.md +0 -170
  139. package/docs/components/kanban-view.md +0 -135
  140. package/docs/components/table-view.md +0 -141
  141. package/docs/components/tree-view.md +0 -147
  142. package/docs/how-to/data-views-from-backend-response.md +0 -194
@@ -0,0 +1,379 @@
1
+ "use client";
2
+
3
+ import React, { useMemo } from "react";
4
+ import { MoreHorizontal } from "lucide-react";
5
+ import { cn } from "../../../utils/cn";
6
+ import type { ColumnColor, RowGroup } from "../../../utils/dataViews/types";
7
+ import { Button } from "../../Button";
8
+ import { DataViewCard } from "../../../layouts/DataViewCard";
9
+ import {
10
+ DragGhost,
11
+ DragList,
12
+ useDragDrop,
13
+ useDragItem,
14
+ useDropContainer,
15
+ } from "../../../hooks/useDragDrop";
16
+ import { Cell } from "../cell";
17
+ import { SkeletonBar, skeletonKeys } from "../states";
18
+ import { buildCardRows } from "./card-rows";
19
+ import { useInfiniteScroll } from "../../../hooks/useInfiniteScroll";
20
+ import { useDataViewsData, useDataViewsView } from "../context";
21
+ import { markView } from "../slots";
22
+ import type { BoardViewProps } from "../types";
23
+
24
+ /**
25
+ * The Figma column pills are deeply saturated dark fills (#131415, #330C69, #532200, #002F66).
26
+ * Each is matched to the closest raw-colour token; purple has no close presentation-layer match,
27
+ * so it stays the exact Figma hex.
28
+ */
29
+ const COLUMN_BG: Record<ColumnColor, string> = {
30
+ gray: "bg-black-900",
31
+ purple: "bg-[#330C69]",
32
+ orange: "bg-orange-900",
33
+ blue: "bg-blue-sparkle-900",
34
+ green: "bg-green-cyan-900",
35
+ red: "bg-red-orange-900",
36
+ };
37
+
38
+ /**
39
+ * `DataViews.Board` — a kanban board.
40
+ *
41
+ * You hand it `groups`; it never groups rows itself. That is deliberate: grouping is a query, and
42
+ * which statuses exist (including the empty ones you still want a column for) is knowledge the
43
+ * board does not have.
44
+ *
45
+ * Dragging emits `onRowMove` and **does not move the card**. The card moves when you update
46
+ * `rows`/`groups` in response — so a failed save leaves the board showing the truth instead of a
47
+ * lie. If nothing seems to happen on drop, that round-trip is what is missing.
48
+ */
49
+ function BoardViewImpl({
50
+ groups,
51
+ titlePath,
52
+ renderCard,
53
+ onRowMove,
54
+ onColumnAction,
55
+ className,
56
+ }: BoardViewProps) {
57
+ const { visibleFields, getRowId, loading } = useDataViewsData();
58
+ const { activeId, setActiveId } = useDataViewsView();
59
+
60
+ const titleField = titlePath ? visibleFields.find((f) => f.path === titlePath) : visibleFields[0];
61
+ const bodyFields = visibleFields.filter((f) => f.path !== titleField?.path);
62
+
63
+ /**
64
+ * Where every card currently sits. A drop reports only what it landed *on* — which may be a
65
+ * column or another card — so this is what turns that into "which column, at what index".
66
+ */
67
+ const placement = useMemo(() => {
68
+ const map = new Map<string, { group: string; index: number }>();
69
+ for (const group of groups) {
70
+ group.rows.forEach((row, index) => {
71
+ map.set(getRowId(row, index), { group: group.id, index });
72
+ });
73
+ }
74
+ return map;
75
+ }, [groups, getRowId]);
76
+
77
+ const { DndContext, contextProps, activeId: draggingId, overId } = useDragDrop({
78
+ mode: "board",
79
+ disabled: !onRowMove,
80
+ containerOf: (id) => placement.get(id)?.group ?? null,
81
+ onMove: ({ id, from, to }) => {
82
+ if (!to) return;
83
+ // Dropped on a card: take that card's column and slot in at its position. Dropped on the
84
+ // column itself — which is what an empty column offers — append.
85
+ const onCard = placement.get(to);
86
+ onRowMove?.({
87
+ id,
88
+ from,
89
+ to: onCard ? onCard.group : to,
90
+ ...(onCard ? { index: onCard.index } : null),
91
+ });
92
+ },
93
+ });
94
+
95
+ /**
96
+ * Which column the drop would land in. Not `useDropContainer`'s `isOver`: the nearest droppable
97
+ * to the pointer is almost always a *card*, so a column with anything in it would never light
98
+ * up. The card's own column is the answer.
99
+ */
100
+ const targetGroup = overId ? (placement.get(overId)?.group ?? overId) : null;
101
+
102
+ /** The row under the pointer, so the ghost can be the card itself rather than an outline. */
103
+ const dragging = draggingId
104
+ ? groups
105
+ .flatMap((group) =>
106
+ group.rows.map((row, index) => ({ row, group, index, id: getRowId(row, index) })),
107
+ )
108
+ .find((r) => r.id === draggingId)
109
+ : undefined;
110
+
111
+ return (
112
+ <DndContext {...contextProps}>
113
+ <div
114
+ className={cn(
115
+ "bg-background-presentation-body-primary h-full overflow-x-auto p-2",
116
+ className,
117
+ )}
118
+ >
119
+ <div className="flex h-full gap-4" style={{ minWidth: "max-content" }}>
120
+ {groups.map((group, i) => (
121
+ <React.Fragment key={group.id}>
122
+ <BoardColumn
123
+ group={group}
124
+ isTarget={targetGroup === group.id}
125
+ onColumnAction={onColumnAction}
126
+ ids={group.rows.map((row, index) => getRowId(row, index))}
127
+ >
128
+ {loading && <SkeletonCards />}
129
+ {!loading && group.rows.map((row, index) => {
130
+ // `index` is the position within this column. It only reaches `getRowId` as the
131
+ // last-resort fallback for rows with no id of their own — give those a stable
132
+ // `getRowId` if you drag them, or two columns will hand out the same key.
133
+ const id = getRowId(row, index);
134
+ return (
135
+ <BoardCard
136
+ key={id}
137
+ id={id}
138
+ draggable={Boolean(onRowMove)}
139
+ isActive={activeId === id}
140
+ onClick={() => setActiveId(activeId === id ? null : id)}
141
+ >
142
+ {(isDragging) =>
143
+ renderCard ? (
144
+ renderCard({
145
+ row,
146
+ id,
147
+ index,
148
+ fields: visibleFields,
149
+ group,
150
+ isActive: activeId === id,
151
+ isDragging,
152
+ })
153
+ ) : (
154
+ <DataViewCard
155
+ title={titleField ? <Cell field={titleField} row={row} /> : undefined}
156
+ rows={buildCardRows(bodyFields, row)}
157
+ className={cn(
158
+ onRowMove
159
+ ? "cursor-grab transition-shadow hover:shadow-md active:cursor-grabbing"
160
+ : "cursor-pointer",
161
+ activeId === id && "border-border-presentation-state-focus",
162
+ )}
163
+ />
164
+ )
165
+ }
166
+ </BoardCard>
167
+ );
168
+ })}
169
+ </BoardColumn>
170
+
171
+ {i < groups.length - 1 && (
172
+ // Starts below the column header rather than at the top of the board.
173
+ <div
174
+ aria-hidden
175
+ className="border-border-presentation-global-primary mt-[42px] self-stretch border-l-[2px] border-dashed"
176
+ />
177
+ )}
178
+ </React.Fragment>
179
+ ))}
180
+ </div>
181
+ </div>
182
+
183
+ {/* The ghost is the card, drawn through whatever renders the real one — a custom
184
+ `renderCard` included, or the ghost would announce itself as a different object. */}
185
+ <DragGhost>
186
+ {dragging && (
187
+ <div className="w-[271px] cursor-grabbing opacity-95 shadow-lg">
188
+ {renderCard ? (
189
+ renderCard({
190
+ row: dragging.row,
191
+ id: dragging.id,
192
+ index: dragging.index,
193
+ fields: visibleFields,
194
+ group: dragging.group,
195
+ isActive: activeId === dragging.id,
196
+ isDragging: true,
197
+ })
198
+ ) : (
199
+ <DataViewCard
200
+ title={titleField ? <Cell field={titleField} row={dragging.row} /> : undefined}
201
+ rows={buildCardRows(bodyFields, dragging.row)}
202
+ className="border-border-presentation-state-focus"
203
+ />
204
+ )}
205
+ </div>
206
+ )}
207
+ </DragGhost>
208
+ </DndContext>
209
+ );
210
+ }
211
+
212
+ /**
213
+ * Asks for the next page when *this* column is scrolled to its end.
214
+ *
215
+ * Every column calls the same `onLoadMore`, because the board is fed one shared page of rows and
216
+ * regroups them — so a short column can end up asking for more on behalf of a long one. That is
217
+ * the trade for not having to page each column separately.
218
+ */
219
+ function ColumnSentinel() {
220
+ const { loading, loadingMore, hasMore, onLoadMore } = useDataViewsData();
221
+ const { sentinelRef } = useInfiniteScroll({
222
+ onLoadMore,
223
+ hasMore,
224
+ loading: loading || loadingMore,
225
+ });
226
+ if (!hasMore || loading) return null;
227
+ return (
228
+ <>
229
+ <div ref={sentinelRef as React.Ref<HTMLDivElement>} aria-hidden className="h-px shrink-0" />
230
+ {loadingMore && <SkeletonCards count={1} />}
231
+ </>
232
+ );
233
+ }
234
+
235
+ /** Per column. Two is enough to read as "cards are coming" without pretending to know how many. */
236
+ const SKELETON_CARDS = 2;
237
+
238
+ /**
239
+ * The loading column.
240
+ *
241
+ * A real `DataViewCard` with shimmer where its text goes, so the card's radius, border and padding
242
+ * are the ones the data will land in. The column header above keeps its label and colour — only
243
+ * the cards are unknown.
244
+ */
245
+ function SkeletonCards({ count = SKELETON_CARDS }: { count?: number }) {
246
+ return (
247
+ <>
248
+ {skeletonKeys(count).map((i) => (
249
+ <DataViewCard
250
+ key={`skeleton-${i}`}
251
+ title={<SkeletonBar className="h-[18px] w-[60%]" />}
252
+ rows={[
253
+ [
254
+ { key: "a", label: <SkeletonBar className="w-[54px]" />, value: <SkeletonBar className="w-[64px]" /> },
255
+ { key: "b", label: <SkeletonBar className="w-[44px]" />, value: <SkeletonBar className="w-[72px]" /> },
256
+ ],
257
+ ]}
258
+ />
259
+ ))}
260
+ </>
261
+ );
262
+ }
263
+
264
+ /**
265
+ * A column: a drop target in its own right, so a card can be dropped on an empty one.
266
+ *
267
+ * `DragList` is what lets a card be dropped *between* two others rather than only onto the column.
268
+ */
269
+ function BoardColumn({
270
+ group,
271
+ ids,
272
+ isTarget,
273
+ onColumnAction,
274
+ children,
275
+ }: {
276
+ group: RowGroup;
277
+ ids: string[];
278
+ isTarget: boolean;
279
+ onColumnAction?: (groupId: string) => void;
280
+ children: React.ReactNode;
281
+ }) {
282
+ const { ref, isOver, containerProps } = useDropContainer(group.id);
283
+
284
+ return (
285
+ <div
286
+ ref={ref}
287
+ {...containerProps}
288
+ className={cn(
289
+ // The transparent 2px border is permanent so the drop-target state costs no layout shift.
290
+ "flex w-[279px] flex-col gap-2 rounded-[12px] border-2 border-transparent p-1",
291
+ "transition-colors duration-150 ease-in-out",
292
+ (isOver || isTarget) &&
293
+ "bg-background-presentation-cardbutton-blue-hover border-border-presentation-state-focus border-dashed",
294
+ )}
295
+ >
296
+ <ColumnHeader group={group} onAction={onColumnAction} />
297
+ <div className="flex flex-col gap-2 overflow-y-auto py-1">
298
+ <DragList ids={ids}>{children}</DragList>
299
+ <ColumnSentinel />
300
+ </div>
301
+ </div>
302
+ );
303
+ }
304
+
305
+ /**
306
+ * The wrapper around a card — ours whichever card is shown, because dragging and the click that
307
+ * opens a row are board behaviour, not card decoration.
308
+ *
309
+ * `children` is a function so a custom card can be told whether it is the one being dragged
310
+ * without this component knowing what it renders.
311
+ */
312
+ function BoardCard({
313
+ id,
314
+ draggable,
315
+ isActive,
316
+ onClick,
317
+ children,
318
+ }: {
319
+ id: string;
320
+ draggable: boolean;
321
+ isActive: boolean;
322
+ onClick: () => void;
323
+ children: (isDragging: boolean) => React.ReactNode;
324
+ }) {
325
+ const { ref, handleProps, style, isDragging } = useDragItem(id, !draggable);
326
+
327
+ return (
328
+ <div
329
+ ref={ref}
330
+ style={style}
331
+ {...handleProps}
332
+ onClick={onClick}
333
+ data-active={isActive || undefined}
334
+ className={cn(
335
+ draggable ? "cursor-grab active:cursor-grabbing" : "cursor-pointer",
336
+ isDragging && "opacity-40",
337
+ )}
338
+ >
339
+ {children(isDragging)}
340
+ </div>
341
+ );
342
+ }
343
+
344
+ /** The solid dark pill at the top of a column. No dot, no count — just the title and an action. */
345
+ function ColumnHeader({
346
+ group,
347
+ onAction,
348
+ }: {
349
+ group: RowGroup;
350
+ onAction?: (groupId: string) => void;
351
+ }) {
352
+ return (
353
+ <div
354
+ className={cn(
355
+ "flex items-center justify-between rounded-[8px] px-[6px] py-[4px]",
356
+ COLUMN_BG[group.color ?? "gray"],
357
+ )}
358
+ >
359
+ <h3 className="typography-headers-small-medium text-content-presentation-global-primary-light">
360
+ {group.label}
361
+ </h3>
362
+ {onAction && (
363
+ <Button
364
+ variant="BorderStyle"
365
+ buttonType="icon"
366
+ className="text-content-presentation-global-primary-light h-5 w-5 border-0 bg-transparent hover:bg-white/10"
367
+ onClick={() => onAction(group.id)}
368
+ >
369
+ <MoreHorizontal className="h-3.5 w-3.5" />
370
+ </Button>
371
+ )}
372
+ </div>
373
+ );
374
+ }
375
+
376
+ export const BoardView = markView(BoardViewImpl, {
377
+ defaultId: "board",
378
+ defaultLabel: "Board",
379
+ });
@@ -0,0 +1,36 @@
1
+ "use client";
2
+
3
+ import { getByPath } from "../../../utils/dataViews/path";
4
+ import type { FieldConfig, Row } from "../../../utils/dataViews/types";
5
+ import type { DataViewCardRow } from "../../../layouts/DataViewCard";
6
+ import { Cell } from "../cell";
7
+
8
+ /**
9
+ * Turn a row's fields into the `DataViewCard` body.
10
+ *
11
+ * Shared by the board's cards and the tree pane's, so the same row looks the same in both rather
12
+ * than drifting into two nearly-identical builders.
13
+ *
14
+ * Pair the body fields two per row so the card grid keeps its rhythm even when one side is
15
+ * missing: a lone survivor spans both columns, and fully empty pairs are dropped rather than
16
+ * rendering a phantom row of hairlines.
17
+ */
18
+ export function buildCardRows(fields: readonly FieldConfig[], row: Row): DataViewCardRow[] {
19
+ const rows: DataViewCardRow[] = [];
20
+ for (let i = 0; i < fields.length; i += 2) {
21
+ const pair = [fields[i], fields[i + 1]];
22
+ const cells: DataViewCardRow = [];
23
+ for (const [offset, field] of pair.entries()) {
24
+ if (!field) continue;
25
+ if (getByPath(row, field.path) == null) continue;
26
+ cells.push({
27
+ // Position, not path — two fields may share one.
28
+ key: `${field.path}-${i + offset}`,
29
+ label: field.label ?? field.path,
30
+ value: <Cell field={field} row={row} />,
31
+ });
32
+ }
33
+ if (cells.length > 0) rows.push(cells);
34
+ }
35
+ return rows;
36
+ }
@@ -0,0 +1,257 @@
1
+ "use client";
2
+
3
+ import React from "react";
4
+ import { cn } from "../../../utils/cn";
5
+ import type { FieldConfig } from "../../../utils/dataViews/types";
6
+ import { Divider } from "../../Divider";
7
+ import { Cell } from "../cell";
8
+ import { SkeletonBar, skeletonKeys } from "../states";
9
+ import { useInfiniteScroll } from "../../../hooks/useInfiniteScroll";
10
+ import { useDataViewsData, useDataViewsView } from "../context";
11
+ import { useActiveRow } from "../hooks/useActiveRow";
12
+ import { markView } from "../slots";
13
+ import type { InboxViewProps } from "../types";
14
+
15
+ /**
16
+ * `DataViews.Inbox` — a list of record cards on the left, the one you picked on the right.
17
+ *
18
+ * The rows are not mail-style title/preview lines: each card is a stack of `label : value` rows
19
+ * with a date pill in the corner, so an inbox of orders reads as orders rather than as email.
20
+ *
21
+ * The right pane is `children`: the inbox knows which row is active (`activeId`) but not how you
22
+ * want it displayed, so it hands the decision back rather than inventing a detail layout.
23
+ */
24
+ function InboxViewImpl({
25
+ children,
26
+ renderItem,
27
+ titlePath,
28
+ datePath,
29
+ itemHref,
30
+ linkComponent,
31
+ placeholder,
32
+ className,
33
+ }: InboxViewProps) {
34
+ const { rows, visibleFields, getRowId, loading, loadingMore, hasMore, onLoadMore } =
35
+ useDataViewsData();
36
+ const { sentinelRef } = useInfiniteScroll({
37
+ onLoadMore,
38
+ hasMore,
39
+ loading: loading || loadingMore,
40
+ });
41
+ const { activeId, setActiveId } = useDataViewsView();
42
+ // "Is a row open?", not "is an id set?". `activeId` is shared with the other views, so it can
43
+ // outlive the row it named — after a refetch, or after the tree selected a grouping node that
44
+ // is not a row at all. Keying off the row keeps the pane and the placeholder consistent.
45
+ const activeRow = useActiveRow();
46
+
47
+ const byPath = (path: string | undefined) =>
48
+ path ? visibleFields.find((f) => f.path === path) : undefined;
49
+
50
+ // The date is pulled out of the card body and shown as a pill; everything else becomes a
51
+ // label/value row.
52
+ const dateField = byPath(datePath) ?? visibleFields.find((f) => f.type === "date" || f.type === "date-format");
53
+ const titleField = byPath(titlePath);
54
+ const rowFields = visibleFields.filter(
55
+ (f) => f.path !== dateField?.path && f.type !== "hidden",
56
+ );
57
+
58
+ const Link = linkComponent ?? "a";
59
+
60
+ return (
61
+ // Split, like the tree: the gap shows the shell's black and the two panels sit on it.
62
+ <div className={cn("flex h-full flex-col gap-2 bg-black md:flex-row", className)}>
63
+ <ul
64
+ className={cn(
65
+ "border-border-presentation-global-primary flex w-full flex-col overflow-hidden rounded-[16px] border",
66
+ "bg-background-presentation-form-base md:w-96",
67
+ )}
68
+ >
69
+ <li className="border-border-presentation-global-primary border-b px-3 py-2">
70
+ <span
71
+ style={{ fontFeatureSettings: "'cv05' on" }}
72
+ className="typography-display-medium-medium text-content-presentation-global-primary uppercase"
73
+ >
74
+ {titleField?.label ?? "inbox"}
75
+ </span>
76
+ </li>
77
+
78
+ <div className="bg-background-presentation-button-disabled flex flex-1 flex-col gap-1 overflow-y-auto py-1">
79
+ {loading && <SkeletonItems count={rowFields.length || 2} />}
80
+ {!loading && rows.map((row, index) => {
81
+ const id = getRowId(row, index);
82
+ const selected = activeId === id;
83
+ const href = itemHref?.(row, id);
84
+
85
+ const card = renderItem ? (
86
+ renderItem({ row, id, index, fields: visibleFields, isActive: selected })
87
+ ) : (
88
+ <>
89
+ <div className="flex w-full flex-col gap-1">
90
+ {rowFields.map((field, i) => (
91
+ <div key={`${field.path}-${i}`} className="flex flex-col">
92
+ <div className="flex items-center gap-2">
93
+ <span className="typography-body-large-semibold text-content-presentation-global-secondary w-[100px] shrink-0">
94
+ {field.label ?? field.path}:
95
+ </span>
96
+ <span className="flex h-full items-center py-0.5">
97
+ <span className="bg-black-alpha-15 block h-full w-px" />
98
+ </span>
99
+ <span className="typography-body-large-medium text-content-presentation-global-primary min-w-0 flex-1 truncate">
100
+ <Cell field={field} row={row} />
101
+ </span>
102
+ </div>
103
+ {i < rowFields.length - 1 && <Divider className="mt-1" />}
104
+ </div>
105
+ ))}
106
+ </div>
107
+
108
+ {dateField && (
109
+ <div className="flex items-center justify-end">
110
+ <div className="bg-black-alpha-10 inline-flex items-center gap-0.5 rounded-md p-0.5">
111
+ <div className="rounded-sm px-1">
112
+ <span className="typography-labels-medium-semibold text-content-presentation-global-primary">
113
+ {dateField.label ?? "Created at"}:
114
+ </span>
115
+ </div>
116
+ <div className="bg-black-alpha-075 rounded-sm px-1">
117
+ <span className="typography-labels-medium-semibold text-content-presentation-global-primary">
118
+ <Cell field={dateField} row={row} />
119
+ </span>
120
+ </div>
121
+ </div>
122
+ </div>
123
+ )}
124
+ </>
125
+ );
126
+
127
+ const cardClass = cn(
128
+ "flex cursor-pointer flex-col gap-2 p-3 transition-colors",
129
+ "bg-background-presentation-form-base",
130
+ // The transparent 2px edges are permanent so hover and selection never shift the row.
131
+ "border-y-2 border-transparent",
132
+ !selected &&
133
+ "hover:bg-[image:linear-gradient(0deg,rgba(151,72,255,0.05)_0%,rgba(151,72,255,0.05)_100%)] hover:border-y-[#AE71FF]",
134
+ selected &&
135
+ "bg-[image:linear-gradient(0deg,rgba(0,117,255,0.05)_0%,rgba(0,117,255,0.05)_100%)] border-y-border-presentation-state-focus",
136
+ );
137
+
138
+ return (
139
+ <li key={id}>
140
+ {href ? (
141
+ <Link
142
+ href={href}
143
+ onClick={() => setActiveId(id)}
144
+ className={cn(cardClass, "text-inherit no-underline")}
145
+ >
146
+ {card}
147
+ </Link>
148
+ ) : (
149
+ <div
150
+ role="button"
151
+ tabIndex={0}
152
+ aria-current={selected || undefined}
153
+ onClick={() => setActiveId(id)}
154
+ onKeyDown={(e) => {
155
+ if (e.key !== "Enter" && e.key !== " ") return;
156
+ e.preventDefault();
157
+ setActiveId(id);
158
+ }}
159
+ className={cardClass}
160
+ >
161
+ {card}
162
+ </div>
163
+ )}
164
+ </li>
165
+ );
166
+ })}
167
+ {/* Asks for the next page as the list nears its end. Inside the scroller, so it is the
168
+ list's own scrolling that triggers it. */}
169
+ {hasMore && !loading && (
170
+ <li ref={sentinelRef as React.Ref<HTMLLIElement>} aria-hidden className="h-px" />
171
+ )}
172
+ {loadingMore && <SkeletonItems count={rowFields.length || 2} items={2} />}
173
+ </div>
174
+ </ul>
175
+
176
+ <div
177
+ className={cn(
178
+ "bg-background-presentation-form-base flex flex-1 flex-col overflow-hidden rounded-[16px]",
179
+ activeRow === null && "border-border-presentation-global-primary items-center justify-center border",
180
+ )}
181
+ >
182
+ {activeRow === null
183
+ ? (placeholder ?? (
184
+ <p className="text-content-presentation-global-tertiary">
185
+ Select an item to view details
186
+ </p>
187
+ ))
188
+ : children}
189
+ </div>
190
+ </div>
191
+ );
192
+ }
193
+
194
+ /** Enough items to fill the list's fold. */
195
+ const SKELETON_ITEMS = 5;
196
+
197
+ /**
198
+ * The loading list.
199
+ *
200
+ * Mirrors a real item: the same `p-3` card with `border-y-2` transparent edges, and the same
201
+ * label / divider / value row at the 100px label column — so items do not reflow when the messages
202
+ * arrive. `count` is the number of field rows a real item will have.
203
+ */
204
+ function SkeletonItems({ count, items = SKELETON_ITEMS }: { count: number; items?: number }) {
205
+ return (
206
+ <>
207
+ {skeletonKeys(items).map((item) => (
208
+ <li key={`skeleton-${item}`}>
209
+ <div className="bg-background-presentation-form-base flex flex-col gap-2 border-y-2 border-transparent p-3">
210
+ <div className="flex w-full flex-col gap-1">
211
+ {skeletonKeys(count).map((i) => (
212
+ <div key={i} className="flex items-center gap-2">
213
+ <SkeletonBar className="w-[100px] shrink-0" />
214
+ <span className="flex h-full items-center py-0.5">
215
+ <span className="bg-black-alpha-15 block h-full w-px" />
216
+ </span>
217
+ <SkeletonBar className={["w-[60%]", "w-[40%]", "w-[75%]"][(item + i) % 3]} />
218
+ </div>
219
+ ))}
220
+ </div>
221
+ </div>
222
+ </li>
223
+ ))}
224
+ </>
225
+ );
226
+ }
227
+
228
+ /**
229
+ * `DataViews.Detail` — the default detail pane: every visible field as a label/value row. Drop it
230
+ * in when you want something reasonable immediately, and replace it with your own JSX when you
231
+ * do not.
232
+ */
233
+ export function Detail({ className }: { className?: string }) {
234
+ const { visibleFields } = useDataViewsData();
235
+ const row = useActiveRow();
236
+ if (!row) return null;
237
+
238
+ return (
239
+ <dl className={cn("flex flex-col gap-3 p-6", className)}>
240
+ {visibleFields.map((f: FieldConfig, i) => (
241
+ <div key={`${f.path}-${i}`} className="flex flex-col gap-[2px]">
242
+ <dt className="typography-body-small-regular text-content-presentation-global-tertiary">
243
+ {f.label ?? f.path}
244
+ </dt>
245
+ <dd className="typography-body-medium-medium text-content-presentation-global-primary">
246
+ <Cell field={f} row={row} />
247
+ </dd>
248
+ </div>
249
+ ))}
250
+ </dl>
251
+ );
252
+ }
253
+
254
+ export const InboxView = markView(InboxViewImpl, {
255
+ defaultId: "inbox",
256
+ defaultLabel: "Inbox",
257
+ });