@kivacore/ui-pro-shared 0.0.9 → 0.0.10

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 (145) hide show
  1. package/package.json +187 -21
  2. package/src/.kiva/config.json +27 -0
  3. package/src/components/avatar-group.tsx +222 -0
  4. package/src/components/kiva/bulk-update-dialog/bulk-update-dialog.tsx +240 -0
  5. package/src/components/kiva/bulk-update-dialog/index.tsx +6 -0
  6. package/src/components/kiva/bulk-update-dialog/types.ts +33 -0
  7. package/src/components/kiva/calendar/animations.ts +40 -0
  8. package/src/components/kiva/calendar/calendar-body.tsx +106 -0
  9. package/src/components/kiva/calendar/calendar-sidebar.tsx +103 -0
  10. package/src/components/kiva/calendar/calendar.tsx +243 -0
  11. package/src/components/kiva/calendar/constants.ts +12 -0
  12. package/src/components/kiva/calendar/contexts/calendar-context.tsx +696 -0
  13. package/src/components/kiva/calendar/contexts/dnd-context.tsx +173 -0
  14. package/src/components/kiva/calendar/dialogs/add-edit-event-dialog.tsx +287 -0
  15. package/src/components/kiva/calendar/dialogs/delete-event-dialog.tsx +73 -0
  16. package/src/components/kiva/calendar/dialogs/event-details-dialog.tsx +163 -0
  17. package/src/components/kiva/calendar/dialogs/events-list-dialog.tsx +122 -0
  18. package/src/components/kiva/calendar/dnd/draggable-event.tsx +66 -0
  19. package/src/components/kiva/calendar/dnd/droppable-area.tsx +53 -0
  20. package/src/components/kiva/calendar/dnd/resizable-event.tsx +190 -0
  21. package/src/components/kiva/calendar/header/calendar-header.tsx +84 -0
  22. package/src/components/kiva/calendar/header/date-navigator.tsx +188 -0
  23. package/src/components/kiva/calendar/header/filter.tsx +80 -0
  24. package/src/components/kiva/calendar/header/today-button.tsx +49 -0
  25. package/src/components/kiva/calendar/header/user-select.tsx +111 -0
  26. package/src/components/kiva/calendar/header/view-tabs.tsx +144 -0
  27. package/src/components/kiva/calendar/helpers.ts +617 -0
  28. package/src/components/kiva/calendar/hooks/use-disclosure.ts +19 -0
  29. package/src/components/kiva/calendar/hooks/use-local-storage.ts +40 -0
  30. package/src/components/kiva/calendar/hooks/use-media-query.ts +25 -0
  31. package/src/components/kiva/calendar/hooks.ts +5 -0
  32. package/src/components/kiva/calendar/index.tsx +27 -0
  33. package/src/components/kiva/calendar/interfaces.ts +35 -0
  34. package/src/components/kiva/calendar/schemas.ts +20 -0
  35. package/src/components/kiva/calendar/settings/settings.tsx +116 -0
  36. package/src/components/kiva/calendar/skeletons/calendar-header-skeleton.tsx +27 -0
  37. package/src/components/kiva/calendar/skeletons/calendar-skeleton.tsx +19 -0
  38. package/src/components/kiva/calendar/skeletons/day-view-skeleton.tsx +40 -0
  39. package/src/components/kiva/calendar/skeletons/month-view-skeleton.tsx +36 -0
  40. package/src/components/kiva/calendar/skeletons/week-view-skeleton.tsx +48 -0
  41. package/src/components/kiva/calendar/skeletons/year-view-skeleton.tsx +42 -0
  42. package/src/components/kiva/calendar/types.ts +25 -0
  43. package/src/components/kiva/calendar/views/agenda-view/agenda-events.tsx +150 -0
  44. package/src/components/kiva/calendar/views/month-view/calendar-month-view.tsx +82 -0
  45. package/src/components/kiva/calendar/views/month-view/day-cell.tsx +218 -0
  46. package/src/components/kiva/calendar/views/month-view/event-bullet.tsx +46 -0
  47. package/src/components/kiva/calendar/views/month-view/month-event-badge.tsx +154 -0
  48. package/src/components/kiva/calendar/views/timeline/calendar-timeline-view.tsx +100 -0
  49. package/src/components/kiva/calendar/views/timeline/lib/timeline-layout.ts +130 -0
  50. package/src/components/kiva/calendar/views/timeline/lib/timeline-model.ts +504 -0
  51. package/src/components/kiva/calendar/views/timeline/timeline-event-bar.tsx +68 -0
  52. package/src/components/kiva/calendar/views/timeline/timeline-grid-header.tsx +125 -0
  53. package/src/components/kiva/calendar/views/timeline/timeline-row.tsx +159 -0
  54. package/src/components/kiva/calendar/views/timeline/timeline-section.tsx +61 -0
  55. package/src/components/kiva/calendar/views/timeline/use-timeline-sections.ts +278 -0
  56. package/src/components/kiva/calendar/views/week-and-day-view/calendar-day-view.tsx +277 -0
  57. package/src/components/kiva/calendar/views/week-and-day-view/calendar-time-line.tsx +47 -0
  58. package/src/components/kiva/calendar/views/week-and-day-view/calendar-week-view.tsx +229 -0
  59. package/src/components/kiva/calendar/views/week-and-day-view/day-view-multi-day-events-row.tsx +79 -0
  60. package/src/components/kiva/calendar/views/week-and-day-view/event-block.tsx +116 -0
  61. package/src/components/kiva/calendar/views/week-and-day-view/render-grouped-events.tsx +54 -0
  62. package/src/components/kiva/calendar/views/week-and-day-view/week-view-multi-day-events-row.tsx +159 -0
  63. package/src/components/kiva/calendar/views/year-view/calendar-year-view.tsx +159 -0
  64. package/src/components/kiva/data-table-side-filters/components/side-filter-active-chips.tsx +137 -0
  65. package/src/components/kiva/data-table-side-filters/components/side-filter-clear.tsx +34 -0
  66. package/src/components/kiva/data-table-side-filters/components/side-filter-search.tsx +67 -0
  67. package/src/components/kiva/data-table-side-filters/components/side-filter-section.tsx +77 -0
  68. package/src/components/kiva/data-table-side-filters/controllers/side-filter-async-options.tsx +354 -0
  69. package/src/components/kiva/data-table-side-filters/controllers/side-filter-boolean.tsx +66 -0
  70. package/src/components/kiva/data-table-side-filters/controllers/side-filter-checkbox-list.tsx +137 -0
  71. package/src/components/kiva/data-table-side-filters/controllers/side-filter-date-range.tsx +204 -0
  72. package/src/components/kiva/data-table-side-filters/controllers/side-filter-numeric-range.tsx +162 -0
  73. package/src/components/kiva/data-table-side-filters/controllers/side-filter-options-dropdown.tsx +191 -0
  74. package/src/components/kiva/data-table-side-filters/controllers/side-filter-text.tsx +53 -0
  75. package/src/components/kiva/data-table-side-filters/core/query-translator.ts +367 -0
  76. package/src/components/kiva/data-table-side-filters/core/types.ts +81 -0
  77. package/src/components/kiva/data-table-side-filters/data-table-side-filters.tsx +483 -0
  78. package/src/components/kiva/data-table-side-filters/hooks/use-data-table-side-filters.tsx +160 -0
  79. package/src/components/kiva/data-table-side-filters/hooks/use-show-more.tsx +62 -0
  80. package/src/components/kiva/data-table-side-filters/index.tsx +44 -0
  81. package/src/components/kiva/data-table-side-filters/lib/render-mode.ts +62 -0
  82. package/src/components/kiva/date-time-picker.tsx +163 -0
  83. package/src/components/kiva/editable-record-detail.tsx +809 -0
  84. package/src/components/kiva/enum-option-editor/enum-option-editor.tsx +891 -0
  85. package/src/components/kiva/enum-option-editor/index.tsx +26 -0
  86. package/src/components/kiva/enum-option-editor/kiva-enum-option-editor.tsx +320 -0
  87. package/src/components/kiva/enum-option-editor/types.ts +90 -0
  88. package/src/components/kiva/kanban.tsx +1376 -0
  89. package/src/components/kiva/kiva-data-export-menu.tsx +543 -0
  90. package/src/components/kiva/map.tsx +1631 -0
  91. package/src/components/kiva/pivot-calendar/components/group-sidebar.tsx +95 -0
  92. package/src/components/kiva/pivot-calendar/components/pivot-calendar-toolbar.tsx +113 -0
  93. package/src/components/kiva/pivot-calendar/components/stats-cell.tsx +126 -0
  94. package/src/components/kiva/pivot-calendar/helpers.ts +374 -0
  95. package/src/components/kiva/pivot-calendar/hooks/use-pivot-calendar.ts +222 -0
  96. package/src/components/kiva/pivot-calendar/index.tsx +32 -0
  97. package/src/components/kiva/pivot-calendar/pivot-calendar.tsx +107 -0
  98. package/src/components/kiva/pivot-calendar/types.ts +154 -0
  99. package/src/components/kiva/pivot-calendar/views/days-of-month-view.tsx +37 -0
  100. package/src/components/kiva/pivot-calendar/views/days-of-week-view.tsx +37 -0
  101. package/src/components/kiva/pivot-calendar/views/month-calendar-view.tsx +154 -0
  102. package/src/components/kiva/pivot-calendar/views/months-of-year-view.tsx +40 -0
  103. package/src/components/kiva/pivot-calendar/views/pivot-grid-view.tsx +251 -0
  104. package/src/components/kiva/pivot-filter/index.tsx +9 -0
  105. package/src/components/kiva/pivot-filter/pivot-filter-item.tsx +159 -0
  106. package/src/components/kiva/pivot-filter/pivot-filter-settings.tsx +161 -0
  107. package/src/components/kiva/pivot-filter/pivot-filter.tsx +187 -0
  108. package/src/components/kiva/pivot-filter/types.ts +82 -0
  109. package/src/components/kiva/pivot-grid/hooks/use-pivot-grid.ts +683 -0
  110. package/src/components/kiva/pivot-grid/index.tsx +23 -0
  111. package/src/components/kiva/pivot-grid/lib/pivot-grid-color-rules.ts +98 -0
  112. package/src/components/kiva/pivot-grid/lib/pivot-grid-export.ts +265 -0
  113. package/src/components/kiva/pivot-grid/lib/pivot-grid-model.ts +1110 -0
  114. package/src/components/kiva/pivot-grid/pivot-grid-demo-data.ts +200 -0
  115. package/src/components/kiva/pivot-grid/pivot-grid-export-menu.tsx +66 -0
  116. package/src/components/kiva/pivot-grid/pivot-grid-toolbar.tsx +85 -0
  117. package/src/components/kiva/pivot-grid/pivot-grid.tsx +601 -0
  118. package/src/components/kiva/pivot-grid/types.ts +195 -0
  119. package/src/components/kiva/place-autocomplete.tsx +484 -0
  120. package/src/components/kiva/record-delete-confirm-dialog/index.tsx +10 -0
  121. package/src/components/kiva/record-delete-confirm-dialog/record-delete-confirm-dialog.tsx +266 -0
  122. package/src/components/kiva/record-delete-confirm-dialog/types.ts +25 -0
  123. package/src/hooks/kiva/_internal/autonumber-setting.ts +112 -0
  124. package/src/hooks/kiva/_internal/form-layout-to-view.ts +665 -0
  125. package/src/hooks/kiva/_internal/kiva-hook-utils.ts +143 -0
  126. package/src/hooks/kiva/_internal/use-computed-field-evaluator.tsx +310 -0
  127. package/src/hooks/kiva/_internal/use-field-actions-evaluator.tsx +398 -0
  128. package/src/hooks/kiva/_internal/use-form-actions-evaluator.tsx +363 -0
  129. package/src/hooks/kiva/_internal/use-form-view-engine.tsx +2200 -0
  130. package/src/hooks/kiva/_internal/use-kiva-form-view-fields.tsx +190 -0
  131. package/src/hooks/kiva/_internal/use-kiva-kanban-cards.tsx +311 -0
  132. package/src/hooks/kiva/use-data-export.tsx +265 -0
  133. package/src/hooks/kiva/use-kiva-calendar.tsx +992 -0
  134. package/src/hooks/kiva/use-kiva-data-export.tsx +173 -0
  135. package/src/hooks/kiva/use-kiva-data-grid.tsx +4750 -0
  136. package/src/hooks/kiva/use-kiva-data-view-select.tsx +1273 -0
  137. package/src/hooks/kiva/use-kiva-form-view.tsx +1581 -0
  138. package/src/hooks/kiva/use-kiva-inventory.tsx +184 -0
  139. package/src/hooks/kiva/use-kiva-kanban.tsx +1750 -0
  140. package/src/hooks/kiva/use-kiva-map-view.tsx +2255 -0
  141. package/src/hooks/kiva/use-kiva-pivot-calendar.tsx +759 -0
  142. package/src/hooks/kiva/use-kiva-pivot-filter.tsx +1523 -0
  143. package/src/hooks/kiva/use-kiva-pivot-grid.tsx +543 -0
  144. package/src/hooks/kiva/use-kiva-tenant.tsx +313 -0
  145. package/src/hooks/kiva/use-parameter-persistence.ts +191 -0
