@alepha/ui 0.25.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.
- package/README.md +12 -0
- package/components.json +20 -0
- package/package.json +85 -0
- package/src/components/action-error-toaster/action-error-toaster.tsx +70 -0
- package/src/components/admin/admin-audits.tsx +247 -0
- package/src/components/admin/admin-files.tsx +346 -0
- package/src/components/admin/admin-jobs-executions-panel.tsx +235 -0
- package/src/components/admin/admin-jobs-status-badge.tsx +33 -0
- package/src/components/admin/admin-jobs.tsx +294 -0
- package/src/components/admin/admin-keys.tsx +162 -0
- package/src/components/admin/admin-notifications.tsx +130 -0
- package/src/components/admin/admin-page.tsx +24 -0
- package/src/components/admin/admin-parameters.tsx +864 -0
- package/src/components/admin/admin-payments.tsx +93 -0
- package/src/components/admin/admin-sessions.tsx +174 -0
- package/src/components/admin/admin-user-detail-audits-tab.tsx +82 -0
- package/src/components/admin/admin-user-detail-identity-aside.tsx +218 -0
- package/src/components/admin/admin-user-detail-overview-tab.tsx +87 -0
- package/src/components/admin/admin-user-detail-password-dialog.tsx +85 -0
- package/src/components/admin/admin-user-detail-password-schema.ts +11 -0
- package/src/components/admin/admin-user-detail-profile-schema.ts +19 -0
- package/src/components/admin/admin-user-detail-provider-labels.ts +15 -0
- package/src/components/admin/admin-user-detail-security-tab.tsx +146 -0
- package/src/components/admin/admin-user-detail-sessions-tab.tsx +93 -0
- package/src/components/admin/admin-user-detail.tsx +606 -0
- package/src/components/admin/admin-users-roles-picker.tsx +126 -0
- package/src/components/admin/admin-users-status-filter.tsx +51 -0
- package/src/components/admin/admin-users.tsx +518 -0
- package/src/components/admin/diff-lines.spec.ts +58 -0
- package/src/components/admin/diff-lines.ts +58 -0
- package/src/components/admin/parameter-diff-dialog.tsx +119 -0
- package/src/components/admin/parameter-history-item.tsx +252 -0
- package/src/components/admin/parameter-json-dialog.tsx +93 -0
- package/src/components/admin/parameter-save-dialog.tsx +126 -0
- package/src/components/admin/use-confirmed-action.tsx +93 -0
- package/src/components/alepha-table/__tests__/use-table-selection.browser.spec.tsx +92 -0
- package/src/components/alepha-table/alepha-table.tsx +1158 -0
- package/src/components/alepha-table/use-table-selection.ts +95 -0
- package/src/components/app-shell/app-shell.tsx +568 -0
- package/src/components/auth/__tests__/auth-router.spec.ts +52 -0
- package/src/components/auth/auth-login.tsx +382 -0
- package/src/components/auth/auth-register.tsx +786 -0
- package/src/components/auth/auth-reset-password.tsx +349 -0
- package/src/components/auth/auth-router.tsx +126 -0
- package/src/components/auth/auth-verify-email.tsx +179 -0
- package/src/components/auto-form/auto-form.tsx +867 -0
- package/src/components/brand-icon/brand-icon.tsx +104 -0
- package/src/components/button-dark/button-dark.tsx +78 -0
- package/src/components/button-language/button-language.tsx +98 -0
- package/src/components/button-theme/button-theme.tsx +148 -0
- package/src/components/button-user/button-user.tsx +214 -0
- package/src/components/captcha/turnstile-widget.tsx +157 -0
- package/src/components/control/control.tsx +735 -0
- package/src/components/control-array/control-array.tsx +700 -0
- package/src/components/control-base/__tests__/form-field-a11y.browser.spec.tsx +93 -0
- package/src/components/control-base/field-i18n.ts +60 -0
- package/src/components/control-base/form-field.tsx +275 -0
- package/src/components/control-base/grid.tsx +59 -0
- package/src/components/control-base/icon-hint.tsx +64 -0
- package/src/components/control-date/control-date.tsx +190 -0
- package/src/components/control-number/control-number.tsx +136 -0
- package/src/components/control-object/control-object.tsx +200 -0
- package/src/components/control-select/control-select.tsx +774 -0
- package/src/components/control-upload/control-upload.tsx +475 -0
- package/src/components/file-image/file-image.tsx +52 -0
- package/src/components/language-toggle/language-toggle.tsx +66 -0
- package/src/components/markdown-view/markdown-view.css +114 -0
- package/src/components/markdown-view/markdown-view.tsx +92 -0
- package/src/components/nav-shell/nav-page.tsx +56 -0
- package/src/components/nav-shell/nav-shell.tsx +72 -0
- package/src/components/nav-shell/nav-tree-util.ts +62 -0
- package/src/components/nav-shell/spotlight.tsx +163 -0
- package/src/components/nav-shell/use-nav-breadcrumbs.ts +52 -0
- package/src/components/nav-shell/use-nav-entries.ts +101 -0
- package/src/components/nav-shell/use-nav-tree.ts +61 -0
- package/src/components/ui/accordion.tsx +77 -0
- package/src/components/ui/alert-dialog.tsx +186 -0
- package/src/components/ui/alert.tsx +75 -0
- package/src/components/ui/aspect-ratio.tsx +22 -0
- package/src/components/ui/avatar.tsx +106 -0
- package/src/components/ui/badge.tsx +51 -0
- package/src/components/ui/breadcrumb.tsx +121 -0
- package/src/components/ui/button-group.tsx +86 -0
- package/src/components/ui/button.tsx +83 -0
- package/src/components/ui/calendar.tsx +235 -0
- package/src/components/ui/card.tsx +102 -0
- package/src/components/ui/carousel.tsx +241 -0
- package/src/components/ui/chart.tsx +370 -0
- package/src/components/ui/checkbox.tsx +27 -0
- package/src/components/ui/collapsible.tsx +19 -0
- package/src/components/ui/combobox.tsx +298 -0
- package/src/components/ui/command.tsx +196 -0
- package/src/components/ui/context-menu.tsx +271 -0
- package/src/components/ui/dialog.tsx +153 -0
- package/src/components/ui/direction.tsx +4 -0
- package/src/components/ui/drawer.tsx +130 -0
- package/src/components/ui/dropdown-menu.tsx +269 -0
- package/src/components/ui/empty.tsx +100 -0
- package/src/components/ui/hover-card.tsx +50 -0
- package/src/components/ui/input-group.tsx +157 -0
- package/src/components/ui/input-otp.tsx +83 -0
- package/src/components/ui/input.tsx +19 -0
- package/src/components/ui/item.tsx +200 -0
- package/src/components/ui/kbd.tsx +26 -0
- package/src/components/ui/label.tsx +19 -0
- package/src/components/ui/menubar.tsx +283 -0
- package/src/components/ui/navigation-menu.tsx +170 -0
- package/src/components/ui/pagination.tsx +133 -0
- package/src/components/ui/popover.tsx +87 -0
- package/src/components/ui/progress.tsx +82 -0
- package/src/components/ui/radio-group.tsx +35 -0
- package/src/components/ui/resizable.tsx +49 -0
- package/src/components/ui/scroll-area.tsx +51 -0
- package/src/components/ui/segmented.tsx +232 -0
- package/src/components/ui/select.tsx +201 -0
- package/src/components/ui/separator.tsx +22 -0
- package/src/components/ui/sheet.tsx +133 -0
- package/src/components/ui/sidebar.tsx +723 -0
- package/src/components/ui/skeleton.tsx +13 -0
- package/src/components/ui/slider.tsx +51 -0
- package/src/components/ui/sonner.tsx +46 -0
- package/src/components/ui/spinner.tsx +15 -0
- package/src/components/ui/switch.tsx +29 -0
- package/src/components/ui/table.tsx +115 -0
- package/src/components/ui/tabs.tsx +79 -0
- package/src/components/ui/textarea.tsx +17 -0
- package/src/components/ui/toggle-group.tsx +86 -0
- package/src/components/ui/toggle.tsx +44 -0
- package/src/components/ui/tooltip.tsx +63 -0
- package/src/components/use-dialog/use-dialog.tsx +244 -0
- package/src/components/use-toast/use-toast.tsx +89 -0
- package/src/hooks/use-mobile.ts +21 -0
- package/src/lib/__tests__/i18n-fr.spec.ts +72 -0
- package/src/lib/i18n-fr.ts +500 -0
- package/src/lib/utils.ts +6 -0
- package/src/styles.css +129 -0
|
@@ -0,0 +1,1158 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
|
|
3
|
+
void React;
|
|
4
|
+
|
|
5
|
+
import { Button } from "@alepha/ui/components/ui/button";
|
|
6
|
+
import { Checkbox } from "@alepha/ui/components/ui/checkbox";
|
|
7
|
+
import {
|
|
8
|
+
DropdownMenu,
|
|
9
|
+
DropdownMenuCheckboxItem,
|
|
10
|
+
DropdownMenuContent,
|
|
11
|
+
DropdownMenuGroup,
|
|
12
|
+
DropdownMenuItem,
|
|
13
|
+
DropdownMenuLabel,
|
|
14
|
+
DropdownMenuSeparator,
|
|
15
|
+
DropdownMenuTrigger,
|
|
16
|
+
} from "@alepha/ui/components/ui/dropdown-menu";
|
|
17
|
+
import {
|
|
18
|
+
Pagination,
|
|
19
|
+
PaginationContent,
|
|
20
|
+
PaginationEllipsis,
|
|
21
|
+
PaginationItem,
|
|
22
|
+
PaginationLink,
|
|
23
|
+
PaginationNext,
|
|
24
|
+
PaginationPrevious,
|
|
25
|
+
} from "@alepha/ui/components/ui/pagination";
|
|
26
|
+
import { Skeleton } from "@alepha/ui/components/ui/skeleton";
|
|
27
|
+
import {
|
|
28
|
+
Table,
|
|
29
|
+
TableBody,
|
|
30
|
+
TableCell,
|
|
31
|
+
TableHead,
|
|
32
|
+
TableHeader,
|
|
33
|
+
TableRow,
|
|
34
|
+
} from "@alepha/ui/components/ui/table";
|
|
35
|
+
import {
|
|
36
|
+
Tooltip,
|
|
37
|
+
TooltipContent,
|
|
38
|
+
TooltipProvider,
|
|
39
|
+
TooltipTrigger,
|
|
40
|
+
} from "@alepha/ui/components/ui/tooltip";
|
|
41
|
+
import { cn } from "@alepha/ui/lib/utils";
|
|
42
|
+
import { type Page, type ZObject, z } from "alepha";
|
|
43
|
+
import { ClientOnly, useAlepha } from "alepha/react";
|
|
44
|
+
import { type FormModel, useForm } from "alepha/react/form";
|
|
45
|
+
import { useI18n } from "alepha/react/i18n";
|
|
46
|
+
import {
|
|
47
|
+
Columns3,
|
|
48
|
+
FunnelX,
|
|
49
|
+
Inbox,
|
|
50
|
+
MoreVertical,
|
|
51
|
+
RefreshCw,
|
|
52
|
+
X,
|
|
53
|
+
} from "lucide-react";
|
|
54
|
+
import {
|
|
55
|
+
type ComponentType,
|
|
56
|
+
type ReactNode,
|
|
57
|
+
type SVGProps,
|
|
58
|
+
useCallback,
|
|
59
|
+
useEffect,
|
|
60
|
+
useMemo,
|
|
61
|
+
useRef,
|
|
62
|
+
useState,
|
|
63
|
+
} from "react";
|
|
64
|
+
import { useTableSelection } from "./use-table-selection.ts";
|
|
65
|
+
|
|
66
|
+
type IconType = ComponentType<SVGProps<SVGSVGElement>>;
|
|
67
|
+
|
|
68
|
+
export interface ColumnDef<T> {
|
|
69
|
+
label: string;
|
|
70
|
+
cell: (item: T) => ReactNode;
|
|
71
|
+
sortable?: boolean;
|
|
72
|
+
/**
|
|
73
|
+
* Sort key sent to the API. Defaults to the column key.
|
|
74
|
+
*/
|
|
75
|
+
sortKey?: string;
|
|
76
|
+
/**
|
|
77
|
+
* When true, the column starts hidden. The user can still toggle it
|
|
78
|
+
* on via the built-in column picker. Pass `hideColumnPicker` on the
|
|
79
|
+
* table to forbid toggling entirely.
|
|
80
|
+
*/
|
|
81
|
+
defaultHidden?: boolean;
|
|
82
|
+
className?: string;
|
|
83
|
+
align?: "left" | "right" | "center";
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Context passed to every row-action `onClick`. `refresh()` re-fires the
|
|
88
|
+
* current fetch with the current filters/sort — call it after a mutation
|
|
89
|
+
* so the table reflects the new state without a manual reload.
|
|
90
|
+
*/
|
|
91
|
+
export interface RowActionContext {
|
|
92
|
+
refresh: () => void;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export interface RowAction<T> {
|
|
96
|
+
label: string;
|
|
97
|
+
icon?: IconType;
|
|
98
|
+
onClick: (item: T, ctx: RowActionContext) => void | Promise<void>;
|
|
99
|
+
destructive?: boolean;
|
|
100
|
+
disabled?: (item: T) => boolean;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Context passed to every bulk-action `onClick`. `clearSelection()`
|
|
105
|
+
* empties the checkbox set; `refresh()` re-fires the current fetch.
|
|
106
|
+
*/
|
|
107
|
+
export interface BulkActionContext {
|
|
108
|
+
refresh: () => void;
|
|
109
|
+
clearSelection: () => void;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export interface BulkAction<T> {
|
|
113
|
+
label: string;
|
|
114
|
+
icon?: IconType;
|
|
115
|
+
onClick: (selected: T[], ctx: BulkActionContext) => void | Promise<void>;
|
|
116
|
+
destructive?: boolean;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* A standalone toolbar icon-button, rendered in the right-hand icon group
|
|
121
|
+
* next to the column picker and separated from the filter area by a divider.
|
|
122
|
+
* Use for table-scoped actions (e.g. "Upload", "New") that aren't tied to a
|
|
123
|
+
* row or a selection. The table renders the ghost icon button + tooltip so it
|
|
124
|
+
* matches the built-in column-picker / refresh controls.
|
|
125
|
+
*/
|
|
126
|
+
export interface TableAction {
|
|
127
|
+
icon: IconType;
|
|
128
|
+
label: string;
|
|
129
|
+
onClick: () => void;
|
|
130
|
+
disabled?: boolean;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* High-level filter slot. AlephaTable creates the `useForm` internally,
|
|
135
|
+
* wraps `render`'s output in a `<form>` element, persists values under
|
|
136
|
+
* `persistenceKey` when set, and refetches on submit (and on every
|
|
137
|
+
* change, debounced, by default).
|
|
138
|
+
*
|
|
139
|
+
* The render function receives the typed form so callers wire inputs
|
|
140
|
+
* with `form.input.<field>` exactly like a hand-rolled `useForm`.
|
|
141
|
+
*/
|
|
142
|
+
export interface AlephaTableFilters {
|
|
143
|
+
schema: ZObject;
|
|
144
|
+
initialValues?: Record<string, any>;
|
|
145
|
+
render: (form: FormModel<ZObject>) => ReactNode;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
interface SortState {
|
|
149
|
+
field: string;
|
|
150
|
+
direction: "asc" | "desc";
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export interface AlephaTableProps<T> {
|
|
154
|
+
/**
|
|
155
|
+
* Fetcher invoked with paging + sort + filters. Should return an
|
|
156
|
+
* Alepha `Page<T>`.
|
|
157
|
+
*/
|
|
158
|
+
fetch: (params: {
|
|
159
|
+
page: number;
|
|
160
|
+
size: number;
|
|
161
|
+
sort?: string;
|
|
162
|
+
filters?: Record<string, any>;
|
|
163
|
+
}) => Promise<Page<T>>;
|
|
164
|
+
/**
|
|
165
|
+
* Column definitions, keyed by the property name they read from.
|
|
166
|
+
*/
|
|
167
|
+
columns: Record<string, ColumnDef<T>>;
|
|
168
|
+
/**
|
|
169
|
+
* Per-row action menu builder. Return an array of `RowAction` per
|
|
170
|
+
* item. Each `onClick` receives `(item, { refresh })`.
|
|
171
|
+
*/
|
|
172
|
+
rowActions?: (item: T) => RowAction<T>[];
|
|
173
|
+
/**
|
|
174
|
+
* Actions applied to selected rows (enables checkbox column). Each
|
|
175
|
+
* `onClick` receives `(items, { refresh, clearSelection })`.
|
|
176
|
+
*/
|
|
177
|
+
bulkActions?: BulkAction<T>[];
|
|
178
|
+
/**
|
|
179
|
+
* Default page size.
|
|
180
|
+
*/
|
|
181
|
+
defaultSize?: number;
|
|
182
|
+
/**
|
|
183
|
+
* Stable row identifier. Defaults to `item.id`.
|
|
184
|
+
*/
|
|
185
|
+
rowKey?: (item: T) => string;
|
|
186
|
+
/**
|
|
187
|
+
* Click handler invoked when a row is clicked (excluding action
|
|
188
|
+
* buttons).
|
|
189
|
+
*/
|
|
190
|
+
onRowClick?: (item: T) => void;
|
|
191
|
+
/**
|
|
192
|
+
* Auto-refresh interval in ms (only when document is visible).
|
|
193
|
+
*/
|
|
194
|
+
pollMs?: number;
|
|
195
|
+
/**
|
|
196
|
+
* External refetch trigger. Bump this value (e.g. from a `useState`
|
|
197
|
+
* counter) after a mutation performed *outside* the table — such as a
|
|
198
|
+
* toolbar upload action — to make the table reload. Row/bulk actions
|
|
199
|
+
* already get `ctx.refresh()`; this is the escape hatch for everything
|
|
200
|
+
* else. Changing it resets to page 0 and refetches; the initial value
|
|
201
|
+
* is ignored (the table fetches on mount regardless).
|
|
202
|
+
*/
|
|
203
|
+
refreshSignal?: number | string;
|
|
204
|
+
/**
|
|
205
|
+
* High-level filter form. AlephaTable owns the `useForm`, renders the
|
|
206
|
+
* inputs inside a `<form>` in the toolbar, and refetches on
|
|
207
|
+
* submit/change.
|
|
208
|
+
*
|
|
209
|
+
* Mutually exclusive with `form` (legacy: caller-owned form). When
|
|
210
|
+
* both are passed, `filters` wins.
|
|
211
|
+
*/
|
|
212
|
+
filters?: AlephaTableFilters;
|
|
213
|
+
/**
|
|
214
|
+
* When set, filter values, column visibility, and sort state are
|
|
215
|
+
* persisted to `localStorage` under this key. Pick a key that's
|
|
216
|
+
* unique per page and per scope (e.g. `"admin.users"`,
|
|
217
|
+
* `\`lor.board.${campaignId}\``).
|
|
218
|
+
*/
|
|
219
|
+
persistenceKey?: string;
|
|
220
|
+
/**
|
|
221
|
+
* Hide the built-in column visibility dropdown in the toolbar.
|
|
222
|
+
*/
|
|
223
|
+
hideColumnPicker?: boolean;
|
|
224
|
+
/**
|
|
225
|
+
* Hide the built-in actions menu (Refresh, Reset filters).
|
|
226
|
+
*/
|
|
227
|
+
hideActionsMenu?: boolean;
|
|
228
|
+
/**
|
|
229
|
+
* Extra slot rendered to the right of the filter inputs in the
|
|
230
|
+
* toolbar — typically a "New" / "Create" button.
|
|
231
|
+
*/
|
|
232
|
+
toolbar?: ReactNode;
|
|
233
|
+
/**
|
|
234
|
+
* Standalone icon-button actions rendered in the toolbar's right-hand
|
|
235
|
+
* icon group, before the column picker and separated from the filter
|
|
236
|
+
* area by a divider. The table renders the ghost icon button + tooltip
|
|
237
|
+
* itself, so callers only supply the icon/label/handler.
|
|
238
|
+
*/
|
|
239
|
+
actions?: TableAction[];
|
|
240
|
+
/**
|
|
241
|
+
* Extra classes applied to the outer wrapper.
|
|
242
|
+
*/
|
|
243
|
+
className?: string;
|
|
244
|
+
/**
|
|
245
|
+
* Message shown when the page is empty. Defaults to "No results".
|
|
246
|
+
*/
|
|
247
|
+
emptyMessage?: string;
|
|
248
|
+
/**
|
|
249
|
+
* Rich empty-state node rendered when the page is empty — e.g. an icon +
|
|
250
|
+
* message + optional call-to-action. Overrides `emptyMessage` when set.
|
|
251
|
+
*/
|
|
252
|
+
empty?: ReactNode;
|
|
253
|
+
/**
|
|
254
|
+
* Free-form content rendered above the toolbar (e.g. a page title).
|
|
255
|
+
*/
|
|
256
|
+
header?: ReactNode;
|
|
257
|
+
/**
|
|
258
|
+
* Initial sort state. When `persistenceKey` is set, a persisted sort
|
|
259
|
+
* takes precedence over this.
|
|
260
|
+
*/
|
|
261
|
+
defaultSort?: SortState | null;
|
|
262
|
+
/**
|
|
263
|
+
* Called whenever the user toggles a column header. Receives the new
|
|
264
|
+
* sort state (`null` when sort is cleared). Use this if you need a
|
|
265
|
+
* persistence layer beyond `persistenceKey` (e.g. URL state).
|
|
266
|
+
*/
|
|
267
|
+
onSortChange?: (sort: SortState | null) => void;
|
|
268
|
+
/**
|
|
269
|
+
* Legacy: caller-owned filter form. Prefer `filters` (which has
|
|
270
|
+
* AlephaTable own the form). When `filters` is set, this prop is
|
|
271
|
+
* ignored.
|
|
272
|
+
*/
|
|
273
|
+
form?: FormModel<ZObject>;
|
|
274
|
+
/**
|
|
275
|
+
* When true (default when `filters` is set), the table refetches on
|
|
276
|
+
* every `form:change` event, debounced by 250ms. Set to `false` to
|
|
277
|
+
* require an explicit submit.
|
|
278
|
+
*/
|
|
279
|
+
autoApplyFilters?: boolean;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
const defaultRowKey = (item: unknown): string =>
|
|
283
|
+
String((item as { id?: unknown })?.id ?? Math.random());
|
|
284
|
+
|
|
285
|
+
const EMPTY_FILTERS_SCHEMA = z.object({}) as ZObject;
|
|
286
|
+
|
|
287
|
+
/** Synchronous localStorage read. Returns undefined on miss or error. */
|
|
288
|
+
const readPersisted = <V,>(key: string, suffix: string): V | undefined => {
|
|
289
|
+
if (typeof window === "undefined") return undefined;
|
|
290
|
+
try {
|
|
291
|
+
const raw = window.localStorage.getItem(`${key}.${suffix}`);
|
|
292
|
+
return raw == null ? undefined : (JSON.parse(raw) as V);
|
|
293
|
+
} catch {
|
|
294
|
+
return undefined;
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
/** Synchronous localStorage write. Empty objects/null delete the key. */
|
|
299
|
+
const writePersisted = (key: string, suffix: string, value: unknown): void => {
|
|
300
|
+
if (typeof window === "undefined") return;
|
|
301
|
+
const fullKey = `${key}.${suffix}`;
|
|
302
|
+
try {
|
|
303
|
+
const isEmptyObject =
|
|
304
|
+
value !== null &&
|
|
305
|
+
typeof value === "object" &&
|
|
306
|
+
!Array.isArray(value) &&
|
|
307
|
+
Object.keys(value as object).length === 0;
|
|
308
|
+
const isEmptyArray = Array.isArray(value) && value.length === 0;
|
|
309
|
+
if (
|
|
310
|
+
value === undefined ||
|
|
311
|
+
value === null ||
|
|
312
|
+
isEmptyObject ||
|
|
313
|
+
isEmptyArray
|
|
314
|
+
) {
|
|
315
|
+
window.localStorage.removeItem(fullKey);
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
window.localStorage.setItem(fullKey, JSON.stringify(value));
|
|
319
|
+
} catch {
|
|
320
|
+
// localStorage may be unavailable (private mode, quota). Skip.
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
export function AlephaTable<T>(props: AlephaTableProps<T>) {
|
|
325
|
+
const rowKey = props.rowKey ?? defaultRowKey;
|
|
326
|
+
const size = props.defaultSize ?? 20;
|
|
327
|
+
const alepha = useAlepha();
|
|
328
|
+
const { tr } = useI18n();
|
|
329
|
+
|
|
330
|
+
// -- Filter form (internal when `filters` is set, else legacy `form`) -----
|
|
331
|
+
|
|
332
|
+
// Read persisted filter values synchronously so they reach useForm's
|
|
333
|
+
// first invocation. Reading inside an effect would be too late —
|
|
334
|
+
// useForm captures `initialValues` only once via useMemo.
|
|
335
|
+
const persistedFilterValues = useMemo(() => {
|
|
336
|
+
if (!props.persistenceKey || !props.filters) return undefined;
|
|
337
|
+
return readPersisted<Record<string, any>>(props.persistenceKey, "filters");
|
|
338
|
+
}, [props.persistenceKey, props.filters]);
|
|
339
|
+
|
|
340
|
+
const mergedFilterInitialValues = useMemo<Record<string, any>>(
|
|
341
|
+
() => ({
|
|
342
|
+
...(props.filters?.initialValues ?? {}),
|
|
343
|
+
...(persistedFilterValues ?? {}),
|
|
344
|
+
}),
|
|
345
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
346
|
+
[],
|
|
347
|
+
);
|
|
348
|
+
|
|
349
|
+
// Always call useForm to keep hook order stable. When the caller
|
|
350
|
+
// doesn't pass `filters`, the internal form has an empty schema and
|
|
351
|
+
// is simply unused.
|
|
352
|
+
const internalForm = useForm({
|
|
353
|
+
schema: props.filters?.schema ?? EMPTY_FILTERS_SCHEMA,
|
|
354
|
+
initialValues: mergedFilterInitialValues,
|
|
355
|
+
handler: async () => {
|
|
356
|
+
// No-op — the table subscribes to `form:submit:success` to refetch.
|
|
357
|
+
},
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
const form = props.filters ? internalForm : props.form;
|
|
361
|
+
|
|
362
|
+
// -- Paging / sort / data --------------------------------------------------
|
|
363
|
+
|
|
364
|
+
const [page, setPage] = useState(0);
|
|
365
|
+
const [sort, setSort] = useState<SortState | null>(() => {
|
|
366
|
+
if (props.persistenceKey) {
|
|
367
|
+
const persisted = readPersisted<SortState>(props.persistenceKey, "sort");
|
|
368
|
+
if (persisted) return persisted;
|
|
369
|
+
}
|
|
370
|
+
return props.defaultSort ?? null;
|
|
371
|
+
});
|
|
372
|
+
const [data, setData] = useState<T[]>([]);
|
|
373
|
+
const [meta, setMeta] = useState<Page<T>["page"] | null>(null);
|
|
374
|
+
const [loading, setLoading] = useState(false);
|
|
375
|
+
const [refreshKey, setRefreshKey] = useState(0);
|
|
376
|
+
const [isRefreshing, setIsRefreshing] = useState(false);
|
|
377
|
+
|
|
378
|
+
// Alepha's pagination parser reads "field" as asc and "-field" as desc.
|
|
379
|
+
// Multiple comma-separated entries are multi-column sort, so we must
|
|
380
|
+
// NOT use `field,direction` syntax — that would treat "asc"/"desc" as
|
|
381
|
+
// a second column name and 500 on the backend.
|
|
382
|
+
const sortParam = sort
|
|
383
|
+
? sort.direction === "desc"
|
|
384
|
+
? `-${sort.field}`
|
|
385
|
+
: sort.field
|
|
386
|
+
: undefined;
|
|
387
|
+
|
|
388
|
+
// Hold the latest `fetch` in a ref so it is NOT a dependency of `load`.
|
|
389
|
+
// Callers pass `fetch` inline (a new function every render), and a fetcher
|
|
390
|
+
// that writes a store atom the caller also subscribes to would otherwise
|
|
391
|
+
// self-trigger: load → atom write → caller re-render → new `fetch` → new
|
|
392
|
+
// `load` → effect re-runs → infinite loop. The ref keeps the newest closure
|
|
393
|
+
// available while `load` only re-runs on actual inputs (page/size/sort/…).
|
|
394
|
+
const fetchRef = useRef(props.fetch);
|
|
395
|
+
fetchRef.current = props.fetch;
|
|
396
|
+
|
|
397
|
+
const load = useCallback(async () => {
|
|
398
|
+
setLoading(true);
|
|
399
|
+
try {
|
|
400
|
+
const res = await fetchRef.current({
|
|
401
|
+
page,
|
|
402
|
+
size,
|
|
403
|
+
sort: sortParam,
|
|
404
|
+
filters: form?.currentValues,
|
|
405
|
+
});
|
|
406
|
+
setData(res.content);
|
|
407
|
+
setMeta(res.page);
|
|
408
|
+
} catch (error) {
|
|
409
|
+
// Surface read failures through the same `react:action:error` channel
|
|
410
|
+
// that useAction/useQuery use, so a mounted <ActionErrorToaster /> toasts
|
|
411
|
+
// them. Keep the previous rows on screen rather than blanking the table.
|
|
412
|
+
void alepha.events.emit("react:action:error", {
|
|
413
|
+
type: "custom",
|
|
414
|
+
id: "alepha-table:load",
|
|
415
|
+
error: error as Error,
|
|
416
|
+
});
|
|
417
|
+
} finally {
|
|
418
|
+
setLoading(false);
|
|
419
|
+
}
|
|
420
|
+
}, [page, size, sortParam, refreshKey, form, alepha]);
|
|
421
|
+
|
|
422
|
+
useEffect(() => {
|
|
423
|
+
void load();
|
|
424
|
+
}, [load]);
|
|
425
|
+
|
|
426
|
+
// Persist sort to localStorage on every change.
|
|
427
|
+
useEffect(() => {
|
|
428
|
+
if (!props.persistenceKey) return;
|
|
429
|
+
writePersisted(props.persistenceKey, "sort", sort);
|
|
430
|
+
}, [props.persistenceKey, sort]);
|
|
431
|
+
|
|
432
|
+
// -- Refresh + reset wiring -----------------------------------------------
|
|
433
|
+
|
|
434
|
+
const refresh = useCallback(() => {
|
|
435
|
+
setPage(0);
|
|
436
|
+
setRefreshKey((k) => k + 1);
|
|
437
|
+
}, []);
|
|
438
|
+
|
|
439
|
+
const handleRefreshClick = useCallback(() => {
|
|
440
|
+
setIsRefreshing(true);
|
|
441
|
+
refresh();
|
|
442
|
+
setTimeout(() => setIsRefreshing(false), 1000);
|
|
443
|
+
}, [refresh]);
|
|
444
|
+
|
|
445
|
+
// React to the external `refreshSignal` prop. The first render seeds the
|
|
446
|
+
// ref without refetching (the mount effect already loads); every later
|
|
447
|
+
// change triggers a refresh. Kept separate from `load`'s deps so an inline
|
|
448
|
+
// `fetch` closure can't self-trigger a loop (see `fetchRef` above).
|
|
449
|
+
const refreshSignalRef = useRef(props.refreshSignal);
|
|
450
|
+
useEffect(() => {
|
|
451
|
+
if (refreshSignalRef.current === props.refreshSignal) return;
|
|
452
|
+
refreshSignalRef.current = props.refreshSignal;
|
|
453
|
+
refresh();
|
|
454
|
+
}, [props.refreshSignal, refresh]);
|
|
455
|
+
|
|
456
|
+
const resetFilters = useCallback(() => {
|
|
457
|
+
if (!form || !props.filters) return;
|
|
458
|
+
// Per-field `.set(undefined)` is necessary: `setInitialValues({})`
|
|
459
|
+
// doesn't emit `form:change` for deleted keys, so inputs stay
|
|
460
|
+
// visually populated and subscribers don't refetch. Explicit set
|
|
461
|
+
// keeps everyone in sync.
|
|
462
|
+
const keys = Object.keys(z.schema.shape(props.filters.schema));
|
|
463
|
+
for (const key of keys) {
|
|
464
|
+
const input = (form.input as Record<string, { set?: (v: any) => void }>)[
|
|
465
|
+
key
|
|
466
|
+
];
|
|
467
|
+
input?.set?.(undefined);
|
|
468
|
+
}
|
|
469
|
+
}, [form, props.filters]);
|
|
470
|
+
|
|
471
|
+
// -- Form event subscriptions ---------------------------------------------
|
|
472
|
+
|
|
473
|
+
// Refetch on explicit submit (manual Apply, programmatic submit, etc.).
|
|
474
|
+
useEffect(() => {
|
|
475
|
+
if (!form) return;
|
|
476
|
+
return alepha.events.on("form:submit:success", (event) => {
|
|
477
|
+
if (event.id !== form.id) return;
|
|
478
|
+
setPage(0);
|
|
479
|
+
setRefreshKey((k) => k + 1);
|
|
480
|
+
});
|
|
481
|
+
}, [alepha, form]);
|
|
482
|
+
|
|
483
|
+
// Refetch on change (debounced) when autoApplyFilters is on. Default
|
|
484
|
+
// is on whenever AlephaTable owns the form (`filters` prop).
|
|
485
|
+
const autoApply = props.autoApplyFilters ?? Boolean(props.filters);
|
|
486
|
+
useEffect(() => {
|
|
487
|
+
if (!form || !autoApply) return;
|
|
488
|
+
let timeout: ReturnType<typeof setTimeout> | undefined;
|
|
489
|
+
const unsub = alepha.events.on("form:change", (event) => {
|
|
490
|
+
if (event.id !== form.id) return;
|
|
491
|
+
if (timeout) clearTimeout(timeout);
|
|
492
|
+
timeout = setTimeout(() => {
|
|
493
|
+
setPage(0);
|
|
494
|
+
setRefreshKey((k) => k + 1);
|
|
495
|
+
}, 250);
|
|
496
|
+
});
|
|
497
|
+
return () => {
|
|
498
|
+
if (timeout) clearTimeout(timeout);
|
|
499
|
+
unsub();
|
|
500
|
+
};
|
|
501
|
+
}, [alepha, form, autoApply]);
|
|
502
|
+
|
|
503
|
+
// Persist filter values to localStorage on change.
|
|
504
|
+
useEffect(() => {
|
|
505
|
+
if (!props.persistenceKey || !form || !props.filters) return;
|
|
506
|
+
const persist = () => {
|
|
507
|
+
const clean: Record<string, any> = {};
|
|
508
|
+
const values = form.currentValues ?? {};
|
|
509
|
+
for (const [k, v] of Object.entries(values)) {
|
|
510
|
+
if (v === undefined || v === null || v === "") continue;
|
|
511
|
+
if (Array.isArray(v) && v.length === 0) continue;
|
|
512
|
+
clean[k] = v;
|
|
513
|
+
}
|
|
514
|
+
writePersisted(props.persistenceKey!, "filters", clean);
|
|
515
|
+
};
|
|
516
|
+
const unsubs = [
|
|
517
|
+
alepha.events.on("form:change", (event) => {
|
|
518
|
+
if (event.id !== form.id) return;
|
|
519
|
+
persist();
|
|
520
|
+
}),
|
|
521
|
+
alepha.events.on("form:submit:success", (event) => {
|
|
522
|
+
if (event.id !== form.id) return;
|
|
523
|
+
persist();
|
|
524
|
+
}),
|
|
525
|
+
];
|
|
526
|
+
return () => {
|
|
527
|
+
for (const u of unsubs) u();
|
|
528
|
+
};
|
|
529
|
+
}, [alepha, form, props.filters, props.persistenceKey]);
|
|
530
|
+
|
|
531
|
+
// -- Polling ---------------------------------------------------------------
|
|
532
|
+
|
|
533
|
+
useEffect(() => {
|
|
534
|
+
if (!props.pollMs) return;
|
|
535
|
+
const id = setInterval(() => {
|
|
536
|
+
if (document.visibilityState === "visible") setRefreshKey((k) => k + 1);
|
|
537
|
+
}, props.pollMs);
|
|
538
|
+
return () => clearInterval(id);
|
|
539
|
+
}, [props.pollMs]);
|
|
540
|
+
|
|
541
|
+
// -- Selection -------------------------------------------------------------
|
|
542
|
+
|
|
543
|
+
const {
|
|
544
|
+
selection,
|
|
545
|
+
selectedItems,
|
|
546
|
+
allSelected,
|
|
547
|
+
someSelected,
|
|
548
|
+
toggleRow,
|
|
549
|
+
toggleAll,
|
|
550
|
+
clearSelection,
|
|
551
|
+
} = useTableSelection(data, rowKey);
|
|
552
|
+
|
|
553
|
+
// -- Sort ------------------------------------------------------------------
|
|
554
|
+
|
|
555
|
+
const toggleSort = (col: string, def: ColumnDef<T>) => {
|
|
556
|
+
if (!def.sortable) return;
|
|
557
|
+
const field = def.sortKey ?? col;
|
|
558
|
+
setSort((s) => {
|
|
559
|
+
const next: SortState | null =
|
|
560
|
+
!s || s.field !== field
|
|
561
|
+
? { field, direction: "asc" }
|
|
562
|
+
: s.direction === "asc"
|
|
563
|
+
? { field, direction: "desc" }
|
|
564
|
+
: null;
|
|
565
|
+
props.onSortChange?.(next);
|
|
566
|
+
return next;
|
|
567
|
+
});
|
|
568
|
+
};
|
|
569
|
+
|
|
570
|
+
// -- Column visibility -----------------------------------------------------
|
|
571
|
+
|
|
572
|
+
const allColumnKeys = useMemo(
|
|
573
|
+
() => Object.keys(props.columns),
|
|
574
|
+
[props.columns],
|
|
575
|
+
);
|
|
576
|
+
|
|
577
|
+
const [visibleColumns, setVisibleColumns] = useState<Set<string>>(() => {
|
|
578
|
+
if (props.persistenceKey) {
|
|
579
|
+
const persisted = readPersisted<string[]>(
|
|
580
|
+
props.persistenceKey,
|
|
581
|
+
"columns",
|
|
582
|
+
);
|
|
583
|
+
if (persisted) {
|
|
584
|
+
return new Set(persisted.filter((k) => k in props.columns));
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
return new Set(
|
|
588
|
+
allColumnKeys.filter((k) => !props.columns[k].defaultHidden),
|
|
589
|
+
);
|
|
590
|
+
});
|
|
591
|
+
|
|
592
|
+
useEffect(() => {
|
|
593
|
+
if (!props.persistenceKey) return;
|
|
594
|
+
writePersisted(props.persistenceKey, "columns", [...visibleColumns]);
|
|
595
|
+
}, [props.persistenceKey, visibleColumns]);
|
|
596
|
+
|
|
597
|
+
const toggleColumn = (id: string) => {
|
|
598
|
+
setVisibleColumns((prev) => {
|
|
599
|
+
const next = new Set(prev);
|
|
600
|
+
if (next.has(id)) next.delete(id);
|
|
601
|
+
else next.add(id);
|
|
602
|
+
return next;
|
|
603
|
+
});
|
|
604
|
+
};
|
|
605
|
+
|
|
606
|
+
// -- Render ---------------------------------------------------------------
|
|
607
|
+
|
|
608
|
+
const cols = Object.entries(props.columns) as Array<[string, ColumnDef<T>]>;
|
|
609
|
+
const visibleCols = cols.filter(([key]) => visibleColumns.has(key));
|
|
610
|
+
const hasCheckbox = Boolean(props.bulkActions?.length);
|
|
611
|
+
const hasRowActions = Boolean(props.rowActions);
|
|
612
|
+
|
|
613
|
+
const rowCtx: RowActionContext = useMemo(() => ({ refresh }), [refresh]);
|
|
614
|
+
const bulkCtx: BulkActionContext = useMemo(
|
|
615
|
+
() => ({ refresh, clearSelection }),
|
|
616
|
+
[refresh, clearSelection],
|
|
617
|
+
);
|
|
618
|
+
|
|
619
|
+
const hasActiveFilters = useMemo(() => {
|
|
620
|
+
if (!props.filters || !form) return false;
|
|
621
|
+
const values = form.currentValues ?? {};
|
|
622
|
+
for (const v of Object.values(values)) {
|
|
623
|
+
if (v === undefined || v === null || v === "") continue;
|
|
624
|
+
if (Array.isArray(v) && v.length === 0) continue;
|
|
625
|
+
return true;
|
|
626
|
+
}
|
|
627
|
+
return false;
|
|
628
|
+
}, [props.filters, form, refreshKey]);
|
|
629
|
+
|
|
630
|
+
const showToolbar =
|
|
631
|
+
Boolean(props.filters) ||
|
|
632
|
+
Boolean(props.toolbar) ||
|
|
633
|
+
Boolean(props.actions?.length) ||
|
|
634
|
+
!props.hideColumnPicker ||
|
|
635
|
+
!props.hideActionsMenu;
|
|
636
|
+
const showColumnPicker = !props.hideColumnPicker && allColumnKeys.length > 0;
|
|
637
|
+
const showActionsMenu = !props.hideActionsMenu;
|
|
638
|
+
|
|
639
|
+
return (
|
|
640
|
+
<ClientOnly>
|
|
641
|
+
<div className={cn("flex flex-col gap-2", props.className)}>
|
|
642
|
+
{props.header && (
|
|
643
|
+
<div className="flex items-center gap-2">
|
|
644
|
+
<div className="min-w-0 flex-1">{props.header}</div>
|
|
645
|
+
</div>
|
|
646
|
+
)}
|
|
647
|
+
|
|
648
|
+
{showToolbar && (
|
|
649
|
+
<div className="bg-card flex flex-wrap items-end gap-2 rounded-md rounded-b-none border p-2">
|
|
650
|
+
{props.filters && form ? (
|
|
651
|
+
<form
|
|
652
|
+
{...form.props}
|
|
653
|
+
className="flex flex-1 flex-wrap items-end gap-2"
|
|
654
|
+
>
|
|
655
|
+
{props.filters.render(form)}
|
|
656
|
+
</form>
|
|
657
|
+
) : (
|
|
658
|
+
<div className="flex flex-1" />
|
|
659
|
+
)}
|
|
660
|
+
{props.toolbar}
|
|
661
|
+
<TooltipProvider>
|
|
662
|
+
<div className="flex items-end gap-1">
|
|
663
|
+
{props.actions?.length ? (
|
|
664
|
+
<>
|
|
665
|
+
{props.actions.map((action) => {
|
|
666
|
+
const ActionIcon = action.icon;
|
|
667
|
+
return (
|
|
668
|
+
<Tooltip key={action.label}>
|
|
669
|
+
<TooltipTrigger
|
|
670
|
+
render={
|
|
671
|
+
<Button
|
|
672
|
+
type="button"
|
|
673
|
+
size="sm"
|
|
674
|
+
variant="ghost"
|
|
675
|
+
className="h-9 w-9 p-0"
|
|
676
|
+
aria-label={action.label}
|
|
677
|
+
disabled={action.disabled}
|
|
678
|
+
onClick={action.onClick}
|
|
679
|
+
/>
|
|
680
|
+
}
|
|
681
|
+
>
|
|
682
|
+
<ActionIcon className="size-4" />
|
|
683
|
+
</TooltipTrigger>
|
|
684
|
+
<TooltipContent>{action.label}</TooltipContent>
|
|
685
|
+
</Tooltip>
|
|
686
|
+
);
|
|
687
|
+
})}
|
|
688
|
+
{(showColumnPicker || showActionsMenu) && (
|
|
689
|
+
<span
|
|
690
|
+
aria-hidden
|
|
691
|
+
className="bg-border mx-1 h-5 w-px self-center"
|
|
692
|
+
/>
|
|
693
|
+
)}
|
|
694
|
+
</>
|
|
695
|
+
) : null}
|
|
696
|
+
{showColumnPicker && (
|
|
697
|
+
<ColumnPicker<T>
|
|
698
|
+
columns={props.columns}
|
|
699
|
+
visible={visibleColumns}
|
|
700
|
+
onToggle={toggleColumn}
|
|
701
|
+
/>
|
|
702
|
+
)}
|
|
703
|
+
{showActionsMenu && props.filters && (
|
|
704
|
+
<Tooltip>
|
|
705
|
+
<TooltipTrigger
|
|
706
|
+
render={
|
|
707
|
+
<Button
|
|
708
|
+
type="button"
|
|
709
|
+
size="sm"
|
|
710
|
+
variant="ghost"
|
|
711
|
+
className="h-9 w-9 p-0"
|
|
712
|
+
aria-label={tr("alephaTable.resetFilters", {
|
|
713
|
+
default: "Reset filters",
|
|
714
|
+
})}
|
|
715
|
+
disabled={!hasActiveFilters}
|
|
716
|
+
onClick={resetFilters}
|
|
717
|
+
/>
|
|
718
|
+
}
|
|
719
|
+
>
|
|
720
|
+
<FunnelX className="size-4" />
|
|
721
|
+
</TooltipTrigger>
|
|
722
|
+
<TooltipContent>
|
|
723
|
+
{tr("alephaTable.resetFilters", {
|
|
724
|
+
default: "Reset filters",
|
|
725
|
+
})}
|
|
726
|
+
</TooltipContent>
|
|
727
|
+
</Tooltip>
|
|
728
|
+
)}
|
|
729
|
+
{showActionsMenu && (
|
|
730
|
+
<Tooltip>
|
|
731
|
+
<TooltipTrigger
|
|
732
|
+
render={
|
|
733
|
+
<Button
|
|
734
|
+
type="button"
|
|
735
|
+
size="sm"
|
|
736
|
+
variant="ghost"
|
|
737
|
+
className="h-9 w-9 p-0"
|
|
738
|
+
aria-label={tr("alephaTable.refresh", {
|
|
739
|
+
default: "Refresh",
|
|
740
|
+
})}
|
|
741
|
+
disabled={isRefreshing}
|
|
742
|
+
onClick={handleRefreshClick}
|
|
743
|
+
/>
|
|
744
|
+
}
|
|
745
|
+
>
|
|
746
|
+
<RefreshCw
|
|
747
|
+
className={cn("size-4", isRefreshing && "animate-spin")}
|
|
748
|
+
/>
|
|
749
|
+
</TooltipTrigger>
|
|
750
|
+
<TooltipContent>
|
|
751
|
+
{tr("alephaTable.refresh", { default: "Refresh" })}
|
|
752
|
+
</TooltipContent>
|
|
753
|
+
</Tooltip>
|
|
754
|
+
)}
|
|
755
|
+
</div>
|
|
756
|
+
</TooltipProvider>
|
|
757
|
+
</div>
|
|
758
|
+
)}
|
|
759
|
+
|
|
760
|
+
{hasCheckbox && selection.size > 0 && (
|
|
761
|
+
// Linear-style floating action pill: fixed at the bottom-center of
|
|
762
|
+
// the viewport, dark surface that stays readable in both themes
|
|
763
|
+
// because the colors are hard-coded (theme-relative `bg-foreground`
|
|
764
|
+
// inverts awkwardly against a white container in dark mode).
|
|
765
|
+
<div className="pointer-events-none fixed inset-x-0 bottom-6 z-40 flex justify-center">
|
|
766
|
+
<div className="pointer-events-auto flex animate-in fade-in-0 slide-in-from-bottom-2 items-center gap-2 rounded-full bg-zinc-900 px-3 py-1.5 text-zinc-100 shadow-lg ring-1 ring-white/10 duration-150">
|
|
767
|
+
<span className="text-sm pl-2">
|
|
768
|
+
{tr("alephaTable.selected", {
|
|
769
|
+
default: `${selection.size} selected`,
|
|
770
|
+
args: [String(selection.size)],
|
|
771
|
+
})}
|
|
772
|
+
</span>
|
|
773
|
+
<span className="mx-1 h-4 w-px bg-white/20" />
|
|
774
|
+
{props.bulkActions?.map((action) => {
|
|
775
|
+
const ActionIcon = action.icon;
|
|
776
|
+
return (
|
|
777
|
+
<Button
|
|
778
|
+
key={action.label}
|
|
779
|
+
size="sm"
|
|
780
|
+
className={
|
|
781
|
+
action.destructive
|
|
782
|
+
? "h-8 bg-red-600 text-white hover:bg-red-500"
|
|
783
|
+
: "h-8 bg-transparent text-zinc-100 hover:bg-white/10 hover:text-zinc-100"
|
|
784
|
+
}
|
|
785
|
+
onClick={() => action.onClick(selectedItems, bulkCtx)}
|
|
786
|
+
>
|
|
787
|
+
{ActionIcon && <ActionIcon className="size-4" />}
|
|
788
|
+
{action.label}
|
|
789
|
+
</Button>
|
|
790
|
+
);
|
|
791
|
+
})}
|
|
792
|
+
<span className="mx-1 h-4 w-px bg-white/20" />
|
|
793
|
+
<Button
|
|
794
|
+
size="icon"
|
|
795
|
+
className="size-8 bg-transparent text-zinc-300 hover:bg-white/10 hover:text-zinc-100"
|
|
796
|
+
onClick={clearSelection}
|
|
797
|
+
aria-label={tr("alephaTable.clearSelection", {
|
|
798
|
+
default: "Clear selection",
|
|
799
|
+
})}
|
|
800
|
+
>
|
|
801
|
+
<X className="size-4" />
|
|
802
|
+
</Button>
|
|
803
|
+
</div>
|
|
804
|
+
</div>
|
|
805
|
+
)}
|
|
806
|
+
|
|
807
|
+
<div
|
|
808
|
+
className={cn(
|
|
809
|
+
"flex min-h-0 flex-1 flex-col overflow-auto rounded-md border",
|
|
810
|
+
showToolbar && "-mt-2 rounded-t-none border-t-0",
|
|
811
|
+
)}
|
|
812
|
+
>
|
|
813
|
+
<Table>
|
|
814
|
+
<TableHeader className="bg-background sticky top-0 z-10 shadow-[inset_0_-1px_0_0_var(--border)]">
|
|
815
|
+
<TableRow>
|
|
816
|
+
{hasCheckbox && (
|
|
817
|
+
<TableHead className="w-10">
|
|
818
|
+
<Checkbox
|
|
819
|
+
checked={allSelected}
|
|
820
|
+
indeterminate={!allSelected && someSelected}
|
|
821
|
+
onCheckedChange={() => toggleAll()}
|
|
822
|
+
aria-label={tr("alephaTable.selectAll", {
|
|
823
|
+
default: "Select all rows",
|
|
824
|
+
})}
|
|
825
|
+
/>
|
|
826
|
+
</TableHead>
|
|
827
|
+
)}
|
|
828
|
+
{visibleCols.map(([key, def]) => {
|
|
829
|
+
const sorted =
|
|
830
|
+
def.sortable && sort?.field === (def.sortKey ?? key);
|
|
831
|
+
return (
|
|
832
|
+
<TableHead
|
|
833
|
+
key={key}
|
|
834
|
+
className={cn(
|
|
835
|
+
def.className,
|
|
836
|
+
def.align === "right" && "text-right",
|
|
837
|
+
def.align === "center" && "text-center",
|
|
838
|
+
)}
|
|
839
|
+
aria-sort={
|
|
840
|
+
sorted
|
|
841
|
+
? sort?.direction === "asc"
|
|
842
|
+
? "ascending"
|
|
843
|
+
: "descending"
|
|
844
|
+
: undefined
|
|
845
|
+
}
|
|
846
|
+
>
|
|
847
|
+
{def.sortable ? (
|
|
848
|
+
// A real button so keyboard and assistive-tech
|
|
849
|
+
// users can sort — a th onClick is mouse-only.
|
|
850
|
+
<button
|
|
851
|
+
type="button"
|
|
852
|
+
onClick={() => toggleSort(key, def)}
|
|
853
|
+
className="hover:text-foreground inline-flex cursor-pointer select-none items-center gap-1"
|
|
854
|
+
>
|
|
855
|
+
{def.label}
|
|
856
|
+
{sorted && (
|
|
857
|
+
<span aria-hidden>
|
|
858
|
+
{sort?.direction === "asc" ? "↑" : "↓"}
|
|
859
|
+
</span>
|
|
860
|
+
)}
|
|
861
|
+
</button>
|
|
862
|
+
) : (
|
|
863
|
+
<span className="inline-flex items-center gap-1">
|
|
864
|
+
{def.label}
|
|
865
|
+
</span>
|
|
866
|
+
)}
|
|
867
|
+
</TableHead>
|
|
868
|
+
);
|
|
869
|
+
})}
|
|
870
|
+
{hasRowActions && <TableHead className="w-10" />}
|
|
871
|
+
</TableRow>
|
|
872
|
+
</TableHeader>
|
|
873
|
+
<TableBody>
|
|
874
|
+
{loading && data.length === 0 ? (
|
|
875
|
+
<SkeletonRows
|
|
876
|
+
rows={5}
|
|
877
|
+
cols={
|
|
878
|
+
visibleCols.length +
|
|
879
|
+
(hasCheckbox ? 1 : 0) +
|
|
880
|
+
(hasRowActions ? 1 : 0)
|
|
881
|
+
}
|
|
882
|
+
/>
|
|
883
|
+
) : data.length === 0 ? (
|
|
884
|
+
<TableRow className="hover:bg-transparent">
|
|
885
|
+
<TableCell
|
|
886
|
+
colSpan={
|
|
887
|
+
visibleCols.length +
|
|
888
|
+
(hasCheckbox ? 1 : 0) +
|
|
889
|
+
(hasRowActions ? 1 : 0)
|
|
890
|
+
}
|
|
891
|
+
className="p-0"
|
|
892
|
+
>
|
|
893
|
+
{props.empty ?? (
|
|
894
|
+
<div className="text-muted-foreground flex flex-col items-center justify-center gap-2 py-12 text-center">
|
|
895
|
+
<Inbox className="size-8 opacity-40" />
|
|
896
|
+
<p className="text-sm">
|
|
897
|
+
{props.emptyMessage ??
|
|
898
|
+
String(
|
|
899
|
+
tr("alephaTable.empty", {
|
|
900
|
+
default: "No results.",
|
|
901
|
+
}),
|
|
902
|
+
)}
|
|
903
|
+
</p>
|
|
904
|
+
</div>
|
|
905
|
+
)}
|
|
906
|
+
</TableCell>
|
|
907
|
+
</TableRow>
|
|
908
|
+
) : (
|
|
909
|
+
data.map((item) => {
|
|
910
|
+
const key = rowKey(item);
|
|
911
|
+
const isSelected = selection.has(key);
|
|
912
|
+
return (
|
|
913
|
+
<TableRow
|
|
914
|
+
key={key}
|
|
915
|
+
onClick={() => props.onRowClick?.(item)}
|
|
916
|
+
className={cn(
|
|
917
|
+
props.onRowClick && "cursor-pointer",
|
|
918
|
+
isSelected && "bg-muted/30",
|
|
919
|
+
)}
|
|
920
|
+
>
|
|
921
|
+
{hasCheckbox && (
|
|
922
|
+
<TableCell onClick={(e) => e.stopPropagation()}>
|
|
923
|
+
<Checkbox
|
|
924
|
+
checked={isSelected}
|
|
925
|
+
onCheckedChange={() => toggleRow(item)}
|
|
926
|
+
aria-label={tr("alephaTable.selectRow", {
|
|
927
|
+
default: "Select row",
|
|
928
|
+
})}
|
|
929
|
+
/>
|
|
930
|
+
</TableCell>
|
|
931
|
+
)}
|
|
932
|
+
{visibleCols.map(([key, def]) => (
|
|
933
|
+
<TableCell
|
|
934
|
+
key={key}
|
|
935
|
+
className={cn(
|
|
936
|
+
def.className,
|
|
937
|
+
def.align === "right" && "text-right",
|
|
938
|
+
def.align === "center" && "text-center",
|
|
939
|
+
)}
|
|
940
|
+
>
|
|
941
|
+
{def.cell(item)}
|
|
942
|
+
</TableCell>
|
|
943
|
+
))}
|
|
944
|
+
{hasRowActions && (
|
|
945
|
+
<TableCell
|
|
946
|
+
className="text-right"
|
|
947
|
+
onClick={(e) => e.stopPropagation()}
|
|
948
|
+
>
|
|
949
|
+
<RowActionsMenu
|
|
950
|
+
actions={props.rowActions!(item)}
|
|
951
|
+
item={item}
|
|
952
|
+
ctx={rowCtx}
|
|
953
|
+
/>
|
|
954
|
+
</TableCell>
|
|
955
|
+
)}
|
|
956
|
+
</TableRow>
|
|
957
|
+
);
|
|
958
|
+
})
|
|
959
|
+
)}
|
|
960
|
+
</TableBody>
|
|
961
|
+
</Table>
|
|
962
|
+
</div>
|
|
963
|
+
|
|
964
|
+
<div className="flex flex-wrap items-center justify-between gap-2">
|
|
965
|
+
<p className="text-muted-foreground text-xs">
|
|
966
|
+
{meta
|
|
967
|
+
? `Page ${meta.number + 1}${meta.totalPages ? ` of ${meta.totalPages}` : ""} · ${meta.numberOfElements} of ${meta.totalElements ?? "?"}`
|
|
968
|
+
: "—"}
|
|
969
|
+
</p>
|
|
970
|
+
{meta && meta.totalPages && meta.totalPages > 1 ? (
|
|
971
|
+
<Pagination className="mx-0 w-auto justify-end">
|
|
972
|
+
<PaginationContent>
|
|
973
|
+
<PaginationItem>
|
|
974
|
+
<PaginationPrevious
|
|
975
|
+
href="#"
|
|
976
|
+
onClick={(e) => {
|
|
977
|
+
e.preventDefault();
|
|
978
|
+
if (!meta.isFirst) setPage((p) => Math.max(0, p - 1));
|
|
979
|
+
}}
|
|
980
|
+
aria-disabled={meta.isFirst}
|
|
981
|
+
className={cn(
|
|
982
|
+
meta.isFirst && "pointer-events-none opacity-50",
|
|
983
|
+
)}
|
|
984
|
+
/>
|
|
985
|
+
</PaginationItem>
|
|
986
|
+
{computePageItems(meta.number + 1, meta.totalPages).map(
|
|
987
|
+
(item, idx) =>
|
|
988
|
+
item === "ellipsis" ? (
|
|
989
|
+
<PaginationItem key={`e-${idx}`}>
|
|
990
|
+
<PaginationEllipsis />
|
|
991
|
+
</PaginationItem>
|
|
992
|
+
) : (
|
|
993
|
+
<PaginationItem key={item}>
|
|
994
|
+
<PaginationLink
|
|
995
|
+
href="#"
|
|
996
|
+
isActive={item === meta.number + 1}
|
|
997
|
+
onClick={(e) => {
|
|
998
|
+
e.preventDefault();
|
|
999
|
+
setPage(item - 1);
|
|
1000
|
+
}}
|
|
1001
|
+
>
|
|
1002
|
+
{item}
|
|
1003
|
+
</PaginationLink>
|
|
1004
|
+
</PaginationItem>
|
|
1005
|
+
),
|
|
1006
|
+
)}
|
|
1007
|
+
<PaginationItem>
|
|
1008
|
+
<PaginationNext
|
|
1009
|
+
href="#"
|
|
1010
|
+
onClick={(e) => {
|
|
1011
|
+
e.preventDefault();
|
|
1012
|
+
if (!meta.isLast) setPage((p) => p + 1);
|
|
1013
|
+
}}
|
|
1014
|
+
aria-disabled={meta.isLast}
|
|
1015
|
+
className={cn(
|
|
1016
|
+
meta.isLast && "pointer-events-none opacity-50",
|
|
1017
|
+
)}
|
|
1018
|
+
/>
|
|
1019
|
+
</PaginationItem>
|
|
1020
|
+
</PaginationContent>
|
|
1021
|
+
</Pagination>
|
|
1022
|
+
) : null}
|
|
1023
|
+
</div>
|
|
1024
|
+
</div>
|
|
1025
|
+
</ClientOnly>
|
|
1026
|
+
);
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
function ColumnPicker<T>(props: {
|
|
1030
|
+
columns: Record<string, ColumnDef<T>>;
|
|
1031
|
+
visible: Set<string>;
|
|
1032
|
+
onToggle: (key: string) => void;
|
|
1033
|
+
}) {
|
|
1034
|
+
const { tr } = useI18n();
|
|
1035
|
+
const entries = Object.entries(props.columns);
|
|
1036
|
+
return (
|
|
1037
|
+
<DropdownMenu>
|
|
1038
|
+
<DropdownMenuTrigger
|
|
1039
|
+
render={
|
|
1040
|
+
<Button
|
|
1041
|
+
type="button"
|
|
1042
|
+
size="sm"
|
|
1043
|
+
variant="ghost"
|
|
1044
|
+
className="h-9 w-9 p-0"
|
|
1045
|
+
aria-label={tr("alephaTable.toggleColumns", {
|
|
1046
|
+
default: "Toggle columns",
|
|
1047
|
+
})}
|
|
1048
|
+
/>
|
|
1049
|
+
}
|
|
1050
|
+
>
|
|
1051
|
+
<Columns3 className="size-4" />
|
|
1052
|
+
</DropdownMenuTrigger>
|
|
1053
|
+
<DropdownMenuContent align="end">
|
|
1054
|
+
<DropdownMenuGroup>
|
|
1055
|
+
<DropdownMenuLabel>
|
|
1056
|
+
{tr("alephaTable.columns", { default: "Columns" })}
|
|
1057
|
+
</DropdownMenuLabel>
|
|
1058
|
+
<DropdownMenuSeparator />
|
|
1059
|
+
{entries.map(([key, def]) => (
|
|
1060
|
+
<DropdownMenuCheckboxItem
|
|
1061
|
+
key={key}
|
|
1062
|
+
checked={props.visible.has(key)}
|
|
1063
|
+
closeOnClick={false}
|
|
1064
|
+
onCheckedChange={() => props.onToggle(key)}
|
|
1065
|
+
>
|
|
1066
|
+
{def.label}
|
|
1067
|
+
</DropdownMenuCheckboxItem>
|
|
1068
|
+
))}
|
|
1069
|
+
</DropdownMenuGroup>
|
|
1070
|
+
</DropdownMenuContent>
|
|
1071
|
+
</DropdownMenu>
|
|
1072
|
+
);
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
/**
|
|
1076
|
+
* Build the visible page-number sequence with ellipses. Always shows
|
|
1077
|
+
* first + last, ±1 around current. Gaps collapse into a single ellipsis.
|
|
1078
|
+
* `current` and `total` are 1-indexed.
|
|
1079
|
+
*/
|
|
1080
|
+
function computePageItems(
|
|
1081
|
+
current: number,
|
|
1082
|
+
total: number,
|
|
1083
|
+
): Array<number | "ellipsis"> {
|
|
1084
|
+
if (total <= 7) return Array.from({ length: total }, (_, i) => i + 1);
|
|
1085
|
+
const items: Array<number | "ellipsis"> = [1];
|
|
1086
|
+
if (current > 3) items.push("ellipsis");
|
|
1087
|
+
const start = Math.max(2, current - 1);
|
|
1088
|
+
const end = Math.min(total - 1, current + 1);
|
|
1089
|
+
for (let i = start; i <= end; i++) items.push(i);
|
|
1090
|
+
if (current < total - 2) items.push("ellipsis");
|
|
1091
|
+
items.push(total);
|
|
1092
|
+
return items;
|
|
1093
|
+
}
|
|
1094
|
+
|
|
1095
|
+
function SkeletonRows(props: { rows: number; cols: number }) {
|
|
1096
|
+
return (
|
|
1097
|
+
<>
|
|
1098
|
+
{Array.from({ length: props.rows }).map((_, i) => (
|
|
1099
|
+
<TableRow key={i}>
|
|
1100
|
+
{Array.from({ length: props.cols }).map((_, j) => (
|
|
1101
|
+
<TableCell key={j}>
|
|
1102
|
+
<Skeleton className="h-4 w-full" />
|
|
1103
|
+
</TableCell>
|
|
1104
|
+
))}
|
|
1105
|
+
</TableRow>
|
|
1106
|
+
))}
|
|
1107
|
+
</>
|
|
1108
|
+
);
|
|
1109
|
+
}
|
|
1110
|
+
|
|
1111
|
+
function RowActionsMenu<T>(props: {
|
|
1112
|
+
actions: RowAction<T>[];
|
|
1113
|
+
item: T;
|
|
1114
|
+
ctx: RowActionContext;
|
|
1115
|
+
}) {
|
|
1116
|
+
const { tr } = useI18n();
|
|
1117
|
+
if (props.actions.length === 0) return null;
|
|
1118
|
+
return (
|
|
1119
|
+
<DropdownMenu>
|
|
1120
|
+
<DropdownMenuTrigger
|
|
1121
|
+
render={
|
|
1122
|
+
<Button
|
|
1123
|
+
variant="ghost"
|
|
1124
|
+
size="icon"
|
|
1125
|
+
aria-label={tr("alephaTable.openRowActions", {
|
|
1126
|
+
default: "Open row actions",
|
|
1127
|
+
})}
|
|
1128
|
+
/>
|
|
1129
|
+
}
|
|
1130
|
+
>
|
|
1131
|
+
<MoreVertical className="size-4" />
|
|
1132
|
+
</DropdownMenuTrigger>
|
|
1133
|
+
<DropdownMenuContent align="end">
|
|
1134
|
+
{props.actions.map((action, idx) => {
|
|
1135
|
+
const Icon = action.icon;
|
|
1136
|
+
const disabled = action.disabled?.(props.item);
|
|
1137
|
+
const sep =
|
|
1138
|
+
idx > 0 &&
|
|
1139
|
+
action.destructive &&
|
|
1140
|
+
!props.actions[idx - 1].destructive;
|
|
1141
|
+
return (
|
|
1142
|
+
<span key={action.label}>
|
|
1143
|
+
{sep && <DropdownMenuSeparator />}
|
|
1144
|
+
<DropdownMenuItem
|
|
1145
|
+
disabled={disabled}
|
|
1146
|
+
onClick={() => action.onClick(props.item, props.ctx)}
|
|
1147
|
+
className={action.destructive ? "text-destructive" : undefined}
|
|
1148
|
+
>
|
|
1149
|
+
{Icon && <Icon className="mr-2 size-4" />}
|
|
1150
|
+
{action.label}
|
|
1151
|
+
</DropdownMenuItem>
|
|
1152
|
+
</span>
|
|
1153
|
+
);
|
|
1154
|
+
})}
|
|
1155
|
+
</DropdownMenuContent>
|
|
1156
|
+
</DropdownMenu>
|
|
1157
|
+
);
|
|
1158
|
+
}
|