@oaiz/app-kit 0.0.0-stage → 0.1.4128
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.
- package/LICENSE +21 -0
- package/README.md +143 -2
- package/THIRD_PARTY_NOTICES.md +76 -0
- package/package.json +139 -4
- package/registry.json +1847 -0
- package/src/components/activity-timeline.tsx +227 -0
- package/src/components/app-shell.tsx +622 -0
- package/src/components/bar-list.tsx +47 -0
- package/src/components/board.tsx +700 -0
- package/src/components/calendar-view.tsx +1388 -0
- package/src/components/card-grid.tsx +462 -0
- package/src/components/charts.tsx +701 -0
- package/src/components/chat-markdown.tsx +230 -0
- package/src/components/chat-panel.tsx +760 -0
- package/src/components/code-editor.tsx +276 -0
- package/src/components/collection-controls.tsx +226 -0
- package/src/components/command-palette.tsx +403 -0
- package/src/components/confirm-action.tsx +123 -0
- package/src/components/data-table.tsx +821 -0
- package/src/components/date-input.tsx +429 -0
- package/src/components/empty-state.tsx +31 -0
- package/src/components/error-state.tsx +37 -0
- package/src/components/file-upload.tsx +595 -0
- package/src/components/filter-bar.tsx +415 -0
- package/src/components/grouped-picker.tsx +250 -0
- package/src/components/line-items.tsx +601 -0
- package/src/components/list-view.tsx +491 -0
- package/src/components/metrics.tsx +53 -0
- package/src/components/page-header.tsx +33 -0
- package/src/components/property-picker.tsx +546 -0
- package/src/components/record-detail.tsx +416 -0
- package/src/components/record-form.tsx +295 -0
- package/src/components/record-name.tsx +85 -0
- package/src/components/save-bar.tsx +142 -0
- package/src/components/sign-in.tsx +179 -0
- package/src/components/split-view.tsx +128 -0
- package/src/components/status-dot.tsx +56 -0
- package/src/components/stepper.tsx +153 -0
- package/src/components/text-diff-model.ts +111 -0
- package/src/components/text-diff.tsx +98 -0
- package/src/components/time-zone-picker.tsx +69 -0
- package/src/components/ui/accordion.tsx +138 -0
- package/src/components/ui/alert-dialog.tsx +67 -0
- package/src/components/ui/alert.tsx +65 -0
- package/src/components/ui/avatar-group.tsx +119 -0
- package/src/components/ui/avatar.tsx +55 -0
- package/src/components/ui/badge.tsx +59 -0
- package/src/components/ui/bottom-navigation.tsx +102 -0
- package/src/components/ui/breadcrumb.tsx +117 -0
- package/src/components/ui/button.tsx +71 -0
- package/src/components/ui/card.tsx +95 -0
- package/src/components/ui/checkbox.tsx +31 -0
- package/src/components/ui/collapsible.tsx +27 -0
- package/src/components/ui/context-menu.tsx +270 -0
- package/src/components/ui/count-up.tsx +136 -0
- package/src/components/ui/detail-disclosure-section.tsx +109 -0
- package/src/components/ui/dialog.tsx +157 -0
- package/src/components/ui/dropdown-menu.tsx +273 -0
- package/src/components/ui/form-field.tsx +155 -0
- package/src/components/ui/heading.tsx +126 -0
- package/src/components/ui/hover-card.tsx +45 -0
- package/src/components/ui/input.tsx +50 -0
- package/src/components/ui/label.tsx +30 -0
- package/src/components/ui/orb-spinner.tsx +238 -0
- package/src/components/ui/popover.tsx +59 -0
- package/src/components/ui/progress-ring.tsx +125 -0
- package/src/components/ui/progress.tsx +38 -0
- package/src/components/ui/radio-group.tsx +124 -0
- package/src/components/ui/resizable.tsx +48 -0
- package/src/components/ui/scroll-fade.tsx +98 -0
- package/src/components/ui/segmented-control.tsx +70 -0
- package/src/components/ui/select.tsx +230 -0
- package/src/components/ui/separator.tsx +29 -0
- package/src/components/ui/sheet.tsx +170 -0
- package/src/components/ui/shimmer-text.tsx +87 -0
- package/src/components/ui/shortcut.tsx +39 -0
- package/src/components/ui/skeleton.tsx +15 -0
- package/src/components/ui/slider.tsx +48 -0
- package/src/components/ui/switch.tsx +44 -0
- package/src/components/ui/table.tsx +119 -0
- package/src/components/ui/tabs.tsx +80 -0
- package/src/components/ui/text.tsx +93 -0
- package/src/components/ui/textarea.tsx +51 -0
- package/src/components/ui/toast.tsx +141 -0
- package/src/components/ui/toaster.tsx +40 -0
- package/src/components/ui/tooltip.tsx +38 -0
- package/src/components/ui/top-banner.tsx +108 -0
- package/src/components/ui/tree-view.tsx +215 -0
- package/src/components/ui/use-toast.tsx +184 -0
- package/src/components/ui/voice-orb.tsx +252 -0
- package/src/hooks/oaiz-chat.tsx +530 -0
- package/src/hooks/oaiz-data.tsx +617 -0
- package/src/hooks/oaiz-session.tsx +63 -0
- package/src/hooks/use-arrivals.ts +19 -0
- package/src/hooks/use-breakpoint.ts +100 -0
- package/src/hooks/use-media-query.ts +36 -0
- package/src/hooks/use-theme.ts +67 -0
- package/src/hooks/use-url-state.ts +77 -0
- package/src/lib/breakpoints.ts +81 -0
- package/src/lib/csp-nonce.ts +15 -0
- package/src/lib/format.ts +291 -0
- package/src/lib/speech-level.ts +118 -0
- package/src/lib/utils.ts +109 -0
- package/src/styles/oaiz/app.css +21 -0
- package/src/styles/oaiz/scrollbars.css +20 -0
- package/src/styles/oaiz/tokens.css +952 -0
- package/src/styles/oaiz/voice-orb.css +39 -0
|
@@ -0,0 +1,700 @@
|
|
|
1
|
+
import { Button } from './ui/button';
|
|
2
|
+
import {
|
|
3
|
+
useId,
|
|
4
|
+
useLayoutEffect,
|
|
5
|
+
useRef,
|
|
6
|
+
useState,
|
|
7
|
+
useSyncExternalStore,
|
|
8
|
+
type KeyboardEvent,
|
|
9
|
+
type ReactNode,
|
|
10
|
+
} from 'react';
|
|
11
|
+
import {
|
|
12
|
+
DndContext,
|
|
13
|
+
DragOverlay,
|
|
14
|
+
MouseSensor,
|
|
15
|
+
TouchSensor,
|
|
16
|
+
useDraggable,
|
|
17
|
+
useDroppable,
|
|
18
|
+
useSensor,
|
|
19
|
+
useSensors,
|
|
20
|
+
type DragEndEvent,
|
|
21
|
+
type DragStartEvent,
|
|
22
|
+
} from '@dnd-kit/core';
|
|
23
|
+
|
|
24
|
+
import { MEDIA_QUERIES_BELOW } from '../lib/breakpoints';
|
|
25
|
+
import { cn } from '../lib/utils';
|
|
26
|
+
import { ErrorState } from './error-state';
|
|
27
|
+
import {
|
|
28
|
+
DropdownMenu,
|
|
29
|
+
DropdownMenuTrigger,
|
|
30
|
+
DropdownMenuContent,
|
|
31
|
+
DropdownMenuItem,
|
|
32
|
+
DropdownMenuSeparator,
|
|
33
|
+
} from './ui/dropdown-menu';
|
|
34
|
+
import { STATUS_DOT_TONES, type StatusDotTone } from './status-dot';
|
|
35
|
+
import { ScrollFade } from './ui/scroll-fade';
|
|
36
|
+
import { Skeleton } from './ui/skeleton';
|
|
37
|
+
import { Text } from './ui/text';
|
|
38
|
+
|
|
39
|
+
export type BoardKey = string | number;
|
|
40
|
+
|
|
41
|
+
export type BoardColumn = {
|
|
42
|
+
id: string;
|
|
43
|
+
title: string;
|
|
44
|
+
/** The dot before the title: the tone the same value has in its `StatusDot`. */
|
|
45
|
+
tone?: StatusDotTone;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/** Where a card goes: its column, the card it now sits before, and a position between its neighbours. */
|
|
49
|
+
export type BoardMove = {
|
|
50
|
+
key: BoardKey;
|
|
51
|
+
toColumn: string;
|
|
52
|
+
/** The card it sits before; `null` at the end of the column. */
|
|
53
|
+
beforeKey: BoardKey | null;
|
|
54
|
+
/** Write this to the item's position column to keep the order. */
|
|
55
|
+
position: number;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export type BoardProps<Item> = {
|
|
59
|
+
columns: BoardColumn[];
|
|
60
|
+
items: Item[] | undefined;
|
|
61
|
+
itemKey: (item: Item) => BoardKey;
|
|
62
|
+
columnOf: (item: Item) => string;
|
|
63
|
+
/** The order of cards in a column, smallest first; the order of `items` when omitted. */
|
|
64
|
+
positionOf?: (item: Item) => number;
|
|
65
|
+
/** A short name for announcements, such as the card title. */
|
|
66
|
+
itemLabel?: (item: Item) => string;
|
|
67
|
+
renderCard: (item: Item) => ReactNode;
|
|
68
|
+
onMove?: (move: BoardMove) => void;
|
|
69
|
+
onCardClick?: (item: Item) => void;
|
|
70
|
+
/** Below a column's cards, such as an add button. */
|
|
71
|
+
columnFooter?: (column: BoardColumn) => ReactNode;
|
|
72
|
+
/** At the end of a column's header, such as a + button that adds a card to it. */
|
|
73
|
+
columnAction?: (column: BoardColumn) => ReactNode;
|
|
74
|
+
/** A total after the count, from the column's cards, such as the sum of order values. */
|
|
75
|
+
columnSummary?: (column: BoardColumn, items: Item[]) => ReactNode;
|
|
76
|
+
emptyColumn?: ReactNode;
|
|
77
|
+
loading?: boolean;
|
|
78
|
+
/** Shown instead of the board. */
|
|
79
|
+
error?: { message: string } | null;
|
|
80
|
+
onRetry?: () => void;
|
|
81
|
+
labels?: {
|
|
82
|
+
columns?: string;
|
|
83
|
+
instructions?: string;
|
|
84
|
+
move?: (name: string) => string;
|
|
85
|
+
moveTo?: (column: string) => string;
|
|
86
|
+
up?: string;
|
|
87
|
+
down?: string;
|
|
88
|
+
moved?: (name: string, column: string, position: number) => string;
|
|
89
|
+
retry?: string;
|
|
90
|
+
retrying?: string;
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const GAP = 1024;
|
|
95
|
+
|
|
96
|
+
/** A position between two neighbours; either may be missing at the start or end of a column. */
|
|
97
|
+
export function positionBetween(before?: number, after?: number): number {
|
|
98
|
+
if (before === undefined && after === undefined) return GAP;
|
|
99
|
+
if (before === undefined) return after! - GAP;
|
|
100
|
+
if (after === undefined) return before + GAP;
|
|
101
|
+
return (before + after) / 2;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const COLUMN_ID = 'column:';
|
|
105
|
+
const PICKER_ID = 'picker:';
|
|
106
|
+
const CARD_ID = 'card:';
|
|
107
|
+
|
|
108
|
+
function Card<Item>({
|
|
109
|
+
item,
|
|
110
|
+
id,
|
|
111
|
+
instructions,
|
|
112
|
+
onOpen,
|
|
113
|
+
onKeyMove,
|
|
114
|
+
settling,
|
|
115
|
+
onSettled,
|
|
116
|
+
children,
|
|
117
|
+
label,
|
|
118
|
+
actions,
|
|
119
|
+
cardRef,
|
|
120
|
+
}: {
|
|
121
|
+
item: Item;
|
|
122
|
+
id: BoardKey;
|
|
123
|
+
instructions: string;
|
|
124
|
+
label: string;
|
|
125
|
+
actions?: ReactNode;
|
|
126
|
+
cardRef: (node: HTMLDivElement | null) => void;
|
|
127
|
+
onOpen?: (item: Item) => void;
|
|
128
|
+
onKeyMove: (event: KeyboardEvent<HTMLDivElement>) => void;
|
|
129
|
+
/** The card just landed here, so it settles into place. */
|
|
130
|
+
settling: boolean;
|
|
131
|
+
onSettled: () => void;
|
|
132
|
+
children: ReactNode;
|
|
133
|
+
}) {
|
|
134
|
+
const drag = useDraggable({ id: `${CARD_ID}${id}` });
|
|
135
|
+
const drop = useDroppable({ id: `${CARD_ID}${id}` });
|
|
136
|
+
return (
|
|
137
|
+
<div
|
|
138
|
+
ref={(node) => {
|
|
139
|
+
drag.setNodeRef(node);
|
|
140
|
+
drop.setNodeRef(node);
|
|
141
|
+
cardRef(node);
|
|
142
|
+
}}
|
|
143
|
+
{...drag.listeners}
|
|
144
|
+
role="group"
|
|
145
|
+
aria-label={label}
|
|
146
|
+
tabIndex={0}
|
|
147
|
+
aria-describedby={instructions}
|
|
148
|
+
data-testid="board-card"
|
|
149
|
+
className={cn(
|
|
150
|
+
'touch-manipulation rounded-md border bg-card p-inset text-left text-sm text-card-foreground outline-none transition-shadow duration-fast ease-move hover:shadow-sm focus-visible:ring-2 focus-visible:ring-ring',
|
|
151
|
+
onOpen && 'cursor-pointer',
|
|
152
|
+
drag.isDragging && 'opacity-40',
|
|
153
|
+
drop.isOver && !drag.isDragging && 'ring-2 ring-primary/40',
|
|
154
|
+
settling &&
|
|
155
|
+
'animate-in fade-in-0 zoom-in-95 duration-normal ease-spring',
|
|
156
|
+
)}
|
|
157
|
+
onAnimationEnd={settling ? onSettled : undefined}
|
|
158
|
+
onClick={
|
|
159
|
+
onOpen
|
|
160
|
+
? (event) => {
|
|
161
|
+
if (!event.currentTarget.contains(event.target as Node)) return;
|
|
162
|
+
if (
|
|
163
|
+
(event.target as Element).closest(
|
|
164
|
+
'button, a, input, select, textarea, summary, [role="button"], [role="checkbox"], [contenteditable="true"]',
|
|
165
|
+
)
|
|
166
|
+
)
|
|
167
|
+
return;
|
|
168
|
+
onOpen(item);
|
|
169
|
+
}
|
|
170
|
+
: undefined
|
|
171
|
+
}
|
|
172
|
+
onKeyDown={(event) => {
|
|
173
|
+
if (!event.currentTarget.contains(event.target as Node)) return;
|
|
174
|
+
if (
|
|
175
|
+
event.target !== event.currentTarget &&
|
|
176
|
+
!(event.target as Element).closest('[data-card-open]')
|
|
177
|
+
)
|
|
178
|
+
return;
|
|
179
|
+
if (
|
|
180
|
+
event.key === 'Enter' &&
|
|
181
|
+
onOpen &&
|
|
182
|
+
event.target === event.currentTarget
|
|
183
|
+
) {
|
|
184
|
+
event.preventDefault();
|
|
185
|
+
onOpen(item);
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
onKeyMove(event);
|
|
189
|
+
}}
|
|
190
|
+
>
|
|
191
|
+
{children}
|
|
192
|
+
<div
|
|
193
|
+
className="mt-inline flex items-center justify-end gap-inline"
|
|
194
|
+
onMouseDown={(event) => event.stopPropagation()}
|
|
195
|
+
onTouchStart={(event) => event.stopPropagation()}
|
|
196
|
+
>
|
|
197
|
+
{onOpen && (
|
|
198
|
+
<Button variant="plain" size={null}
|
|
199
|
+
type="button"
|
|
200
|
+
data-card-open=""
|
|
201
|
+
aria-label={label}
|
|
202
|
+
onClick={() => onOpen(item)}
|
|
203
|
+
className="min-h-6 min-w-6 rounded-sm underline focus-visible:ring-2 focus-visible:ring-ring"
|
|
204
|
+
>
|
|
205
|
+
↗
|
|
206
|
+
</Button>
|
|
207
|
+
)}
|
|
208
|
+
{actions}
|
|
209
|
+
</div>
|
|
210
|
+
</div>
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function Column({
|
|
215
|
+
column,
|
|
216
|
+
count,
|
|
217
|
+
children,
|
|
218
|
+
footer,
|
|
219
|
+
action,
|
|
220
|
+
summary,
|
|
221
|
+
}: {
|
|
222
|
+
column: BoardColumn;
|
|
223
|
+
count: number;
|
|
224
|
+
children: ReactNode;
|
|
225
|
+
footer?: ReactNode;
|
|
226
|
+
action?: ReactNode;
|
|
227
|
+
summary?: ReactNode;
|
|
228
|
+
}) {
|
|
229
|
+
const titleId = useId();
|
|
230
|
+
const { setNodeRef, isOver } = useDroppable({
|
|
231
|
+
id: `${COLUMN_ID}${column.id}`,
|
|
232
|
+
});
|
|
233
|
+
return (
|
|
234
|
+
<section
|
|
235
|
+
ref={setNodeRef}
|
|
236
|
+
aria-labelledby={titleId}
|
|
237
|
+
data-testid="board-column"
|
|
238
|
+
className={cn(
|
|
239
|
+
'flex max-w-80 flex-[1_0_12rem] flex-col gap-1.5 rounded-lg max-mobile:max-w-none p-1.5 transition-colors duration-fast',
|
|
240
|
+
isOver && 'bg-accent',
|
|
241
|
+
)}
|
|
242
|
+
>
|
|
243
|
+
<header className="group/column relative flex h-7 items-center gap-inline px-1">
|
|
244
|
+
{column.tone && (
|
|
245
|
+
<span
|
|
246
|
+
aria-hidden="true"
|
|
247
|
+
className={cn(
|
|
248
|
+
'size-2 shrink-0 rounded-full',
|
|
249
|
+
STATUS_DOT_TONES[column.tone],
|
|
250
|
+
)}
|
|
251
|
+
/>
|
|
252
|
+
)}
|
|
253
|
+
<Text id={titleId} size="sm" weight="medium">
|
|
254
|
+
{column.title}
|
|
255
|
+
</Text>
|
|
256
|
+
<Text size="xs" variant="muted" className="tabular-nums">
|
|
257
|
+
{count}
|
|
258
|
+
</Text>
|
|
259
|
+
{summary !== undefined && (
|
|
260
|
+
<Text size="xs" variant="muted" className="truncate tabular-nums">
|
|
261
|
+
{summary}
|
|
262
|
+
</Text>
|
|
263
|
+
)}
|
|
264
|
+
{/* Shown on hover, so the header keeps its sum at rest; a touch screen has no hover. */}
|
|
265
|
+
{action && (
|
|
266
|
+
<div className="absolute right-0 flex bg-background opacity-0 focus-within:opacity-100 group-hover/column:opacity-100 max-mobile:static max-mobile:ml-auto max-mobile:bg-transparent max-mobile:opacity-100 pointer-coarse:opacity-100">
|
|
267
|
+
{action}
|
|
268
|
+
</div>
|
|
269
|
+
)}
|
|
270
|
+
</header>
|
|
271
|
+
<div className="flex min-h-16 flex-col gap-1.5">{children}</div>
|
|
272
|
+
{footer}
|
|
273
|
+
</section>
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const PHONE = MEDIA_QUERIES_BELOW.mobile;
|
|
278
|
+
|
|
279
|
+
function subscribeToPhone(onChange: () => void) {
|
|
280
|
+
const query = window.matchMedia?.(PHONE);
|
|
281
|
+
query?.addEventListener('change', onChange);
|
|
282
|
+
return () => query?.removeEventListener('change', onChange);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function usePhone() {
|
|
286
|
+
return useSyncExternalStore(
|
|
287
|
+
subscribeToPhone,
|
|
288
|
+
() => window.matchMedia?.(PHONE).matches ?? false,
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/** Shows one column on a phone. A card dropped here moves to that column. */
|
|
293
|
+
function ColumnPicker({
|
|
294
|
+
column,
|
|
295
|
+
count,
|
|
296
|
+
selected,
|
|
297
|
+
onSelect,
|
|
298
|
+
}: {
|
|
299
|
+
column: BoardColumn;
|
|
300
|
+
count: number;
|
|
301
|
+
selected: boolean;
|
|
302
|
+
onSelect: () => void;
|
|
303
|
+
}) {
|
|
304
|
+
const { setNodeRef, isOver } = useDroppable({
|
|
305
|
+
id: `${PICKER_ID}${column.id}`,
|
|
306
|
+
});
|
|
307
|
+
return (
|
|
308
|
+
<Button variant="plain" size={null}
|
|
309
|
+
ref={setNodeRef}
|
|
310
|
+
type="button"
|
|
311
|
+
aria-pressed={selected}
|
|
312
|
+
onClick={onSelect}
|
|
313
|
+
className={cn(
|
|
314
|
+
'flex h-8 items-center gap-1.5 rounded-md px-inline text-sm outline-none focus-visible:ring-2 focus-visible:ring-ring',
|
|
315
|
+
selected ? 'bg-muted font-medium' : 'text-muted-foreground',
|
|
316
|
+
isOver && 'bg-accent',
|
|
317
|
+
)}
|
|
318
|
+
>
|
|
319
|
+
{column.tone && (
|
|
320
|
+
<span
|
|
321
|
+
aria-hidden="true"
|
|
322
|
+
className={cn(
|
|
323
|
+
'size-2 shrink-0 rounded-full',
|
|
324
|
+
STATUS_DOT_TONES[column.tone],
|
|
325
|
+
)}
|
|
326
|
+
/>
|
|
327
|
+
)}
|
|
328
|
+
{column.title}
|
|
329
|
+
<Text as="span" size="xs" variant="muted" className="tabular-nums">
|
|
330
|
+
{count}
|
|
331
|
+
</Text>
|
|
332
|
+
</Button>
|
|
333
|
+
);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Cards in columns, such as orders by status; on a phone one column shows at a time, with a picker above it. Drag a card with the pointer, or focus it and press Alt
|
|
338
|
+
* with an arrow key: left and right change the column, up and down change the order. `onMove`
|
|
339
|
+
* reports the new column and position; the caller saves them, for example with `useUpdateRow`.
|
|
340
|
+
*/
|
|
341
|
+
export function Board<Item>({
|
|
342
|
+
columns,
|
|
343
|
+
items,
|
|
344
|
+
itemKey,
|
|
345
|
+
columnOf,
|
|
346
|
+
positionOf,
|
|
347
|
+
itemLabel,
|
|
348
|
+
renderCard,
|
|
349
|
+
onMove,
|
|
350
|
+
onCardClick,
|
|
351
|
+
columnFooter,
|
|
352
|
+
columnAction,
|
|
353
|
+
columnSummary,
|
|
354
|
+
emptyColumn = 'No cards',
|
|
355
|
+
loading = false,
|
|
356
|
+
error,
|
|
357
|
+
onRetry,
|
|
358
|
+
labels,
|
|
359
|
+
}: BoardProps<Item>) {
|
|
360
|
+
const instructions = useId();
|
|
361
|
+
const menuMove = useRef<BoardKey | null>(null);
|
|
362
|
+
const cardRefs = useRef(new Map<BoardKey, HTMLDivElement>());
|
|
363
|
+
const focusAfterMove = useRef<{
|
|
364
|
+
key: BoardKey;
|
|
365
|
+
column: string;
|
|
366
|
+
position: number;
|
|
367
|
+
} | null>(null);
|
|
368
|
+
const [active, setActive] = useState<BoardKey | null>(null);
|
|
369
|
+
const [announcement, setAnnouncement] = useState('');
|
|
370
|
+
const [settling, setSettling] = useState<BoardKey | null>(null);
|
|
371
|
+
const phone = usePhone();
|
|
372
|
+
const [phoneColumn, setPhoneColumn] = useState<string | null>(null);
|
|
373
|
+
useLayoutEffect(() => {
|
|
374
|
+
const request = focusAfterMove.current;
|
|
375
|
+
if (!request) return;
|
|
376
|
+
const item = items?.find((each) => itemKey(each) === request.key);
|
|
377
|
+
if (
|
|
378
|
+
!item ||
|
|
379
|
+
columnOf(item) !== request.column ||
|
|
380
|
+
(positionOf && positionOf(item) !== request.position)
|
|
381
|
+
)
|
|
382
|
+
return;
|
|
383
|
+
if (phone && phoneColumn !== request.column) {
|
|
384
|
+
setPhoneColumn(request.column);
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
const card = cardRefs.current.get(request.key);
|
|
388
|
+
if (!card) return;
|
|
389
|
+
(card.querySelector<HTMLElement>('[data-card-open]') ?? card).focus();
|
|
390
|
+
focusAfterMove.current = null;
|
|
391
|
+
});
|
|
392
|
+
const sensors = useSensors(
|
|
393
|
+
// A short drag distance keeps a click a click.
|
|
394
|
+
useSensor(MouseSensor, { activationConstraint: { distance: 4 } }),
|
|
395
|
+
// A touch drags after a hold; a swipe scrolls the page.
|
|
396
|
+
useSensor(TouchSensor, {
|
|
397
|
+
activationConstraint: { delay: 200, tolerance: 8 },
|
|
398
|
+
}),
|
|
399
|
+
);
|
|
400
|
+
|
|
401
|
+
if (error) {
|
|
402
|
+
return (
|
|
403
|
+
<ErrorState
|
|
404
|
+
error={error}
|
|
405
|
+
onRetry={onRetry}
|
|
406
|
+
retrying={loading}
|
|
407
|
+
labels={labels}
|
|
408
|
+
/>
|
|
409
|
+
);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
const list = items ?? [];
|
|
413
|
+
const order = new Map(list.map((item, index) => [itemKey(item), index]));
|
|
414
|
+
const positionOfItem = (item: Item) =>
|
|
415
|
+
positionOf ? positionOf(item) : order.get(itemKey(item))!;
|
|
416
|
+
const grouped = new Map<string, Item[]>();
|
|
417
|
+
for (const item of list) {
|
|
418
|
+
const column = columnOf(item);
|
|
419
|
+
const cards = grouped.get(column) ?? [];
|
|
420
|
+
cards.push(item);
|
|
421
|
+
grouped.set(column, cards);
|
|
422
|
+
}
|
|
423
|
+
for (const cards of grouped.values())
|
|
424
|
+
cards.sort((a, b) => positionOfItem(a) - positionOfItem(b));
|
|
425
|
+
const cardsIn = (columnId: string) => grouped.get(columnId) ?? [];
|
|
426
|
+
const byKey = new Map(list.map((item) => [String(itemKey(item)), item]));
|
|
427
|
+
const labelOf = (item: Item) => itemLabel?.(item) ?? String(itemKey(item));
|
|
428
|
+
|
|
429
|
+
function move(item: Item, toColumn: string, beforeKey: BoardKey | null) {
|
|
430
|
+
const key = itemKey(item);
|
|
431
|
+
const others = cardsIn(toColumn).filter((card) => itemKey(card) !== key);
|
|
432
|
+
const at =
|
|
433
|
+
beforeKey === null
|
|
434
|
+
? others.length
|
|
435
|
+
: others.findIndex((card) => itemKey(card) === beforeKey);
|
|
436
|
+
const index = at === -1 ? others.length : at;
|
|
437
|
+
const from = cardsIn(toColumn).findIndex((card) => itemKey(card) === key);
|
|
438
|
+
if (from === index) return;
|
|
439
|
+
const before = others[index - 1];
|
|
440
|
+
const after = others[index];
|
|
441
|
+
const position = positionBetween(
|
|
442
|
+
before === undefined ? undefined : positionOfItem(before),
|
|
443
|
+
after === undefined ? undefined : positionOfItem(after),
|
|
444
|
+
);
|
|
445
|
+
const title =
|
|
446
|
+
columns.find((column) => column.id === toColumn)?.title ?? toColumn;
|
|
447
|
+
setAnnouncement(
|
|
448
|
+
labels?.moved?.(labelOf(item), title, index + 1) ??
|
|
449
|
+
`Moved ${labelOf(item)} to ${title}, position ${index + 1}.`,
|
|
450
|
+
);
|
|
451
|
+
focusAfterMove.current = { key, column: toColumn, position };
|
|
452
|
+
setSettling(key);
|
|
453
|
+
onMove?.({
|
|
454
|
+
key,
|
|
455
|
+
toColumn,
|
|
456
|
+
beforeKey: after ? itemKey(after) : null,
|
|
457
|
+
position,
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
function onDragStart(event: DragStartEvent) {
|
|
462
|
+
const item = byKey.get(String(event.active.id).slice(CARD_ID.length));
|
|
463
|
+
setActive(item ? itemKey(item) : null);
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
function onDragEnd(event: DragEndEvent) {
|
|
467
|
+
setActive(null);
|
|
468
|
+
const item = byKey.get(String(event.active.id).slice(CARD_ID.length));
|
|
469
|
+
const over = event.over ? String(event.over.id) : null;
|
|
470
|
+
if (!item || over === null || over === String(event.active.id)) return;
|
|
471
|
+
if (over.startsWith(PICKER_ID)) {
|
|
472
|
+
move(item, over.slice(PICKER_ID.length), null);
|
|
473
|
+
return;
|
|
474
|
+
}
|
|
475
|
+
if (over.startsWith(COLUMN_ID)) {
|
|
476
|
+
// A drop on a column's background puts the card at the end of that column.
|
|
477
|
+
move(item, over.slice(COLUMN_ID.length), null);
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
const target = byKey.get(over.slice(CARD_ID.length));
|
|
481
|
+
if (!target) return;
|
|
482
|
+
// Over the lower half of a card, the dragged card goes after it.
|
|
483
|
+
const dragged = event.active.rect.current.translated;
|
|
484
|
+
const targetRect = event.over!.rect;
|
|
485
|
+
const after =
|
|
486
|
+
dragged !== null &&
|
|
487
|
+
dragged.top + dragged.height / 2 > targetRect.top + targetRect.height / 2;
|
|
488
|
+
if (!after) {
|
|
489
|
+
move(item, columnOf(target), itemKey(target));
|
|
490
|
+
return;
|
|
491
|
+
}
|
|
492
|
+
const cards = cardsIn(columnOf(target)).filter(
|
|
493
|
+
(card) => itemKey(card) !== itemKey(item),
|
|
494
|
+
);
|
|
495
|
+
const next = cards[cards.indexOf(target) + 1];
|
|
496
|
+
move(item, columnOf(target), next === undefined ? null : itemKey(next));
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
function keyMove(item: Item, event: KeyboardEvent<HTMLDivElement>) {
|
|
500
|
+
if (!event.altKey || !onMove) return;
|
|
501
|
+
const columnIds = columns.map((column) => column.id);
|
|
502
|
+
const columnIndex = columnIds.indexOf(columnOf(item));
|
|
503
|
+
const cards = cardsIn(columnOf(item));
|
|
504
|
+
const index = cards.findIndex((card) => itemKey(card) === itemKey(item));
|
|
505
|
+
if (event.key === 'ArrowLeft' || event.key === 'ArrowRight') {
|
|
506
|
+
const next =
|
|
507
|
+
columnIds[columnIndex + (event.key === 'ArrowLeft' ? -1 : 1)];
|
|
508
|
+
if (next === undefined) return;
|
|
509
|
+
event.preventDefault();
|
|
510
|
+
move(item, next, null);
|
|
511
|
+
} else if (event.key === 'ArrowUp' && index > 0) {
|
|
512
|
+
event.preventDefault();
|
|
513
|
+
move(item, columnOf(item), itemKey(cards[index - 1]!));
|
|
514
|
+
} else if (event.key === 'ArrowDown' && index < cards.length - 1) {
|
|
515
|
+
event.preventDefault();
|
|
516
|
+
move(
|
|
517
|
+
item,
|
|
518
|
+
columnOf(item),
|
|
519
|
+
cards[index + 2] ? itemKey(cards[index + 2]!) : null,
|
|
520
|
+
);
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
const activeItem = active === null ? undefined : byKey.get(String(active));
|
|
525
|
+
|
|
526
|
+
const selectedColumn = columns.some((column) => column.id === phoneColumn)
|
|
527
|
+
? phoneColumn
|
|
528
|
+
: columns[0]?.id;
|
|
529
|
+
|
|
530
|
+
return (
|
|
531
|
+
<DndContext
|
|
532
|
+
sensors={sensors}
|
|
533
|
+
onDragStart={onDragStart}
|
|
534
|
+
onDragEnd={onDragEnd}
|
|
535
|
+
onDragCancel={() => setActive(null)}
|
|
536
|
+
>
|
|
537
|
+
{phone && (
|
|
538
|
+
<div className="flex flex-wrap gap-1 pb-inline">
|
|
539
|
+
{columns.map((column) => (
|
|
540
|
+
<ColumnPicker
|
|
541
|
+
key={column.id}
|
|
542
|
+
column={column}
|
|
543
|
+
count={cardsIn(column.id).length}
|
|
544
|
+
selected={column.id === selectedColumn}
|
|
545
|
+
onSelect={() => setPhoneColumn(column.id)}
|
|
546
|
+
/>
|
|
547
|
+
))}
|
|
548
|
+
</div>
|
|
549
|
+
)}
|
|
550
|
+
<ScrollFade
|
|
551
|
+
role="region"
|
|
552
|
+
aria-busy={loading}
|
|
553
|
+
aria-label={labels?.columns ?? 'Columns'}
|
|
554
|
+
className="flex gap-stack pb-inline"
|
|
555
|
+
>
|
|
556
|
+
{columns
|
|
557
|
+
.filter((column) => !phone || column.id === selectedColumn)
|
|
558
|
+
.map((column) => {
|
|
559
|
+
const cards = cardsIn(column.id);
|
|
560
|
+
return (
|
|
561
|
+
<Column
|
|
562
|
+
key={column.id}
|
|
563
|
+
column={column}
|
|
564
|
+
count={cards.length}
|
|
565
|
+
footer={columnFooter?.(column)}
|
|
566
|
+
action={columnAction?.(column)}
|
|
567
|
+
summary={loading ? undefined : columnSummary?.(column, cards)}
|
|
568
|
+
>
|
|
569
|
+
{loading ? (
|
|
570
|
+
<>
|
|
571
|
+
<Skeleton className="h-16 w-full" />
|
|
572
|
+
<Skeleton className="h-16 w-full" />
|
|
573
|
+
</>
|
|
574
|
+
) : cards.length === 0 ? (
|
|
575
|
+
<Text size="sm" variant="muted" className="px-1 py-inline">
|
|
576
|
+
{emptyColumn}
|
|
577
|
+
</Text>
|
|
578
|
+
) : (
|
|
579
|
+
cards.map((item) => (
|
|
580
|
+
<Card
|
|
581
|
+
key={itemKey(item)}
|
|
582
|
+
item={item}
|
|
583
|
+
id={itemKey(item)}
|
|
584
|
+
instructions={instructions}
|
|
585
|
+
label={labelOf(item)}
|
|
586
|
+
cardRef={(node) => {
|
|
587
|
+
if (node) cardRefs.current.set(itemKey(item), node);
|
|
588
|
+
else cardRefs.current.delete(itemKey(item));
|
|
589
|
+
}}
|
|
590
|
+
actions={
|
|
591
|
+
onMove && (
|
|
592
|
+
<DropdownMenu>
|
|
593
|
+
<DropdownMenuTrigger asChild>
|
|
594
|
+
<Button variant="plain" size={null}
|
|
595
|
+
type="button"
|
|
596
|
+
aria-label={
|
|
597
|
+
labels?.move?.(labelOf(item)) ??
|
|
598
|
+
`Move ${labelOf(item)}`
|
|
599
|
+
}
|
|
600
|
+
className="min-h-6 rounded-sm px-2 text-xs underline focus-visible:ring-2 focus-visible:ring-ring"
|
|
601
|
+
>
|
|
602
|
+
{labels?.move?.(labelOf(item)) ?? 'Move…'}
|
|
603
|
+
</Button>
|
|
604
|
+
</DropdownMenuTrigger>
|
|
605
|
+
<DropdownMenuContent
|
|
606
|
+
onCloseAutoFocus={(event) => {
|
|
607
|
+
if (menuMove.current !== null) {
|
|
608
|
+
event.preventDefault();
|
|
609
|
+
const card = cardRefs.current.get(
|
|
610
|
+
menuMove.current,
|
|
611
|
+
);
|
|
612
|
+
(
|
|
613
|
+
card?.querySelector<HTMLElement>(
|
|
614
|
+
'[data-card-open]',
|
|
615
|
+
) ?? card
|
|
616
|
+
)?.focus();
|
|
617
|
+
menuMove.current = null;
|
|
618
|
+
}
|
|
619
|
+
}}
|
|
620
|
+
>
|
|
621
|
+
{columns
|
|
622
|
+
.filter((each) => each.id !== column.id)
|
|
623
|
+
.map((each) => (
|
|
624
|
+
<DropdownMenuItem
|
|
625
|
+
key={each.id}
|
|
626
|
+
onSelect={() => {
|
|
627
|
+
menuMove.current = itemKey(item);
|
|
628
|
+
move(item, each.id, null);
|
|
629
|
+
}}
|
|
630
|
+
>
|
|
631
|
+
{labels?.moveTo?.(each.title) ??
|
|
632
|
+
`Move to ${each.title}`}
|
|
633
|
+
</DropdownMenuItem>
|
|
634
|
+
))}
|
|
635
|
+
<DropdownMenuSeparator />
|
|
636
|
+
<DropdownMenuItem
|
|
637
|
+
disabled={cards[0] === item}
|
|
638
|
+
onSelect={() => {
|
|
639
|
+
menuMove.current = itemKey(item);
|
|
640
|
+
move(
|
|
641
|
+
item,
|
|
642
|
+
column.id,
|
|
643
|
+
itemKey(cards[cards.indexOf(item) - 1]!),
|
|
644
|
+
);
|
|
645
|
+
}}
|
|
646
|
+
>
|
|
647
|
+
{labels?.up ?? 'Move up'}
|
|
648
|
+
</DropdownMenuItem>
|
|
649
|
+
<DropdownMenuItem
|
|
650
|
+
disabled={cards[cards.length - 1] === item}
|
|
651
|
+
onSelect={() => {
|
|
652
|
+
menuMove.current = itemKey(item);
|
|
653
|
+
move(
|
|
654
|
+
item,
|
|
655
|
+
column.id,
|
|
656
|
+
cards[cards.indexOf(item) + 2]
|
|
657
|
+
? itemKey(cards[cards.indexOf(item) + 2]!)
|
|
658
|
+
: null,
|
|
659
|
+
);
|
|
660
|
+
}}
|
|
661
|
+
>
|
|
662
|
+
{labels?.down ?? 'Move down'}
|
|
663
|
+
</DropdownMenuItem>
|
|
664
|
+
</DropdownMenuContent>
|
|
665
|
+
</DropdownMenu>
|
|
666
|
+
)
|
|
667
|
+
}
|
|
668
|
+
onOpen={onCardClick}
|
|
669
|
+
onKeyMove={(event) => keyMove(item, event)}
|
|
670
|
+
settling={settling === itemKey(item)}
|
|
671
|
+
onSettled={() => setSettling(null)}
|
|
672
|
+
>
|
|
673
|
+
{renderCard(item)}
|
|
674
|
+
</Card>
|
|
675
|
+
))
|
|
676
|
+
)}
|
|
677
|
+
</Column>
|
|
678
|
+
);
|
|
679
|
+
})}
|
|
680
|
+
</ScrollFade>
|
|
681
|
+
<p id={instructions} className="sr-only">
|
|
682
|
+
{onMove
|
|
683
|
+
? (labels?.instructions ??
|
|
684
|
+
'Use the Move menu, or press Alt with Left or Right to change column and Up or Down to change order.')
|
|
685
|
+
: ''}
|
|
686
|
+
</p>
|
|
687
|
+
<p aria-live="polite" className="sr-only">
|
|
688
|
+
{announcement}
|
|
689
|
+
</p>
|
|
690
|
+
{/* No drop animation: it flies to where the card was before the move, not where it lands. */}
|
|
691
|
+
<DragOverlay dropAnimation={null}>
|
|
692
|
+
{activeItem ? (
|
|
693
|
+
<div className="rotate-1 rounded-md border bg-card p-inset text-sm text-card-foreground shadow-lg">
|
|
694
|
+
{renderCard(activeItem)}
|
|
695
|
+
</div>
|
|
696
|
+
) : null}
|
|
697
|
+
</DragOverlay>
|
|
698
|
+
</DndContext>
|
|
699
|
+
);
|
|
700
|
+
}
|