@@ -0,0 +1,543 @@
1
+ 'use client';
2
+
3
+ // @ts-nocheck
4
+ /* eslint-disable */
5
+ import {
6
+ useCallback, useEffect, useMemo, useState
7
+ } from 'react';
8
+
9
+ import {
10
+ Columns3,
11
+ Download,
12
+ Loader2,
13
+ Search,
14
+ Settings2,
15
+ Table2
16
+ } from 'lucide-react';
17
+ import { toast } from 'sonner';
18
+
19
+ import {
20
+ Dialog,
21
+ DialogContent,
22
+ DialogDescription,
23
+ DialogFooter,
24
+ DialogHeader,
25
+ DialogTitle
26
+ } from '@kivacore/ui-pro-shared/components/dialog';
27
+ import {
28
+ DropdownMenu,
29
+ DropdownMenuContent,
30
+ DropdownMenuItem,
31
+ DropdownMenuLabel,
32
+ DropdownMenuRadioGroup,
33
+ DropdownMenuRadioItem,
34
+ DropdownMenuSeparator,
35
+ DropdownMenuTrigger
36
+ } from '@kivacore/ui-pro-shared/components/dropdown-menu';
37
+ import { Button } from '@kivacore/ui-pro-shared/components/button';
38
+ import { Checkbox } from '@kivacore/ui-pro-shared/components/checkbox';
39
+ import { Input } from '@kivacore/ui-pro-shared/components/input';
40
+ import { ScrollArea } from '@kivacore/ui-pro-shared/components/scroll-area';
41
+ import { ToggleGroup, ToggleGroupItem } from '@kivacore/ui-pro-shared/components/toggle-group';
42
+ import { cn } from '@kivacore/ui-pro-shared/lib/utils';
43
+
44
+ import { useUiTranslation } from '@kivacore/ui-pro-shared/hooks/kiva/use-ui-translation';
45
+
46
+ /** Which column set the user picked from the menu. */
47
+ export type KivaDataExportScope = 'visible' | 'all' | 'custom';
48
+
49
+ /**
50
+ * Per-column representation variant. For relation/user/enum-like fields the
51
+ * server can project the same logical column as multiple physical columns
52
+ * (display label, raw id, relation autonumber).
53
+ */
54
+ export type KivaDataExportColumnVariant = 'name' | 'id' | 'autonumber_id';
55
+
56
+ /** A single column entry inside a custom export selection. */
57
+ export interface KivaDataExportColumn {
58
+ /** Field slug. */
59
+ slug: string;
60
+ /**
61
+ * Variants to project. Empty/undefined keeps the server default (single
62
+ * display column). Populate to ask the server for additional projections,
63
+ * e.g. `{ slug: 'country', variants: ['id', 'name', 'autonumber_id'] }`
64
+ * which is encoded as `country(id,name,autonumber_id)`.
65
+ */
66
+ variants?: ReadonlyArray<KivaDataExportColumnVariant>;
67
+ }
68
+
69
+ /** File type the export endpoint writes. */
70
+ export type KivaDataExportFormat = 'xlsx' | 'csv';
71
+
72
+ /** Result reported by the menu when the user picks an entry. */
73
+ export type KivaDataExportSelection
74
+ = | { scope: 'visible'; format: KivaDataExportFormat }
75
+ | { scope: 'all'; format: KivaDataExportFormat }
76
+ | { scope: 'custom'; format: KivaDataExportFormat; columns: ReadonlyArray<KivaDataExportColumn> };
77
+
78
+ /** Minimal field shape the menu/dialog needs to render the picker. */
79
+ export interface KivaDataExportFieldOption {
80
+ slug: string;
81
+ name: string;
82
+ type: string;
83
+ }
84
+
85
+ /** Output order of a column's variants: the label, then the No, then the raw id. */
86
+ const VARIANT_ORDER: ReadonlyArray<KivaDataExportColumnVariant> = ['name', 'autonumber_id', 'id'];
87
+
88
+ function orderVariants(column: KivaDataExportColumn): Array<KivaDataExportColumnVariant> {
89
+ const picked = new Set(column.variants ?? []);
90
+
91
+ return VARIANT_ORDER.filter(variant => picked.has(variant));
92
+ }
93
+
94
+ /**
95
+ * Encode a column entry into the wire format the export endpoint expects:
96
+ * the bare `slug` when only the label is wanted (the endpoint writes a bare
97
+ * relation, user or choice field as its label), `slug(v1,v2)` when the No or
98
+ * the raw id is picked too, in `VARIANT_ORDER`.
99
+ */
100
+ export function encodeKivaDataExportColumn(
101
+ column: KivaDataExportColumn
102
+ ): string {
103
+ const variants = orderVariants(column);
104
+
105
+ if (variants.length === 0 || (variants.length === 1 && variants[0] === 'name')) return column.slug;
106
+
107
+ return `${column.slug}(${variants.join(',')})`;
108
+ }
109
+
110
+ /**
111
+ * The keys the export endpoint's `headers` map uses for an entry's columns:
112
+ * the bare slug for a single label column, `slug.variant` for each projected
113
+ * variant. `variant` is null for the bare slug.
114
+ */
115
+ export function kivaDataExportColumnKeys(
116
+ column: KivaDataExportColumn
117
+ ): Array<{ key: string; variant: KivaDataExportColumnVariant | null }> {
118
+ if (encodeKivaDataExportColumn(column) === column.slug) return [{ key: column.slug, variant: null }];
119
+
120
+ return orderVariants(column).map(variant => ({ key: `${column.slug}.${variant}`, variant }));
121
+ }
122
+
123
+ export interface KivaDataExportMenuProps {
124
+ /**
125
+ * Called when the user picks an entry. Should kick off the server export.
126
+ * A rejection is shown to the user as an error toast.
127
+ */
128
+ onExport: (selection: KivaDataExportSelection) => void | Promise<void>;
129
+ /**
130
+ * Exportable fields. When provided the menu surfaces a "Select columns…"
131
+ * entry that opens a dialog letting the user cherry-pick fields and pick
132
+ * variants for relation/user/enum-like columns.
133
+ */
134
+ fields?: ReadonlyArray<KivaDataExportFieldOption>;
135
+ /**
136
+ * Slugs that should start checked when the dialog opens. Defaults to every
137
+ * field in `fields`.
138
+ */
139
+ visibleSlugs?: ReadonlyArray<string>;
140
+ /** Show a spinner on the trigger and disable interactions while true. */
141
+ isExporting?: boolean;
142
+ /** Disable the trigger entirely (e.g. while the data source is still loading). */
143
+ disabled?: boolean;
144
+ /** Extra className applied to the trigger button. */
145
+ className?: string;
146
+ }
147
+
148
+ const RELATION_FIELD_TYPES = new Set<string>(['field-relation', 'field-relatedField']);
149
+
150
+ const USER_FIELD_TYPES = new Set<string>(['field-userSelect', 'field-userMultiSelect']);
151
+
152
+ const ENUM_FIELD_TYPES = new Set<string>([
153
+ 'field-select',
154
+ 'field-multiSelect',
155
+ 'field-radioGroup',
156
+ 'field-tagSelect',
157
+ 'field-status',
158
+ 'field-approvalStatus',
159
+ 'field-enum',
160
+ 'field-systemEnum'
161
+ ]);
162
+
163
+ function getFieldKind(
164
+ type: string
165
+ ): 'relation' | 'user' | 'enum' | 'plain' {
166
+ if (RELATION_FIELD_TYPES.has(type)) return 'relation';
167
+ if (USER_FIELD_TYPES.has(type)) return 'user';
168
+ if (ENUM_FIELD_TYPES.has(type)) return 'enum';
169
+
170
+ return 'plain';
171
+ }
172
+
173
+ function isVariantField(type: string): boolean {
174
+ return getFieldKind(type) !== 'plain';
175
+ }
176
+
177
+ /**
178
+ * Toolbar dropdown that surfaces server-side data-source exports. The menu
179
+ * itself doesn't know how to call the API — it just reports the user's
180
+ * selection so the consumer can wire it up to `useKivaDataExport`.
181
+ *
182
+ * When `fields` is supplied a third entry — "Select columns…" — opens a
183
+ * dialog that lets the user choose specific columns and pick projections
184
+ * for relation/user/enum-like columns (Name / ID / Autonumber ID).
185
+ */
186
+ export function KivaDataExportMenu({
187
+ onExport,
188
+ fields,
189
+ visibleSlugs,
190
+ isExporting = false,
191
+ disabled = false,
192
+ className
193
+ }: KivaDataExportMenuProps) {
194
+ const { t } = useUiTranslation();
195
+ const [dialogOpen, setDialogOpen] = useState(false);
196
+ const [format, setFormat] = useState<KivaDataExportFormat>('xlsx');
197
+
198
+ /** Resolves to whether the export went through; a failure is toasted here. */
199
+ const triggerExport = useCallback(
200
+ async (selection: KivaDataExportSelection): Promise<boolean> => {
201
+ try {
202
+ await onExport(selection);
203
+
204
+ return true;
205
+ } catch (error) {
206
+ toast.error(t('ui.dataGrid.exportFailed', 'Export failed'), {
207
+ description: error instanceof Error ? error.message : undefined
208
+ });
209
+
210
+ return false;
211
+ }
212
+ },
213
+ [onExport, t]
214
+ );
215
+
216
+ const handleScopeSelect = useCallback(
217
+ (scope: 'visible' | 'all') => {
218
+ void triggerExport({ scope, format });
219
+ },
220
+ [triggerExport, format]
221
+ );
222
+
223
+ const canPickColumns = Boolean(fields && fields.length > 0);
224
+
225
+ return (
226
+ <>
227
+ <DropdownMenu>
228
+ <DropdownMenuTrigger asChild>
229
+ <Button
230
+ type="button"
231
+ variant="outline"
232
+ size="icon-sm"
233
+ aria-label={t('ui.dataGrid.export', 'Export')}
234
+ disabled={disabled || isExporting}
235
+ className={cn(className)}>
236
+ {isExporting ? <Loader2 className="size-4 animate-spin" /> : <Download className="size-4" />}
237
+ </Button>
238
+ </DropdownMenuTrigger>
239
+ <DropdownMenuContent align="end" className="min-w-56">
240
+ <DropdownMenuLabel>
241
+ {t('ui.dataGrid.exportMenuLabel', 'Export')}
242
+ </DropdownMenuLabel>
243
+ <DropdownMenuRadioGroup
244
+ value={format}
245
+ onValueChange={value => setFormat(value === 'csv' ? 'csv' : 'xlsx')}>
246
+ {/* Picking a format keeps the menu open for the scope entries below. */}
247
+ <DropdownMenuRadioItem value="xlsx" onSelect={event => event.preventDefault()}>
248
+ {t('ui.dataGrid.exportFormatXlsx', 'Excel (.xlsx)')}
249
+ </DropdownMenuRadioItem>
250
+ <DropdownMenuRadioItem value="csv" onSelect={event => event.preventDefault()}>
251
+ {t('ui.dataGrid.exportFormatCsv', 'CSV')}
252
+ </DropdownMenuRadioItem>
253
+ </DropdownMenuRadioGroup>
254
+ <DropdownMenuSeparator />
255
+ <DropdownMenuItem onSelect={() => handleScopeSelect('visible')}>
256
+ <Columns3 className="size-4" />
257
+ {t('ui.dataGrid.exportVisibleColumns', 'Export visible columns')}
258
+ </DropdownMenuItem>
259
+ <DropdownMenuItem onSelect={() => handleScopeSelect('all')}>
260
+ <Table2 className="size-4" />
261
+ {t('ui.dataGrid.exportAllColumns', 'Export all columns')}
262
+ </DropdownMenuItem>
263
+ {canPickColumns && (
264
+ <>
265
+ <DropdownMenuSeparator />
266
+ <DropdownMenuItem onSelect={() => setDialogOpen(true)}>
267
+ <Settings2 className="size-4" />
268
+ {t('ui.dataGrid.exportSelectColumns', 'Select columns…')}
269
+ </DropdownMenuItem>
270
+ </>
271
+ )}
272
+ </DropdownMenuContent>
273
+ </DropdownMenu>
274
+
275
+ {canPickColumns && (
276
+ <KivaDataExportColumnDialog
277
+ open={dialogOpen}
278
+ onOpenChange={setDialogOpen}
279
+ fields={fields ?? []}
280
+ visibleSlugs={visibleSlugs}
281
+ isExporting={isExporting}
282
+ format={format}
283
+ onFormatChange={setFormat}
284
+ onExport={async (columns) => {
285
+ if (await triggerExport({ scope: 'custom', format, columns })) setDialogOpen(false);
286
+ }} />
287
+ )}
288
+ </>
289
+ );
290
+ }
291
+
292
+ interface KivaDataExportColumnDialogProps {
293
+ open: boolean;
294
+ onOpenChange: (open: boolean) => void;
295
+ fields: ReadonlyArray<KivaDataExportFieldOption>;
296
+ visibleSlugs?: ReadonlyArray<string>;
297
+ isExporting: boolean;
298
+ format: KivaDataExportFormat;
299
+ onFormatChange: (format: KivaDataExportFormat) => void;
300
+ onExport: (columns: ReadonlyArray<KivaDataExportColumn>) => Promise<void> | void;
301
+ }
302
+
303
+ function KivaDataExportColumnDialog({
304
+ open,
305
+ onOpenChange,
306
+ fields,
307
+ visibleSlugs,
308
+ isExporting,
309
+ format,
310
+ onFormatChange,
311
+ onExport
312
+ }: KivaDataExportColumnDialogProps) {
313
+ const { t } = useUiTranslation();
314
+
315
+ const [selected, setSelected] = useState<Record<string, boolean>>({});
316
+ const [variants, setVariants] = useState<Record<string, Array<KivaDataExportColumnVariant>>>({});
317
+ const [search, setSearch] = useState('');
318
+
319
+ useEffect(() => {
320
+ if (!open) return;
321
+
322
+ const initialSelected: Record<string, boolean> = {};
323
+ const initialVariants: Record<string, Array<KivaDataExportColumnVariant>> = {};
324
+ const visibleSet = visibleSlugs ? new Set(visibleSlugs) : null;
325
+
326
+ for (const field of fields) {
327
+ initialSelected[field.slug] = visibleSet ? visibleSet.has(field.slug) : true;
328
+
329
+ if (isVariantField(field.type)) {
330
+ initialVariants[field.slug] = ['name'];
331
+ }
332
+ }
333
+
334
+ queueMicrotask(() => {
335
+ setSelected(initialSelected);
336
+ setVariants(initialVariants);
337
+ setSearch('');
338
+ });
339
+ }, [open, fields, visibleSlugs]);
340
+
341
+ const filteredFields = useMemo(() => {
342
+ const query = search.trim().toLowerCase();
343
+
344
+ if (query.length === 0) return fields;
345
+
346
+ return fields.filter(field => field.name.toLowerCase().includes(query) || field.slug.toLowerCase().includes(query));
347
+ }, [fields, search]);
348
+
349
+ const selectedCount = useMemo(
350
+ () => fields.reduce((count, field) => (selected[field.slug] ? count + 1 : count), 0),
351
+ [fields, selected]
352
+ );
353
+
354
+ const allFilteredSelected = filteredFields.length > 0
355
+ && filteredFields.every(field => selected[field.slug]);
356
+
357
+ const handleToggleField = useCallback((slug: string, next: boolean) => {
358
+ setSelected(prev => ({ ...prev, [slug]: next }));
359
+ }, []);
360
+
361
+ const handleSelectAll = useCallback((next: boolean) => {
362
+ setSelected((prev) => {
363
+ const updated = { ...prev };
364
+
365
+ for (const field of filteredFields) {
366
+ updated[field.slug] = next;
367
+ }
368
+
369
+ return updated;
370
+ });
371
+ }, [filteredFields]);
372
+
373
+ const handleVariantChange = useCallback(
374
+ (slug: string, next: Array<string>) => {
375
+ const sanitized = next
376
+ .filter((value): value is KivaDataExportColumnVariant => value === 'name' || value === 'id' || value === 'autonumber_id');
377
+
378
+ setVariants(prev => ({
379
+ ...prev,
380
+ [slug]: sanitized.length > 0 ? sanitized : ['name']
381
+ }));
382
+ },
383
+ []
384
+ );
385
+
386
+ const buildColumns = useCallback((): Array<KivaDataExportColumn> => {
387
+ return fields
388
+ .filter(field => selected[field.slug])
389
+ .map((field) => {
390
+ if (!isVariantField(field.type)) {
391
+ return { slug: field.slug };
392
+ }
393
+
394
+ const picked = variants[field.slug] ?? ['name'];
395
+ const ordered = VARIANT_ORDER.filter(variant => picked.includes(variant));
396
+
397
+ return {
398
+ slug: field.slug,
399
+ variants: ordered.length > 0 ? ordered : (['name'] as Array<KivaDataExportColumnVariant>)
400
+ };
401
+ });
402
+ }, [fields, selected, variants]);
403
+
404
+ const handleSubmit = useCallback(async () => {
405
+ const columns = buildColumns();
406
+
407
+ if (columns.length === 0) return;
408
+
409
+ await onExport(columns);
410
+ }, [buildColumns, onExport]);
411
+
412
+ return (
413
+ <Dialog open={open} onOpenChange={onOpenChange}>
414
+ <DialogContent className="max-w-2xl gap-4">
415
+ <DialogHeader>
416
+ <DialogTitle>
417
+ {t('ui.dataGrid.exportSelectColumnsTitle', 'Select columns to export')}
418
+ </DialogTitle>
419
+ <DialogDescription>
420
+ {t(
421
+ 'ui.dataGrid.exportSelectColumnsDescription',
422
+ 'Pick the columns to include. For relation, user, and choice columns you can also export the raw value alongside the display label.'
423
+ )}
424
+ </DialogDescription>
425
+ </DialogHeader>
426
+
427
+ <div className="flex items-center gap-2">
428
+ <div className="relative flex-1">
429
+ <Search className="pointer-events-none absolute left-2.5 top-1/2 size-4 -translate-y-1/2 text-muted-foreground" />
430
+ <Input
431
+ type="search"
432
+ value={search}
433
+ onChange={event => setSearch(event.target.value)}
434
+ placeholder={t('ui.dataGrid.exportSearchColumns', 'Search columns')}
435
+ className="pl-8" />
436
+ </div>
437
+ <Button
438
+ type="button"
439
+ variant="ghost"
440
+ size="sm"
441
+ onClick={() => handleSelectAll(!allFilteredSelected)}>
442
+ {allFilteredSelected ? t('ui.dataGrid.exportClearAll', 'Clear all') : t('ui.dataGrid.exportSelectAll', 'Select all')}
443
+ </Button>
444
+ </div>
445
+
446
+ <ScrollArea className="h-[360px] rounded-md border">
447
+ <div className="divide-y">
448
+ {filteredFields.length === 0 ? (
449
+ <div className="px-4 py-12 text-center text-sm text-muted-foreground">
450
+ {t('ui.dataGrid.exportNoColumns', 'No matching columns')}
451
+ </div>
452
+ ) : (
453
+ filteredFields.map((field) => {
454
+ const checked = Boolean(selected[field.slug]);
455
+ const kind = getFieldKind(field.type);
456
+ const showAutonumber = kind === 'relation';
457
+ const value = variants[field.slug] ?? [];
458
+
459
+ return (
460
+ <div
461
+ key={field.slug}
462
+ className="flex flex-wrap items-center gap-3 px-3 py-2.5">
463
+ <label className="flex min-w-0 flex-1 cursor-pointer items-center gap-3">
464
+ <Checkbox
465
+ checked={checked}
466
+ onCheckedChange={next => handleToggleField(field.slug, next === true)} />
467
+ <span className="flex min-w-0 flex-col">
468
+ <span className="truncate text-sm font-medium">{field.name}</span>
469
+ <span className="truncate text-xs text-muted-foreground">{field.slug}</span>
470
+ </span>
471
+ </label>
472
+ {kind !== 'plain' && (
473
+ <ToggleGroup
474
+ type="multiple"
475
+ size="sm"
476
+ variant="outline"
477
+ value={value}
478
+ onValueChange={next => handleVariantChange(field.slug, next)}
479
+ disabled={!checked}
480
+ className="shrink-0">
481
+ <ToggleGroupItem value="name" aria-label={t('ui.dataGrid.exportVariantName', 'Name')}>
482
+ {t('ui.dataGrid.exportVariantName', 'Name')}
483
+ </ToggleGroupItem>
484
+ {showAutonumber && (
485
+ <ToggleGroupItem
486
+ value="autonumber_id"
487
+ aria-label={t('ui.dataGrid.exportVariantNo', 'No')}>
488
+ {t('ui.dataGrid.exportVariantNo', 'No')}
489
+ </ToggleGroupItem>
490
+ )}
491
+ <ToggleGroupItem value="id" aria-label={t('ui.dataGrid.exportVariantId', 'ID')}>
492
+ {t('ui.dataGrid.exportVariantId', 'ID')}
493
+ </ToggleGroupItem>
494
+ </ToggleGroup>
495
+ )}
496
+ </div>
497
+ );
498
+ })
499
+ )}
500
+ </div>
501
+ </ScrollArea>
502
+
503
+ <DialogFooter className="items-center sm:justify-between">
504
+ <span className="text-xs text-muted-foreground">
505
+ {`${selectedCount} / ${fields.length} ${t('ui.dataGrid.exportSelectedSuffix', 'selected')}`}
506
+ </span>
507
+ <div className="flex items-center gap-2">
508
+ <ToggleGroup
509
+ type="single"
510
+ size="sm"
511
+ variant="outline"
512
+ value={format}
513
+ onValueChange={(next) => {
514
+ if (next === 'xlsx' || next === 'csv') onFormatChange(next);
515
+ }}
516
+ aria-label={t('ui.dataGrid.exportFormat', 'Format')}>
517
+ <ToggleGroupItem value="xlsx">
518
+ {t('ui.dataGrid.exportFormatXlsxShort', 'Excel')}
519
+ </ToggleGroupItem>
520
+ <ToggleGroupItem value="csv">
521
+ {t('ui.dataGrid.exportFormatCsv', 'CSV')}
522
+ </ToggleGroupItem>
523
+ </ToggleGroup>
524
+ <Button
525
+ type="button"
526
+ variant="outline"
527
+ onClick={() => onOpenChange(false)}
528
+ disabled={isExporting}>
529
+ {t('ui.dataGrid.exportCancel', 'Cancel')}
530
+ </Button>
531
+ <Button
532
+ type="button"
533
+ onClick={() => void handleSubmit()}
534
+ disabled={selectedCount === 0 || isExporting}>
535
+ {isExporting ? <Loader2 className="size-4 animate-spin" /> : <Download className="size-4" />}
536
+ {t('ui.dataGrid.exportSubmit', 'Export')}
537
+ </Button>
538
+ </div>
539
+ </DialogFooter>
540
+ </DialogContent>
541
+ </Dialog>
542
+ );
543
+ }