@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,266 @@
1
+ 'use client';
2
+
3
+ // @ts-nocheck
4
+ /* eslint-disable */
5
+ import {
6
+ useCallback, useEffect, useMemo, useState
7
+ } from 'react';
8
+
9
+ import { AlertTriangle, Loader2 } from 'lucide-react';
10
+
11
+ import {
12
+ AlertDialog,
13
+ AlertDialogCancel,
14
+ AlertDialogContent,
15
+ AlertDialogDescription,
16
+ AlertDialogFooter,
17
+ AlertDialogHeader,
18
+ AlertDialogTitle
19
+ } from '@kivacore/ui-pro-shared/components/alert-dialog';
20
+ import { Button } from '@kivacore/ui-pro-shared/components/button';
21
+ import { Label } from '@kivacore/ui-pro-shared/components/label';
22
+ import { RadioGroup, RadioGroupItem } from '@kivacore/ui-pro-shared/components/radio-group';
23
+ import {
24
+ Table,
25
+ TableBody,
26
+ TableCell,
27
+ TableHead,
28
+ TableHeader,
29
+ TableRow
30
+ } from '@kivacore/ui-pro-shared/components/table';
31
+ import { cn } from '@kivacore/ui-pro-shared/lib/utils';
32
+
33
+ import {
34
+ type ChildAction,
35
+ type DataSourceRelation,
36
+ type RecordDeleteConfirmDialogProps
37
+ } from './types';
38
+
39
+ export function RecordDeleteConfirmDialog({
40
+ open,
41
+ onOpenChange,
42
+ recordCount,
43
+ relations,
44
+ isLoadingRelations,
45
+ onConfirm,
46
+ isPending
47
+ }: RecordDeleteConfirmDialogProps) {
48
+ const childRelations = useMemo(
49
+ () => relations ?? [],
50
+ [relations]
51
+ );
52
+
53
+ const [childActions, setChildActions] = useState<
54
+ Record<string, ChildAction>
55
+ >({});
56
+
57
+ useEffect(() => {
58
+ if (!open) return;
59
+ const initial: Record<string, ChildAction> = {};
60
+
61
+ for (const rel of childRelations) {
62
+ initial[rel.dataSourceId] = 'keep';
63
+ }
64
+ queueMicrotask(() => setChildActions(initial));
65
+ }, [open, childRelations]);
66
+
67
+ const allAction = useMemo<ChildAction | undefined>(() => {
68
+ if (childRelations.length === 0) return undefined;
69
+ const values = Object.values(childActions);
70
+
71
+ if (values.length === 0) return 'keep';
72
+ const first = values[0];
73
+
74
+ return values.every(v => v === first) ? first : undefined;
75
+ }, [childActions, childRelations]);
76
+
77
+ const onAllActionChange = useCallback((value: string) => {
78
+ const action = value as ChildAction;
79
+
80
+ setChildActions((prev) => {
81
+ const next = { ...prev };
82
+
83
+ for (const key of Object.keys(next)) {
84
+ next[key] = action;
85
+ }
86
+
87
+ return next;
88
+ });
89
+ }, []);
90
+
91
+ const onRelationActionChange = useCallback(
92
+ (dataSourceId: string, value: string) => {
93
+ setChildActions(prev => ({
94
+ ...prev,
95
+ [dataSourceId]: value as ChildAction
96
+ }));
97
+ },
98
+ []
99
+ );
100
+
101
+ const onConfirmClick = useCallback(() => {
102
+ onConfirm(childActions);
103
+ }, [onConfirm, childActions]);
104
+
105
+ const count = recordCount;
106
+ const hasChildRelations = childRelations.length > 0;
107
+
108
+ return (
109
+ <AlertDialog open={open} onOpenChange={onOpenChange}>
110
+ <AlertDialogContent
111
+ data-grid-popover
112
+ className={cn(
113
+ 'w-[min(90vw,960px)]',
114
+ 'data-[size=default]:max-w-none sm:data-[size=default]:max-w-none',
115
+ hasChildRelations ? 'sm:max-w-5xl' : 'sm:max-w-xl'
116
+ )}>
117
+ <AlertDialogHeader>
118
+ <AlertDialogTitle className="flex items-center gap-2">
119
+ <AlertTriangle className="size-5 text-amber-500" />
120
+ Confirm Deletion
121
+ </AlertDialogTitle>
122
+ <AlertDialogDescription>
123
+ Are you sure you want to delete{' '}
124
+ {count === 1 ? 'this item' : `these ${count} items`}? This action
125
+ cannot be undone. Please confirm your choice.
126
+ </AlertDialogDescription>
127
+ </AlertDialogHeader>
128
+
129
+ {isLoadingRelations ? (
130
+ <div className="flex items-center justify-center py-4">
131
+ <Loader2 className="size-5 animate-spin text-muted-foreground" />
132
+ </div>
133
+ ) : hasChildRelations ? (
134
+ <ChildRelationsForm
135
+ childRelations={childRelations}
136
+ childActions={childActions}
137
+ allAction={allAction}
138
+ onAllActionChange={onAllActionChange}
139
+ onRelationActionChange={onRelationActionChange} />
140
+ ) : null}
141
+
142
+ <AlertDialogFooter>
143
+ <AlertDialogCancel disabled={isPending}>Cancel</AlertDialogCancel>
144
+ <Button
145
+ variant="destructive"
146
+ disabled={isPending || isLoadingRelations}
147
+ onClick={onConfirmClick}>
148
+ {isPending ? 'Deleting...' : 'Delete'}
149
+ </Button>
150
+ </AlertDialogFooter>
151
+ </AlertDialogContent>
152
+ </AlertDialog>
153
+ );
154
+ }
155
+
156
+ function ChildRelationsForm({
157
+ childRelations,
158
+ childActions,
159
+ allAction,
160
+ onAllActionChange,
161
+ onRelationActionChange
162
+ }: {
163
+ childRelations: Array<DataSourceRelation>;
164
+ childActions: Record<string, ChildAction>;
165
+ allAction: ChildAction | undefined;
166
+ onAllActionChange: (value: string) => void;
167
+ onRelationActionChange: (dataSourceId: string, value: string) => void;
168
+ }) {
169
+ return (
170
+ <div className="space-y-3">
171
+ <p className="text-sm text-muted-foreground">
172
+ Please choose desired action for related records.
173
+ </p>
174
+
175
+ <div className="space-y-2">
176
+ <p className="flex items-center gap-1.5 text-sm font-medium">
177
+ <span className="text-muted-foreground">&#x2637;</span>
178
+ <span>For All Tables</span>
179
+ </p>
180
+ <RadioGroup
181
+ value={allAction ?? ''}
182
+ onValueChange={onAllActionChange}
183
+ className="flex items-center gap-6">
184
+ <div className="flex items-center gap-2">
185
+ <RadioGroupItem value="keep" id="all-keep" />
186
+ <Label htmlFor="all-keep" className="text-sm">
187
+ Remove Relation And Keep
188
+ </Label>
189
+ </div>
190
+ <div className="flex items-center gap-2">
191
+ <RadioGroupItem value="delete" id="all-delete" />
192
+ <Label htmlFor="all-delete" className="text-sm">
193
+ Delete Related Records
194
+ </Label>
195
+ </div>
196
+ </RadioGroup>
197
+ </div>
198
+
199
+ <Table>
200
+ <TableHeader>
201
+ <TableRow>
202
+ <TableHead className="w-45">Related data source</TableHead>
203
+ <TableHead>Action</TableHead>
204
+ </TableRow>
205
+ </TableHeader>
206
+ <TableBody>
207
+ {childRelations.map(rel => (
208
+ <ChildRelationRow
209
+ key={rel.dataSourceId}
210
+ relation={rel}
211
+ action={childActions[rel.dataSourceId] ?? 'keep'}
212
+ onActionChange={onRelationActionChange} />
213
+ ))}
214
+ </TableBody>
215
+ </Table>
216
+ </div>
217
+ );
218
+ }
219
+
220
+ function ChildRelationRow({
221
+ relation,
222
+ action,
223
+ onActionChange
224
+ }: {
225
+ relation: DataSourceRelation;
226
+ action: ChildAction;
227
+ onActionChange: (dataSourceId: string, value: string) => void;
228
+ }) {
229
+ const keepId = `${relation.dataSourceId}-keep`;
230
+ const deleteId = `${relation.dataSourceId}-delete`;
231
+
232
+ const onChange = useCallback(
233
+ (value: string) => {
234
+ onActionChange(relation.dataSourceId, value);
235
+ },
236
+ [onActionChange, relation.dataSourceId]
237
+ );
238
+
239
+ return (
240
+ <TableRow>
241
+ <TableCell className="font-medium">
242
+ {relation.dataSourceName}
243
+ <span className="text-destructive">*</span>
244
+ </TableCell>
245
+ <TableCell>
246
+ <RadioGroup
247
+ value={action}
248
+ onValueChange={onChange}
249
+ className="flex items-center gap-6">
250
+ <div className="flex items-center gap-2">
251
+ <RadioGroupItem value="keep" id={keepId} />
252
+ <Label htmlFor={keepId} className="text-sm">
253
+ Remove Relation And Keep
254
+ </Label>
255
+ </div>
256
+ <div className="flex items-center gap-2">
257
+ <RadioGroupItem value="delete" id={deleteId} />
258
+ <Label htmlFor={deleteId} className="text-sm">
259
+ Delete Related Records
260
+ </Label>
261
+ </div>
262
+ </RadioGroup>
263
+ </TableCell>
264
+ </TableRow>
265
+ );
266
+ }
@@ -0,0 +1,25 @@
1
+ // @ts-nocheck
2
+ /* eslint-disable */
3
+ export interface DataSourceRelation {
4
+ dataSourceId: string;
5
+ dataSourceName: string;
6
+ dataSourceSlug: string;
7
+ appSlug: string;
8
+ dataSourceType: string;
9
+ fieldId: string;
10
+ fieldSlug: string;
11
+ fieldName: string;
12
+ fieldType: string;
13
+ }
14
+
15
+ export type ChildAction = 'keep' | 'delete';
16
+
17
+ export interface RecordDeleteConfirmDialogProps {
18
+ open: boolean;
19
+ onOpenChange: (open: boolean) => void;
20
+ recordCount: number;
21
+ relations?: Array<DataSourceRelation>;
22
+ isLoadingRelations?: boolean;
23
+ onConfirm: (childActions: Record<string, ChildAction>) => void | Promise<void>;
24
+ isPending?: boolean;
25
+ }
@@ -0,0 +1,112 @@
1
+ // @ts-nocheck
2
+ /* eslint-disable */
3
+ import { useQuery } from '@tanstack/react-query';
4
+
5
+ /**
6
+ * Whether a data source lets people set its record number ("No",
7
+ * `autonumber_id`) by hand. The setting is a column of the data source row
8
+ * (`autonumber_editable`), so the product tenant can switch it on once for a
9
+ * shared table and every tenant using the table gets it. The API ignores a
10
+ * sent number unless it is on and the table takes a number set by hand (a
11
+ * `GENERATED ALWAYS` No never does).
12
+ *
13
+ * Grids and forms read the outcome from the data source metadata they already
14
+ * hold (`isAutonumberEditable`). The stored setting (`fetchAutonumberEditable`)
15
+ * is for the screens that change it.
16
+ */
17
+
18
+ export const AUTONUMBER_FIELD_SLUG = 'autonumber_id';
19
+
20
+ interface AutonumberSettingClient {
21
+ get<T = unknown>(url: string, params?: Record<string, unknown>): Promise<T>;
22
+ }
23
+
24
+ /**
25
+ * What the data source metadata says: the API marks the No field
26
+ * `options.editable: true` when the No can be set by hand. Anything else, an
27
+ * API that does not send the flag included, keeps the number read-only.
28
+ */
29
+ export function isAutonumberEditable(dataSource: unknown): boolean {
30
+ const fields = (dataSource as { fields?: unknown } | null | undefined)?.fields;
31
+ const field = Array.isArray(fields) ? fields.find(entry => (entry as { slug?: unknown } | null)?.slug === AUTONUMBER_FIELD_SLUG) as { options?: unknown } | undefined : undefined;
32
+
33
+ return parseOptions(field?.options)?.editable === true;
34
+ }
35
+
36
+ export function autonumberEditableQueryKey(dataSourceId: string | null | undefined) {
37
+ return ['kiva', 'autonumberEditable', dataSourceId ?? null] as const;
38
+ }
39
+
40
+ /** Read the way the API reads it: `true`, `1` or the string forms are on; a missing value, or anything else, is off. */
41
+ export function isAutonumberEditableOption(value: unknown): boolean {
42
+ return value === true || value === 1 || (typeof value === 'string' && ['true', '1'].includes(value.trim().toLowerCase()));
43
+ }
44
+
45
+ /*
46
+ * A session that may not read the setting never will: an API (OAuth2) token
47
+ * without an Architect scope gets a 403 from the dev data source endpoint. The
48
+ * setting then counts as off and that client is not asked again, so a page of
49
+ * grids and forms makes one failed request instead of one per mount.
50
+ */
51
+ const unreadableBy = new WeakSet<AutonumberSettingClient>();
52
+
53
+ /** The stored setting, from the data source's own row. A data source that cannot be found counts as off. */
54
+ export async function fetchAutonumberEditable(client: AutonumberSettingClient, dataSourceId: string): Promise<boolean> {
55
+ if (unreadableBy.has(client)) return false;
56
+
57
+ let response: unknown;
58
+
59
+ try {
60
+ response = await client.get<unknown>(`/v1/dev/data-sources/${dataSourceId}`);
61
+ } catch (error) {
62
+ const status = (error as { status?: unknown } | null)?.status;
63
+
64
+ if (status === 404) return false;
65
+ if (status !== 403) throw error;
66
+ unreadableBy.add(client);
67
+
68
+ return false;
69
+ }
70
+ const dataSource = response && typeof response === 'object' && 'data' in response ? (response as { data: unknown }).data : response;
71
+
72
+ return isAutonumberEditableOption((dataSource as { autonumber_editable?: unknown } | null)?.autonumber_editable);
73
+ }
74
+
75
+ function parseOptions(value: unknown): Record<string, unknown> | null {
76
+ if (typeof value === 'string') {
77
+ try {
78
+ return parseOptions(JSON.parse(value));
79
+ } catch {
80
+ return null;
81
+ }
82
+ }
83
+
84
+ return value && typeof value === 'object' && !Array.isArray(value) ? value as Record<string, unknown> : null;
85
+ }
86
+
87
+ /**
88
+ * Reads the setting for one data source. While it loads, and when it cannot be
89
+ * read, the number stays read-only, as it was before the setting existed.
90
+ */
91
+ export function useAutonumberEditable({
92
+ client,
93
+ dataSourceId,
94
+ enabled = true,
95
+ staleTime = 5 * 60_000
96
+ }: {
97
+ client: AutonumberSettingClient | null | undefined;
98
+ dataSourceId: string | null | undefined;
99
+ enabled?: boolean;
100
+ /** How long an answer is reused; 0 asks again on every mount, for screens where the setting may just have changed elsewhere. */
101
+ staleTime?: number;
102
+ }): boolean {
103
+ const query = useQuery({
104
+ queryKey: autonumberEditableQueryKey(dataSourceId),
105
+ queryFn: () => fetchAutonumberEditable(client as AutonumberSettingClient, dataSourceId as string),
106
+ enabled: enabled && Boolean(client) && Boolean(dataSourceId),
107
+ staleTime,
108
+ retry: false
109
+ });
110
+
111
+ return query.data === true;
112
+ }