@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,313 @@
1
+ 'use client';
2
+
3
+ // @ts-nocheck
4
+ /* eslint-disable */
5
+ import {
6
+ createContext, use, useMemo, type ReactNode
7
+ } from 'react';
8
+
9
+ import { type RestApiClient } from '@kivacore/api-client';
10
+ import {
11
+ createDateUtils,
12
+ createNumberUtils,
13
+ type DateUtils,
14
+ type NumberUtils,
15
+ type TenantPreferences
16
+ } from '@kivacore/app-utils';
17
+ import { useQuery } from '@tanstack/react-query';
18
+
19
+ import {
20
+ DateFormatProvider,
21
+ type DateFormatFn
22
+ } from '@kivacore/ui-pro-shared/hooks/kiva/use-date-format';
23
+ import {
24
+ NumberFormatProvider,
25
+ type NumberFormatFn,
26
+ type NumberFormatOptions
27
+ } from '@kivacore/ui-pro-shared/hooks/kiva/use-number-format';
28
+
29
+ /**
30
+ * `GET /v1/tenant/preferences` (after the api-client envelope unwrap)
31
+ * returns `{ preferences, tenant, product, enums }`, where every key
32
+ * inside `preferences` is camelCase: `dateFormat`, `dateTimeFormat`,
33
+ * `longDateFormat`, `decimalPrecision`, `thousandSeparator`,
34
+ * `decimalSeparator`, …
35
+ *
36
+ * `@kivacore/app-utils@0.11.x` factories (`createDateUtils`,
37
+ * `createNumberUtils`) read the inner preferences object keyed in
38
+ * snake_case (`date_format`, `date_time_format`, `decimal_precision`, …).
39
+ *
40
+ * Without normalization both factories silently fall back to their
41
+ * hard-coded defaults (`'Y-m-d'`, `'Y-m-d H:i:s'`, `en-US` locale) —
42
+ * that's the root cause of "tenant date/time format ignored" (issue #63).
43
+ *
44
+ * This helper unwraps the envelope and mirrors camelCase keys onto
45
+ * snake_case so the existing app-utils factories pick up the tenant's
46
+ * configured formats. Accepts either the full envelope or an already
47
+ * inner-shaped object.
48
+ */
49
+ export function normalizeTenantPreferences(envelope: unknown): TenantPreferences | null {
50
+ if (!envelope || typeof envelope !== 'object') return null;
51
+ const payload = 'success' in envelope && 'data' in envelope ? envelope.data : envelope;
52
+
53
+ if (!payload || typeof payload !== 'object') return null;
54
+ const candidate = 'preferences' in payload ? payload.preferences : payload;
55
+
56
+ if (!candidate || typeof candidate !== 'object') return null;
57
+ const raw = candidate as Record<string, unknown>;
58
+
59
+ return {
60
+ ...raw,
61
+ date_format: (raw.date_format ?? raw.dateFormat) as string | undefined,
62
+ date_time_format: (raw.date_time_format ?? raw.dateTimeFormat) as string | undefined,
63
+ long_date_format: (raw.long_date_format ?? raw.longDateFormat) as string | undefined,
64
+ thousand_separator: (raw.thousand_separator ?? raw.thousandSeparator) as string | undefined,
65
+ decimal_separator: (raw.decimal_separator ?? raw.decimalSeparator) as string | undefined,
66
+ decimal_precision: (raw.decimal_precision ?? raw.decimalPrecision) as number | undefined,
67
+ locale: raw.locale as string | undefined
68
+ };
69
+ }
70
+
71
+ export interface KivaTenantContextValue {
72
+ preferences: TenantPreferences | null;
73
+ dateUtils: DateUtils | null;
74
+ numberUtils: NumberUtils | null;
75
+ isLoading: boolean;
76
+ }
77
+
78
+ const KivaTenantContext = createContext<KivaTenantContextValue>({
79
+ preferences: null,
80
+ dateUtils: null,
81
+ numberUtils: null,
82
+ isLoading: false
83
+ });
84
+
85
+ /**
86
+ * Read the active tenant preferences and the matching `dateUtils` /
87
+ * `numberUtils` instances built from them. Returns `null` for each
88
+ * until `<KivaTenantProvider>` finishes loading.
89
+ *
90
+ * For grid / calendar / form cells you usually don't need this — they
91
+ * already read `useDateFormat()` / `useNumberFormat()` which
92
+ * `<KivaTenantProvider>` wires up automatically. Use this hook for
93
+ * ad-hoc formatting (chart axes, custom headers, side panels, …) or
94
+ * when you need the raw `TenantPreferences` object.
95
+ */
96
+ export function useKivaTenant(): KivaTenantContextValue {
97
+ return use(KivaTenantContext);
98
+ }
99
+
100
+ export interface KivaTenantProviderProps {
101
+ /** REST API client used to fetch `/v1/tenant/preferences`. */
102
+ client: RestApiClient | null | undefined;
103
+ /**
104
+ * Defer the fetch until your auth flow is ready. Defaults to
105
+ * `Boolean(client)`, so pass `false` (or leave `client` null) until
106
+ * the user is authenticated.
107
+ */
108
+ enabled?: boolean;
109
+ /** User's preferred timezone (IANA id). Forwarded to `createDateUtils`. */
110
+ userTimezone?: string;
111
+ /**
112
+ * Language for day and month names in tenant date formats (`F`, `M`,
113
+ * `l`, `D`), e.g. the UI language `tr`, `tr_TR` or `de-DE`. Forwarded to
114
+ * `createDateUtils`. Optional: without it names stay English.
115
+ */
116
+ locale?: string;
117
+ /** Cache freshness window for the preferences query. Default 30 min. */
118
+ staleTime?: number;
119
+ children: ReactNode;
120
+ }
121
+
122
+ const TENANT_QUERY_KEY = ['kiva', 'tenant', 'preferences'] as const;
123
+
124
+ const tenantQueryScopes = new WeakMap<RestApiClient, number>();
125
+ let nextTenantQueryScope = 0;
126
+
127
+ function tenantQueryScope(client: RestApiClient | null | undefined) {
128
+ if (!client) return null;
129
+ const scope = tenantQueryScopes.get(client);
130
+
131
+ if (scope !== undefined) return scope;
132
+ const next = nextTenantQueryScope++;
133
+
134
+ tenantQueryScopes.set(client, next);
135
+
136
+ return next;
137
+ }
138
+
139
+ const rawDateString: DateFormatFn = (value) => {
140
+ if (value == null) return '';
141
+ if (value instanceof Date) return value.toISOString();
142
+
143
+ return String(value);
144
+ };
145
+
146
+ const rawNumberString: NumberFormatFn = (value) => {
147
+ if (value == null || value === '') return '';
148
+
149
+ return String(value);
150
+ };
151
+
152
+ /**
153
+ * Single-line tenant integration for Kiva apps:
154
+ *
155
+ * ```tsx
156
+ * <KivaTenantProvider
157
+ * client={apiClient}
158
+ * enabled={authStatus === 'authenticated'}
159
+ * userTimezone={user?.timeZone?.id}
160
+ * locale={i18n.language}>
161
+ * <App />
162
+ * </KivaTenantProvider>
163
+ * ```
164
+ *
165
+ * Fetches `/v1/tenant/preferences`, normalizes the API envelope, builds
166
+ * `dateUtils` + `numberUtils`, and wires both `<DateFormatProvider>` and
167
+ * `<NumberFormatProvider>` so every UI component that reads
168
+ * `useDateFormat()` / `useNumberFormat()` — including
169
+ * `useKivaDataGrid`, calendar cells, side-filter chips, value
170
+ * renderers — picks up the tenant's configured formats automatically.
171
+ *
172
+ * Replaces the per-app `TenantProvider` + `useGridFormatters` +
173
+ * `<DateFormatProvider>` boilerplate every Kiva consumer used to
174
+ * write by hand. `useKivaTenant()` exposes the raw
175
+ * `{ preferences, dateUtils, numberUtils, isLoading }` for ad-hoc use.
176
+ */
177
+ export function KivaTenantProvider({
178
+ client,
179
+ enabled,
180
+ userTimezone,
181
+ locale,
182
+ staleTime = 30 * 60_000,
183
+ children
184
+ }: KivaTenantProviderProps) {
185
+ const query = useQuery<unknown>({
186
+ queryKey: [...TENANT_QUERY_KEY, tenantQueryScope(client)],
187
+ queryFn: async () => {
188
+ if (!client) throw new Error('[KivaTenantProvider] No API client');
189
+
190
+ return client.get('/v1/tenant/preferences', { includeEnums: false });
191
+ },
192
+ enabled: (enabled ?? Boolean(client)) && Boolean(client),
193
+ staleTime
194
+ });
195
+
196
+ const value = useMemo<KivaTenantContextValue>(() => {
197
+ const preferences = normalizeTenantPreferences(query.data);
198
+
199
+ if (!preferences) {
200
+ return {
201
+ preferences: null,
202
+ dateUtils: null,
203
+ numberUtils: null,
204
+ isLoading: query.isLoading
205
+ };
206
+ }
207
+
208
+ return {
209
+ preferences,
210
+ dateUtils: createDateUtils({ preferences, userTimezone, locale }),
211
+ numberUtils: createNumberUtils({ preferences }),
212
+ isLoading: query.isLoading
213
+ };
214
+ }, [
215
+ query.data,
216
+ query.isLoading,
217
+ userTimezone,
218
+ locale
219
+ ]);
220
+
221
+ const formatDate: DateFormatFn = useMemo(() => {
222
+ const utils = value.dateUtils;
223
+
224
+ return (raw) => {
225
+ if (raw == null) return '';
226
+ if (!utils) return rawDateString(raw);
227
+ try {
228
+ return utils.formatDate(raw as string | Date) ?? '';
229
+ } catch {
230
+ return rawDateString(raw);
231
+ }
232
+ };
233
+ }, [value.dateUtils]);
234
+
235
+ const formatDateTime: DateFormatFn = useMemo(() => {
236
+ const utils = value.dateUtils;
237
+
238
+ return (raw) => {
239
+ if (raw == null) return '';
240
+ if (!utils) return rawDateString(raw);
241
+ try {
242
+ return utils.formatDateTime(raw as string | Date) ?? '';
243
+ } catch {
244
+ return rawDateString(raw);
245
+ }
246
+ };
247
+ }, [value.dateUtils]);
248
+
249
+ /*
250
+ * No tenant-specific time format on the preferences payload (yet) —
251
+ * fall back to the locale's default `HH:mm` rendering so chips /
252
+ * cells aren't blank when components ask for time-only output.
253
+ */
254
+ const formatTime: DateFormatFn = useMemo(() => {
255
+ return (raw) => {
256
+ if (raw == null) return '';
257
+ try {
258
+ const date = raw instanceof Date ? raw : new Date(raw as string);
259
+
260
+ if (Number.isNaN(date.getTime())) return String(raw);
261
+
262
+ return date.toLocaleTimeString(undefined, { hour: '2-digit', minute: '2-digit' });
263
+ } catch {
264
+ return String(raw);
265
+ }
266
+ };
267
+ }, []);
268
+
269
+ const formatNumber: NumberFormatFn = useMemo(() => {
270
+ const utils = value.numberUtils;
271
+
272
+ return (raw, options?: NumberFormatOptions) => {
273
+ if (raw == null || raw === '') return '';
274
+ const numeric = typeof raw === 'number' ? raw : Number(raw);
275
+
276
+ if (Number.isNaN(numeric)) return String(raw);
277
+ if (!utils) return rawNumberString(numeric);
278
+
279
+ const variant = options?.variant ?? 'number';
280
+
281
+ const formatOpts
282
+ = options?.decimalPrecision !== undefined || options?.thousandSeparator !== undefined ? {
283
+ ...(options?.decimalPrecision !== undefined ? { decimalPrecision: options.decimalPrecision } : {}),
284
+ ...(options?.thousandSeparator !== undefined ? { thousandSeparator: options.thousandSeparator } : {})
285
+ } : undefined;
286
+
287
+ const base = utils.formatNumber(
288
+ variant === 'percent' ? numeric * 100 : numeric,
289
+ formatOpts
290
+ ) ?? String(numeric);
291
+
292
+ if (variant === 'currency' && options?.currency) {
293
+ return `${base} ${options.currency}`;
294
+ }
295
+ if (variant === 'percent') return `${base}%`;
296
+
297
+ return base;
298
+ };
299
+ }, [value.numberUtils]);
300
+
301
+ return (
302
+ <KivaTenantContext value={value}>
303
+ <DateFormatProvider
304
+ formatDate={formatDate}
305
+ formatDateTime={formatDateTime}
306
+ formatTime={formatTime}>
307
+ <NumberFormatProvider formatNumber={formatNumber}>
308
+ {children}
309
+ </NumberFormatProvider>
310
+ </DateFormatProvider>
311
+ </KivaTenantContext>
312
+ );
313
+ }
@@ -0,0 +1,191 @@
1
+ 'use client';
2
+
3
+ // @ts-nocheck
4
+ /* eslint-disable */
5
+ import { useEffect, useMemo, useRef } from 'react';
6
+
7
+ /**
8
+ * Where persisted view parameters are stored. `session` (the default) keeps
9
+ * tweaks for the lifetime of the browser tab; `local` survives across tabs and
10
+ * restarts.
11
+ */
12
+ export type ParameterPersistenceStorage = 'session' | 'local';
13
+
14
+ export interface ParameterPersistenceConfig {
15
+ /** Storage backend. Defaults to `'session'`. */
16
+ storage?: ParameterPersistenceStorage;
17
+ /**
18
+ * Override for the data-source segment of the storage key. When omitted the
19
+ * key is derived from the hook's `appSlug` / `dataSourceSlug`. Use this to
20
+ * keep two listings of the same data source from sharing persisted state.
21
+ */
22
+ key?: string;
23
+ }
24
+
25
+ /**
26
+ * Prop value accepted by the data-listing hooks. `true` is shorthand for
27
+ * `{ storage: 'session' }`; pass an object to opt into local storage or to
28
+ * override the key.
29
+ */
30
+ export type PersistStateProp = boolean | ParameterPersistenceConfig;
31
+
32
+ interface ResolvedPersistConfig {
33
+ storage: ParameterPersistenceStorage;
34
+ key: string;
35
+ }
36
+
37
+ /** Normalize the public `persistState` prop into a concrete config (or `null`). */
38
+ export function resolvePersistConfig(prop: PersistStateProp | undefined): ResolvedPersistConfig | null {
39
+ if (!prop) return null;
40
+ if (prop === true) return { storage: 'session', key: '' };
41
+
42
+ return { storage: prop.storage ?? 'session', key: prop.key ?? '' };
43
+ }
44
+
45
+ const STORAGE_PREFIX = 'docyrus:view-params';
46
+
47
+ /**
48
+ * Resolve the `Storage` backend for a persistence mode. Returns `null` when
49
+ * storage is unavailable (SSR, privacy modes, sandboxed iframes) so callers
50
+ * can degrade to a no-op. Shared with `useKivaDataViewSelect`'s
51
+ * active-view persistence so both layers resolve storage identically.
52
+ */
53
+ export function getPersistStore(storage: ParameterPersistenceStorage): Storage | null {
54
+ if (typeof window === 'undefined') return null;
55
+
56
+ try {
57
+ return storage === 'local' ? window.localStorage : window.sessionStorage;
58
+ } catch {
59
+ return null;
60
+ }
61
+ }
62
+
63
+ /**
64
+ * Build a namespaced storage key from its parts (falsy parts are dropped).
65
+ * Convention: `docyrus:view-params:<appSlug>:<dataSourceSlug>:<viewId>`.
66
+ */
67
+ export function buildPersistKey(parts: ReadonlyArray<string | undefined | null>): string {
68
+ return [STORAGE_PREFIX, ...parts.filter((part): part is string => Boolean(part))].join(':');
69
+ }
70
+
71
+ interface UseParameterPersistenceArgs<T> {
72
+ /** Whether persistence is active at all. */
73
+ enabled: boolean;
74
+ /** Storage backend. */
75
+ storage: ParameterPersistenceStorage;
76
+ /**
77
+ * Full storage key including the active view id. `null` means "not ready to
78
+ * persist yet" (e.g. the view id hasn't resolved) — restore and write are
79
+ * both suspended until it becomes a string.
80
+ */
81
+ storageKey: string | null;
82
+ /**
83
+ * Gate that defers restore/write until the surrounding view layer has
84
+ * settled (schema fields loaded, saved view applied). Restore for a given
85
+ * key runs exactly once, after `ready` is true.
86
+ */
87
+ ready: boolean;
88
+ /** Current serializable parameter snapshot. */
89
+ snapshot: T;
90
+ /** Apply a previously persisted snapshot back into the hook's state. */
91
+ onRestore: (restored: Partial<T>) => void;
92
+ /** Debounce for writes. Defaults to 300ms. */
93
+ debounceMs?: number;
94
+ }
95
+
96
+ /**
97
+ * Persist an arbitrary serializable parameter snapshot to session/local
98
+ * storage and restore it on mount / key change.
99
+ *
100
+ * Design notes:
101
+ * - Restore runs at most once per `storageKey`. Switching views (a new key)
102
+ * triggers a fresh restore for that view's persisted overlay.
103
+ * - Writes are skipped until the key's restore has run, so the initial
104
+ * (pre-restore) snapshot never clobbers stored state.
105
+ * - The caller must declare this hook AFTER any "apply saved view" effect so
106
+ * the restore's `onRestore` lands on top of the view preset within the same
107
+ * commit.
108
+ */
109
+ export function useParameterPersistence<T extends object>({
110
+ enabled,
111
+ storage,
112
+ storageKey,
113
+ ready,
114
+ snapshot,
115
+ onRestore,
116
+ debounceMs = 300
117
+ }: UseParameterPersistenceArgs<T>): void {
118
+ const onRestoreRef = useRef(onRestore);
119
+
120
+ onRestoreRef.current = onRestore;
121
+
122
+ /*
123
+ * The key we have already restored for — guards both the one-shot restore
124
+ * and the "don't write before restore" rule.
125
+ */
126
+ const restoredKeyRef = useRef<string | null>(null);
127
+
128
+ useEffect(() => {
129
+ if (!enabled || !ready || !storageKey) return;
130
+ if (restoredKeyRef.current === storageKey) return;
131
+
132
+ restoredKeyRef.current = storageKey;
133
+
134
+ const store = getPersistStore(storage);
135
+
136
+ if (!store) return;
137
+
138
+ try {
139
+ const raw = store.getItem(storageKey);
140
+
141
+ if (!raw) return;
142
+
143
+ const parsed = JSON.parse(raw) as Partial<T> | null;
144
+
145
+ if (parsed && typeof parsed === 'object') onRestoreRef.current(parsed);
146
+ } catch {
147
+ }
148
+ }, [
149
+ enabled,
150
+ ready,
151
+ storage,
152
+ storageKey
153
+ ]);
154
+
155
+ const serialized = useMemo(() => {
156
+ try {
157
+ return JSON.stringify(snapshot);
158
+ } catch {
159
+ return null;
160
+ }
161
+ }, [snapshot]);
162
+
163
+ useEffect(() => {
164
+ if (!enabled || !ready || !storageKey || serialized == null) return;
165
+ /*
166
+ * Don't persist until we've restored this key — otherwise the transient
167
+ * default snapshot overwrites the user's saved overlay before it loads.
168
+ */
169
+ if (restoredKeyRef.current !== storageKey) return;
170
+
171
+ const store = getPersistStore(storage);
172
+
173
+ if (!store) return;
174
+
175
+ const handle = window.setTimeout(() => {
176
+ try {
177
+ store.setItem(storageKey, serialized);
178
+ } catch {
179
+ }
180
+ }, debounceMs);
181
+
182
+ return () => window.clearTimeout(handle);
183
+ }, [
184
+ enabled,
185
+ ready,
186
+ storage,
187
+ storageKey,
188
+ serialized,
189
+ debounceMs
190
+ ]);
191
+ }