@kivacore/ui-pro-shared 0.0.9 → 0.0.11
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/package.json +167 -1
- package/src/.kiva/config.json +27 -0
- package/src/components/avatar-group.tsx +222 -0
- package/src/components/kiva/bulk-update-dialog/bulk-update-dialog.tsx +240 -0
- package/src/components/kiva/bulk-update-dialog/index.tsx +6 -0
- package/src/components/kiva/bulk-update-dialog/types.ts +33 -0
- package/src/components/kiva/calendar/animations.ts +40 -0
- package/src/components/kiva/calendar/calendar-body.tsx +106 -0
- package/src/components/kiva/calendar/calendar-sidebar.tsx +103 -0
- package/src/components/kiva/calendar/calendar.tsx +243 -0
- package/src/components/kiva/calendar/constants.ts +12 -0
- package/src/components/kiva/calendar/contexts/calendar-context.tsx +696 -0
- package/src/components/kiva/calendar/contexts/dnd-context.tsx +173 -0
- package/src/components/kiva/calendar/dialogs/add-edit-event-dialog.tsx +287 -0
- package/src/components/kiva/calendar/dialogs/delete-event-dialog.tsx +73 -0
- package/src/components/kiva/calendar/dialogs/event-details-dialog.tsx +163 -0
- package/src/components/kiva/calendar/dialogs/events-list-dialog.tsx +122 -0
- package/src/components/kiva/calendar/dnd/draggable-event.tsx +66 -0
- package/src/components/kiva/calendar/dnd/droppable-area.tsx +53 -0
- package/src/components/kiva/calendar/dnd/resizable-event.tsx +190 -0
- package/src/components/kiva/calendar/header/calendar-header.tsx +84 -0
- package/src/components/kiva/calendar/header/date-navigator.tsx +188 -0
- package/src/components/kiva/calendar/header/filter.tsx +80 -0
- package/src/components/kiva/calendar/header/today-button.tsx +49 -0
- package/src/components/kiva/calendar/header/user-select.tsx +111 -0
- package/src/components/kiva/calendar/header/view-tabs.tsx +144 -0
- package/src/components/kiva/calendar/helpers.ts +617 -0
- package/src/components/kiva/calendar/hooks/use-disclosure.ts +19 -0
- package/src/components/kiva/calendar/hooks/use-local-storage.ts +40 -0
- package/src/components/kiva/calendar/hooks/use-media-query.ts +25 -0
- package/src/components/kiva/calendar/hooks.ts +5 -0
- package/src/components/kiva/calendar/index.tsx +27 -0
- package/src/components/kiva/calendar/interfaces.ts +35 -0
- package/src/components/kiva/calendar/schemas.ts +20 -0
- package/src/components/kiva/calendar/settings/settings.tsx +116 -0
- package/src/components/kiva/calendar/skeletons/calendar-header-skeleton.tsx +27 -0
- package/src/components/kiva/calendar/skeletons/calendar-skeleton.tsx +19 -0
- package/src/components/kiva/calendar/skeletons/day-view-skeleton.tsx +40 -0
- package/src/components/kiva/calendar/skeletons/month-view-skeleton.tsx +36 -0
- package/src/components/kiva/calendar/skeletons/week-view-skeleton.tsx +48 -0
- package/src/components/kiva/calendar/skeletons/year-view-skeleton.tsx +42 -0
- package/src/components/kiva/calendar/types.ts +25 -0
- package/src/components/kiva/calendar/views/agenda-view/agenda-events.tsx +150 -0
- package/src/components/kiva/calendar/views/month-view/calendar-month-view.tsx +82 -0
- package/src/components/kiva/calendar/views/month-view/day-cell.tsx +218 -0
- package/src/components/kiva/calendar/views/month-view/event-bullet.tsx +46 -0
- package/src/components/kiva/calendar/views/month-view/month-event-badge.tsx +154 -0
- package/src/components/kiva/calendar/views/timeline/calendar-timeline-view.tsx +100 -0
- package/src/components/kiva/calendar/views/timeline/lib/timeline-layout.ts +130 -0
- package/src/components/kiva/calendar/views/timeline/lib/timeline-model.ts +504 -0
- package/src/components/kiva/calendar/views/timeline/timeline-event-bar.tsx +68 -0
- package/src/components/kiva/calendar/views/timeline/timeline-grid-header.tsx +125 -0
- package/src/components/kiva/calendar/views/timeline/timeline-row.tsx +159 -0
- package/src/components/kiva/calendar/views/timeline/timeline-section.tsx +61 -0
- package/src/components/kiva/calendar/views/timeline/use-timeline-sections.ts +278 -0
- package/src/components/kiva/calendar/views/week-and-day-view/calendar-day-view.tsx +277 -0
- package/src/components/kiva/calendar/views/week-and-day-view/calendar-time-line.tsx +47 -0
- package/src/components/kiva/calendar/views/week-and-day-view/calendar-week-view.tsx +229 -0
- package/src/components/kiva/calendar/views/week-and-day-view/day-view-multi-day-events-row.tsx +79 -0
- package/src/components/kiva/calendar/views/week-and-day-view/event-block.tsx +116 -0
- package/src/components/kiva/calendar/views/week-and-day-view/render-grouped-events.tsx +54 -0
- package/src/components/kiva/calendar/views/week-and-day-view/week-view-multi-day-events-row.tsx +159 -0
- package/src/components/kiva/calendar/views/year-view/calendar-year-view.tsx +159 -0
- package/src/components/kiva/data-table-side-filters/components/side-filter-active-chips.tsx +137 -0
- package/src/components/kiva/data-table-side-filters/components/side-filter-clear.tsx +34 -0
- package/src/components/kiva/data-table-side-filters/components/side-filter-search.tsx +67 -0
- package/src/components/kiva/data-table-side-filters/components/side-filter-section.tsx +77 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-async-options.tsx +354 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-boolean.tsx +66 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-checkbox-list.tsx +137 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-date-range.tsx +204 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-numeric-range.tsx +162 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-options-dropdown.tsx +191 -0
- package/src/components/kiva/data-table-side-filters/controllers/side-filter-text.tsx +53 -0
- package/src/components/kiva/data-table-side-filters/core/query-translator.ts +367 -0
- package/src/components/kiva/data-table-side-filters/core/types.ts +81 -0
- package/src/components/kiva/data-table-side-filters/data-table-side-filters.tsx +483 -0
- package/src/components/kiva/data-table-side-filters/hooks/use-data-table-side-filters.tsx +160 -0
- package/src/components/kiva/data-table-side-filters/hooks/use-show-more.tsx +62 -0
- package/src/components/kiva/data-table-side-filters/index.tsx +44 -0
- package/src/components/kiva/data-table-side-filters/lib/render-mode.ts +62 -0
- package/src/components/kiva/date-time-picker.tsx +163 -0
- package/src/components/kiva/editable-record-detail.tsx +809 -0
- package/src/components/kiva/enum-option-editor/enum-option-editor.tsx +891 -0
- package/src/components/kiva/enum-option-editor/index.tsx +26 -0
- package/src/components/kiva/enum-option-editor/kiva-enum-option-editor.tsx +320 -0
- package/src/components/kiva/enum-option-editor/types.ts +90 -0
- package/src/components/kiva/kanban.tsx +1376 -0
- package/src/components/kiva/kiva-data-export-menu.tsx +543 -0
- package/src/components/kiva/map.tsx +1631 -0
- package/src/components/kiva/pivot-calendar/components/group-sidebar.tsx +95 -0
- package/src/components/kiva/pivot-calendar/components/pivot-calendar-toolbar.tsx +113 -0
- package/src/components/kiva/pivot-calendar/components/stats-cell.tsx +126 -0
- package/src/components/kiva/pivot-calendar/helpers.ts +374 -0
- package/src/components/kiva/pivot-calendar/hooks/use-pivot-calendar.ts +222 -0
- package/src/components/kiva/pivot-calendar/index.tsx +32 -0
- package/src/components/kiva/pivot-calendar/pivot-calendar.tsx +107 -0
- package/src/components/kiva/pivot-calendar/types.ts +154 -0
- package/src/components/kiva/pivot-calendar/views/days-of-month-view.tsx +37 -0
- package/src/components/kiva/pivot-calendar/views/days-of-week-view.tsx +37 -0
- package/src/components/kiva/pivot-calendar/views/month-calendar-view.tsx +154 -0
- package/src/components/kiva/pivot-calendar/views/months-of-year-view.tsx +40 -0
- package/src/components/kiva/pivot-calendar/views/pivot-grid-view.tsx +251 -0
- package/src/components/kiva/pivot-filter/index.tsx +9 -0
- package/src/components/kiva/pivot-filter/pivot-filter-item.tsx +159 -0
- package/src/components/kiva/pivot-filter/pivot-filter-settings.tsx +161 -0
- package/src/components/kiva/pivot-filter/pivot-filter.tsx +187 -0
- package/src/components/kiva/pivot-filter/types.ts +82 -0
- package/src/components/kiva/pivot-grid/hooks/use-pivot-grid.ts +683 -0
- package/src/components/kiva/pivot-grid/index.tsx +23 -0
- package/src/components/kiva/pivot-grid/lib/pivot-grid-color-rules.ts +98 -0
- package/src/components/kiva/pivot-grid/lib/pivot-grid-export.ts +265 -0
- package/src/components/kiva/pivot-grid/lib/pivot-grid-model.ts +1110 -0
- package/src/components/kiva/pivot-grid/pivot-grid-demo-data.ts +200 -0
- package/src/components/kiva/pivot-grid/pivot-grid-export-menu.tsx +66 -0
- package/src/components/kiva/pivot-grid/pivot-grid-toolbar.tsx +85 -0
- package/src/components/kiva/pivot-grid/pivot-grid.tsx +601 -0
- package/src/components/kiva/pivot-grid/types.ts +195 -0
- package/src/components/kiva/place-autocomplete.tsx +484 -0
- package/src/components/kiva/record-delete-confirm-dialog/index.tsx +10 -0
- package/src/components/kiva/record-delete-confirm-dialog/record-delete-confirm-dialog.tsx +266 -0
- package/src/components/kiva/record-delete-confirm-dialog/types.ts +25 -0
- package/src/hooks/kiva/_internal/autonumber-setting.ts +112 -0
- package/src/hooks/kiva/_internal/form-layout-to-view.ts +665 -0
- package/src/hooks/kiva/_internal/kiva-hook-utils.ts +143 -0
- package/src/hooks/kiva/_internal/use-computed-field-evaluator.tsx +310 -0
- package/src/hooks/kiva/_internal/use-field-actions-evaluator.tsx +398 -0
- package/src/hooks/kiva/_internal/use-form-actions-evaluator.tsx +363 -0
- package/src/hooks/kiva/_internal/use-form-view-engine.tsx +2200 -0
- package/src/hooks/kiva/_internal/use-kiva-form-view-fields.tsx +190 -0
- package/src/hooks/kiva/_internal/use-kiva-kanban-cards.tsx +311 -0
- package/src/hooks/kiva/use-data-export.tsx +265 -0
- package/src/hooks/kiva/use-kiva-calendar.tsx +992 -0
- package/src/hooks/kiva/use-kiva-data-export.tsx +173 -0
- package/src/hooks/kiva/use-kiva-data-grid.tsx +4750 -0
- package/src/hooks/kiva/use-kiva-data-view-select.tsx +1273 -0
- package/src/hooks/kiva/use-kiva-form-view.tsx +1581 -0
- package/src/hooks/kiva/use-kiva-inventory.tsx +184 -0
- package/src/hooks/kiva/use-kiva-kanban.tsx +1750 -0
- package/src/hooks/kiva/use-kiva-map-view.tsx +2255 -0
- package/src/hooks/kiva/use-kiva-pivot-calendar.tsx +759 -0
- package/src/hooks/kiva/use-kiva-pivot-filter.tsx +1523 -0
- package/src/hooks/kiva/use-kiva-pivot-grid.tsx +543 -0
- package/src/hooks/kiva/use-kiva-tenant.tsx +313 -0
- package/src/hooks/kiva/use-parameter-persistence.ts +191 -0
|
@@ -0,0 +1,1273 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
// @ts-nocheck
|
|
4
|
+
/* eslint-disable */
|
|
5
|
+
import {
|
|
6
|
+
useCallback, useEffect, useMemo, useRef, useState
|
|
7
|
+
} from 'react';
|
|
8
|
+
|
|
9
|
+
import { type RestApiClient } from '@kivacore/api-client';
|
|
10
|
+
import {
|
|
11
|
+
createDataFormClient,
|
|
12
|
+
createDataSourceClient,
|
|
13
|
+
createDataViewClient,
|
|
14
|
+
type CreateDataViewBody,
|
|
15
|
+
type DataForm,
|
|
16
|
+
type DataSource,
|
|
17
|
+
type DataSourceField,
|
|
18
|
+
type DataView,
|
|
19
|
+
type UpdateDataViewBody
|
|
20
|
+
} from '@kivacore/app-utils';
|
|
21
|
+
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
|
22
|
+
import { type FullField } from 'react-querybuilder';
|
|
23
|
+
|
|
24
|
+
import { getSharedKivaInventory } from '@kivacore/ui-pro-shared/hooks/kiva/use-kiva-inventory';
|
|
25
|
+
import { getPersistStore, type ParameterPersistenceStorage } from '@kivacore/ui-pro-shared/hooks/kiva/use-parameter-persistence';
|
|
26
|
+
import { type DataGridViewSelectProps } from '@kivacore/ui-pro-shared/components/kiva/data-grid-view-select';
|
|
27
|
+
import { type SavedDataGridView } from '@kivacore/ui-pro-shared/components/kiva/data-grid/types';
|
|
28
|
+
import { normalizeRowHeight } from '@kivacore/ui-pro-shared/components/kiva/data-grid/lib/data-grid';
|
|
29
|
+
import {
|
|
30
|
+
FILTER_GROUP_INPUT_TYPE,
|
|
31
|
+
FILTER_GROUP_VALUE_EDITOR_TYPE,
|
|
32
|
+
getFilterGroupForFieldType,
|
|
33
|
+
getOperatorsForGroup,
|
|
34
|
+
resolveValueEditorType,
|
|
35
|
+
type FilterGroup
|
|
36
|
+
} from '@kivacore/ui-pro-shared/components/kiva/query-builder/query-operators';
|
|
37
|
+
|
|
38
|
+
export interface UseKivaDataViewSelectOptions {
|
|
39
|
+
client: RestApiClient;
|
|
40
|
+
appSlug: string;
|
|
41
|
+
dataSourceSlug: string;
|
|
42
|
+
appId?: string;
|
|
43
|
+
overrideFields?: Array<FullField>;
|
|
44
|
+
/**
|
|
45
|
+
* Pre-resolved data-source schema. When provided, the hook skips its
|
|
46
|
+
* internal `getBySlug` schema fetch entirely and reads fields / metadata
|
|
47
|
+
* from this object instead — useful for previews or any surface that already
|
|
48
|
+
* holds the schema in memory and must not hit the DB. Pair with
|
|
49
|
+
* `enabled: false` (stops the relation-option fetch too) and
|
|
50
|
+
* `enableDataViews: false` for a fully fetch-free hook. Unlike
|
|
51
|
+
* `overrideFields` (which only replaces the derived field list), this also
|
|
52
|
+
* supplies the `dataSource` object — id, slug, and metadata — to every
|
|
53
|
+
* downstream consumer (data grid, table, kanban, gallery, map view).
|
|
54
|
+
*/
|
|
55
|
+
dataSource?: DataSource | null;
|
|
56
|
+
mapField?: (field: DataSourceField, defaultMapped: FullField) => FullField | null;
|
|
57
|
+
staleTime?: number;
|
|
58
|
+
enabled?: boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Fetch and manage saved data views (the `/views` endpoint). Default `true`.
|
|
61
|
+
* Set `false` for data sources without a view-configuration backend (e.g.
|
|
62
|
+
* core/tenant system data sources) so the hook never calls `/views`; the
|
|
63
|
+
* tab strip then shows only `systemViews` (if any) and the table renders all
|
|
64
|
+
* fields with default columns.
|
|
65
|
+
*/
|
|
66
|
+
enableDataViews?: boolean;
|
|
67
|
+
/**
|
|
68
|
+
* `expand` query param sent with the data-source schema fetch. Defaults to
|
|
69
|
+
* `'enums'` (so select/status fields carry their option metadata). Pass
|
|
70
|
+
* `false` (or `''`) to omit `expand` entirely for backends that don't
|
|
71
|
+
* support it. When `enableForms` is on, `forms` is appended automatically.
|
|
72
|
+
*/
|
|
73
|
+
dataSourceExpand?: string | false;
|
|
74
|
+
/**
|
|
75
|
+
* Fetch the data source's saved forms (appends `forms` to the schema
|
|
76
|
+
* `expand`) and surface them as `forms` on the result + `gridViewSelectProps`
|
|
77
|
+
* so the view editor can bind a form to each view. Default `true`. Has no
|
|
78
|
+
* effect when `dataSourceExpand` is `false` / `''` (no `expand` at all).
|
|
79
|
+
*/
|
|
80
|
+
enableForms?: boolean;
|
|
81
|
+
persistActiveView?: boolean;
|
|
82
|
+
persistKey?: string;
|
|
83
|
+
/**
|
|
84
|
+
* Storage backend for the persisted active-view id. Defaults to `'local'`
|
|
85
|
+
* (the historical always-on behavior). The data-listing hooks (grid /
|
|
86
|
+
* table / kanban) thread their `persistState.storage` here so the active
|
|
87
|
+
* view follows the same session/local scope — and the same
|
|
88
|
+
* `docyrus:view-params` namespace via `persistKey` — as the rest of the
|
|
89
|
+
* persisted view parameters. The per-user hidden-views list intentionally
|
|
90
|
+
* stays in `localStorage`: hiding a view is a durable preference, not a
|
|
91
|
+
* per-tab tweak.
|
|
92
|
+
*/
|
|
93
|
+
activeViewStorage?: ParameterPersistenceStorage;
|
|
94
|
+
/**
|
|
95
|
+
* Column id used as the default row-grouping column for views that do not
|
|
96
|
+
* already specify a `grouping`. Forwarded to `DataGridViewSelect` and
|
|
97
|
+
* applied to the table by `useKivaDataGrid`.
|
|
98
|
+
*
|
|
99
|
+
* Eligible field types: `field-select`, `field-status`, `field-relation`,
|
|
100
|
+
* `field-date`, `field-dateTime`, `field-user`.
|
|
101
|
+
*/
|
|
102
|
+
defaultRowGroupingColumn?: string;
|
|
103
|
+
/**
|
|
104
|
+
* Developer-defined static views that always appear before the saved
|
|
105
|
+
* server-side views in the tab list. Each entry is automatically marked
|
|
106
|
+
* with `isSystem: true` so `<DataGridViewSelect>` hides Edit/Delete/Add
|
|
107
|
+
* actions for them. Hidden state is still per-user and persisted in
|
|
108
|
+
* `localStorage` (under the same key namespace as the active view), so
|
|
109
|
+
* users can hide a system view from the tab strip and bring it back via
|
|
110
|
+
* the **Manage All Views** menu.
|
|
111
|
+
*/
|
|
112
|
+
systemViews?: Array<SavedDataGridView>;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
type ManagedProps
|
|
116
|
+
= | 'views'
|
|
117
|
+
| 'activeViewId'
|
|
118
|
+
| 'fields'
|
|
119
|
+
| 'forms'
|
|
120
|
+
| 'onViewChange'
|
|
121
|
+
| 'onViewCreate'
|
|
122
|
+
| 'onViewSave'
|
|
123
|
+
| 'onViewDelete'
|
|
124
|
+
| 'onViewHide'
|
|
125
|
+
| 'onViewUnhide'
|
|
126
|
+
| 'onViewReorder'
|
|
127
|
+
| 'hiddenViewIds'
|
|
128
|
+
| 'disabled'
|
|
129
|
+
| 'defaultRowGroupingColumn'
|
|
130
|
+
| 'isSaving'
|
|
131
|
+
| 'isLoading';
|
|
132
|
+
|
|
133
|
+
export interface UseKivaDataViewSelectResult {
|
|
134
|
+
gridViewSelectProps: Pick<DataGridViewSelectProps<unknown>, ManagedProps>;
|
|
135
|
+
views: Array<SavedDataGridView>;
|
|
136
|
+
fields: Array<FullField>;
|
|
137
|
+
/** Active (non-archived) saved forms on the data source, default-form first. */
|
|
138
|
+
forms: Array<DataForm>;
|
|
139
|
+
dataSource: DataSource | undefined;
|
|
140
|
+
activeViewId: string;
|
|
141
|
+
setActiveViewId: (viewId: string) => void;
|
|
142
|
+
isLoading: boolean;
|
|
143
|
+
error: Error | null;
|
|
144
|
+
refetch: () => void;
|
|
145
|
+
/**
|
|
146
|
+
* The view list loaded successfully and came back empty.
|
|
147
|
+
*
|
|
148
|
+
* Nothing here is broken — but the grid will fall back to the data source's
|
|
149
|
+
* raw field order, which is a technical order nobody chose. Surface it, offer
|
|
150
|
+
* to create a view, or pass a `systemViews` entry as the default.
|
|
151
|
+
*/
|
|
152
|
+
hasNoViews: boolean;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function useKivaDataViewSelect(
|
|
156
|
+
options: UseKivaDataViewSelectOptions
|
|
157
|
+
): UseKivaDataViewSelectResult {
|
|
158
|
+
const {
|
|
159
|
+
client,
|
|
160
|
+
appSlug,
|
|
161
|
+
dataSourceSlug,
|
|
162
|
+
appId,
|
|
163
|
+
overrideFields,
|
|
164
|
+
dataSource: providedDataSource,
|
|
165
|
+
mapField,
|
|
166
|
+
staleTime = 30_000,
|
|
167
|
+
enabled = true,
|
|
168
|
+
enableDataViews = true,
|
|
169
|
+
dataSourceExpand = 'enums',
|
|
170
|
+
enableForms = true,
|
|
171
|
+
persistActiveView = true,
|
|
172
|
+
persistKey,
|
|
173
|
+
activeViewStorage = 'local',
|
|
174
|
+
defaultRowGroupingColumn,
|
|
175
|
+
systemViews
|
|
176
|
+
} = options;
|
|
177
|
+
|
|
178
|
+
const queryClient = useQueryClient();
|
|
179
|
+
|
|
180
|
+
/*
|
|
181
|
+
* Shared, post-sign-in-warmed inventory cache. Backing the data-source, view,
|
|
182
|
+
* and form clients with it means the grid's schema + saved views + forms are
|
|
183
|
+
* all served from the single `/v1/apps/data-sources?expand=views,forms,fields`
|
|
184
|
+
* request the inventory made at bootstrap — instead of a `getBySlug(expand)`,
|
|
185
|
+
* a `/views`, and a `/forms` round-trip per grid mount. Mutations through the
|
|
186
|
+
* view client patch the cache in place, so reads stay correct across a session.
|
|
187
|
+
*/
|
|
188
|
+
const inventory = useMemo(() => getSharedKivaInventory(client), [client]);
|
|
189
|
+
|
|
190
|
+
const dataSourcesClient = useMemo(
|
|
191
|
+
() => createDataSourceClient(client, { inventory }),
|
|
192
|
+
[client, inventory]
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
const dataViewsClient = useMemo(
|
|
196
|
+
() => createDataViewClient(client, appSlug, dataSourceSlug, { inventory }),
|
|
197
|
+
[
|
|
198
|
+
client,
|
|
199
|
+
appSlug,
|
|
200
|
+
dataSourceSlug,
|
|
201
|
+
inventory
|
|
202
|
+
]
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
const storageKey = useMemo(() => {
|
|
206
|
+
if (!persistActiveView) return null;
|
|
207
|
+
|
|
208
|
+
if (persistKey) return persistKey;
|
|
209
|
+
|
|
210
|
+
const suffix = appId ? `${appSlug}:${dataSourceSlug}:${appId}` : `${appSlug}:${dataSourceSlug}`;
|
|
211
|
+
|
|
212
|
+
return `docyrus:data-grid-view:${suffix}`;
|
|
213
|
+
}, [
|
|
214
|
+
persistActiveView,
|
|
215
|
+
persistKey,
|
|
216
|
+
appSlug,
|
|
217
|
+
dataSourceSlug,
|
|
218
|
+
appId
|
|
219
|
+
]);
|
|
220
|
+
|
|
221
|
+
/*
|
|
222
|
+
* Independent storage key for the per-user hidden-views list. Uses the
|
|
223
|
+
* same `appSlug[:dataSourceSlug[:appId]]` namespace as the active view so
|
|
224
|
+
* one consumer's hide list doesn't bleed into another's.
|
|
225
|
+
*/
|
|
226
|
+
const hiddenStorageKey = useMemo(() => {
|
|
227
|
+
const suffix = appId ? `${appSlug}:${dataSourceSlug}:${appId}` : `${appSlug}:${dataSourceSlug}`;
|
|
228
|
+
|
|
229
|
+
return `docyrus:data-grid-view-hidden:${suffix}`;
|
|
230
|
+
}, [appSlug, dataSourceSlug, appId]);
|
|
231
|
+
|
|
232
|
+
const readStoredHiddenIds = useCallback((): Array<string> => {
|
|
233
|
+
if (typeof window === 'undefined') return [];
|
|
234
|
+
|
|
235
|
+
try {
|
|
236
|
+
const raw = window.localStorage.getItem(hiddenStorageKey);
|
|
237
|
+
|
|
238
|
+
if (!raw) return [];
|
|
239
|
+
|
|
240
|
+
const parsed = JSON.parse(raw) as unknown;
|
|
241
|
+
|
|
242
|
+
if (!Array.isArray(parsed)) return [];
|
|
243
|
+
|
|
244
|
+
return parsed.filter((id): id is string => typeof id === 'string');
|
|
245
|
+
} catch {
|
|
246
|
+
return [];
|
|
247
|
+
}
|
|
248
|
+
}, [hiddenStorageKey]);
|
|
249
|
+
|
|
250
|
+
const writeStoredHiddenIds = useCallback(
|
|
251
|
+
(ids: Array<string>): void => {
|
|
252
|
+
if (typeof window === 'undefined') return;
|
|
253
|
+
|
|
254
|
+
try {
|
|
255
|
+
window.localStorage.setItem(hiddenStorageKey, JSON.stringify(ids));
|
|
256
|
+
} catch {
|
|
257
|
+
/* storage unavailable */
|
|
258
|
+
}
|
|
259
|
+
},
|
|
260
|
+
[hiddenStorageKey]
|
|
261
|
+
);
|
|
262
|
+
|
|
263
|
+
const [hiddenViewIds, setHiddenViewIds] = useState<Array<string>>(() => readStoredHiddenIds());
|
|
264
|
+
|
|
265
|
+
/*
|
|
266
|
+
* Re-read the stored hidden IDs whenever the storage key changes (e.g. on
|
|
267
|
+
* navigating to a different data source). Store-and-compare pattern so
|
|
268
|
+
* we don't trigger the `set-state-in-effect` warning.
|
|
269
|
+
*/
|
|
270
|
+
const [trackedReadStoredHiddenIds, setTrackedReadStoredHiddenIds] = useState(() => readStoredHiddenIds);
|
|
271
|
+
|
|
272
|
+
if (trackedReadStoredHiddenIds !== readStoredHiddenIds) {
|
|
273
|
+
setTrackedReadStoredHiddenIds(() => readStoredHiddenIds);
|
|
274
|
+
setHiddenViewIds(readStoredHiddenIds());
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const onViewHide = useCallback(
|
|
278
|
+
(viewId: string) => {
|
|
279
|
+
setHiddenViewIds((prev) => {
|
|
280
|
+
if (prev.includes(viewId)) return prev;
|
|
281
|
+
|
|
282
|
+
const next = [...prev, viewId];
|
|
283
|
+
|
|
284
|
+
writeStoredHiddenIds(next);
|
|
285
|
+
|
|
286
|
+
return next;
|
|
287
|
+
});
|
|
288
|
+
},
|
|
289
|
+
[writeStoredHiddenIds]
|
|
290
|
+
);
|
|
291
|
+
|
|
292
|
+
const onViewUnhide = useCallback(
|
|
293
|
+
(viewId: string) => {
|
|
294
|
+
setHiddenViewIds((prev) => {
|
|
295
|
+
if (!prev.includes(viewId)) return prev;
|
|
296
|
+
|
|
297
|
+
const next = prev.filter(id => id !== viewId);
|
|
298
|
+
|
|
299
|
+
writeStoredHiddenIds(next);
|
|
300
|
+
|
|
301
|
+
return next;
|
|
302
|
+
});
|
|
303
|
+
},
|
|
304
|
+
[writeStoredHiddenIds]
|
|
305
|
+
);
|
|
306
|
+
|
|
307
|
+
const readStoredViewId = useCallback((): string | null => {
|
|
308
|
+
if (!storageKey) return null;
|
|
309
|
+
|
|
310
|
+
const store = getPersistStore(activeViewStorage);
|
|
311
|
+
|
|
312
|
+
if (!store) return null;
|
|
313
|
+
|
|
314
|
+
try {
|
|
315
|
+
return store.getItem(storageKey);
|
|
316
|
+
} catch {
|
|
317
|
+
return null;
|
|
318
|
+
}
|
|
319
|
+
}, [storageKey, activeViewStorage]);
|
|
320
|
+
|
|
321
|
+
const writeStoredViewId = useCallback(
|
|
322
|
+
(viewId: string): void => {
|
|
323
|
+
if (!storageKey) return;
|
|
324
|
+
|
|
325
|
+
const store = getPersistStore(activeViewStorage);
|
|
326
|
+
|
|
327
|
+
if (!store) return;
|
|
328
|
+
|
|
329
|
+
try {
|
|
330
|
+
store.setItem(storageKey, viewId);
|
|
331
|
+
} catch {
|
|
332
|
+
/* storage unavailable (Safari private mode, quota, etc.) */
|
|
333
|
+
}
|
|
334
|
+
},
|
|
335
|
+
[storageKey, activeViewStorage]
|
|
336
|
+
);
|
|
337
|
+
|
|
338
|
+
const dataSourceKey = useMemo(
|
|
339
|
+
() => [
|
|
340
|
+
'kiva',
|
|
341
|
+
'dataSource',
|
|
342
|
+
appSlug,
|
|
343
|
+
dataSourceSlug,
|
|
344
|
+
dataSourceExpand || 'none'
|
|
345
|
+
] as const,
|
|
346
|
+
[appSlug, dataSourceSlug, dataSourceExpand]
|
|
347
|
+
);
|
|
348
|
+
|
|
349
|
+
const viewsKey = useMemo(
|
|
350
|
+
() => [
|
|
351
|
+
'kiva',
|
|
352
|
+
'dataViews',
|
|
353
|
+
appSlug,
|
|
354
|
+
dataSourceSlug,
|
|
355
|
+
appId ?? null
|
|
356
|
+
] as const,
|
|
357
|
+
[appSlug, dataSourceSlug, appId]
|
|
358
|
+
);
|
|
359
|
+
|
|
360
|
+
const queryEnabled = enabled && Boolean(appSlug) && Boolean(dataSourceSlug);
|
|
361
|
+
|
|
362
|
+
const dataSourceQuery = useQuery({
|
|
363
|
+
queryKey: dataSourceKey,
|
|
364
|
+
queryFn: async () => {
|
|
365
|
+
/*
|
|
366
|
+
* Cache-first: no `expand` → the inventory serves the data source with
|
|
367
|
+
* `fields` (incl. enums), `views`, and `forms` already embedded. On a
|
|
368
|
+
* cache miss the app-utils client falls back to a scoped GET with no
|
|
369
|
+
* expand (basic shape, no fields) — detected by the absent `fields`
|
|
370
|
+
* array — so re-fetch scoped WITH expand to cover data sources outside
|
|
371
|
+
* the inventory list (e.g. system data sources).
|
|
372
|
+
*/
|
|
373
|
+
const cached = await dataSourcesClient.getBySlug(appSlug, dataSourceSlug);
|
|
374
|
+
|
|
375
|
+
if (Array.isArray((cached as DataSource | undefined)?.fields)) {
|
|
376
|
+
return cached;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
return dataSourcesClient.getBySlug(
|
|
380
|
+
appSlug,
|
|
381
|
+
dataSourceSlug,
|
|
382
|
+
{ expand: dataSourceExpand || 'enums' }
|
|
383
|
+
);
|
|
384
|
+
},
|
|
385
|
+
enabled: queryEnabled && !providedDataSource,
|
|
386
|
+
staleTime
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
/*
|
|
390
|
+
* Caller-supplied schema wins over the fetched one. When `providedDataSource`
|
|
391
|
+
* is set the query above is disabled, so this is the only source of fields /
|
|
392
|
+
* metadata for every downstream derivation (and for the `dataSource` result).
|
|
393
|
+
*/
|
|
394
|
+
const resolvedDataSource = providedDataSource ?? dataSourceQuery.data;
|
|
395
|
+
|
|
396
|
+
/*
|
|
397
|
+
* Saved forms. Inventory-backed: `list()` reads the cached data source's
|
|
398
|
+
* embedded `forms` array (the inventory's `expand=views,forms,fields` fetch
|
|
399
|
+
* embeds forms WITH their `layout`, normalized to the canonical `DataForm`
|
|
400
|
+
* shape), so no dedicated `/forms` round-trip on grid mount. The hook only
|
|
401
|
+
* ever calls `list()` (never `get(id)`), and the embedded shape carries the
|
|
402
|
+
* layout/display fields the view↔form binding needs. Falls back to the API on
|
|
403
|
+
* a cache miss; a failure (e.g. system data sources without a forms endpoint)
|
|
404
|
+
* leaves `forms` empty without breaking the hook.
|
|
405
|
+
*/
|
|
406
|
+
const dataFormsClient = useMemo(
|
|
407
|
+
() => createDataFormClient(client, appSlug, dataSourceSlug, { inventory }),
|
|
408
|
+
[
|
|
409
|
+
client,
|
|
410
|
+
appSlug,
|
|
411
|
+
dataSourceSlug,
|
|
412
|
+
inventory
|
|
413
|
+
]
|
|
414
|
+
);
|
|
415
|
+
|
|
416
|
+
const formsKey = useMemo(
|
|
417
|
+
() => [
|
|
418
|
+
'kiva',
|
|
419
|
+
'dataSourceForms',
|
|
420
|
+
appSlug,
|
|
421
|
+
dataSourceSlug
|
|
422
|
+
] as const,
|
|
423
|
+
[appSlug, dataSourceSlug]
|
|
424
|
+
);
|
|
425
|
+
|
|
426
|
+
const formsQuery = useQuery({
|
|
427
|
+
queryKey: formsKey,
|
|
428
|
+
queryFn: () => dataFormsClient.list(),
|
|
429
|
+
enabled: queryEnabled && enableForms,
|
|
430
|
+
staleTime
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
/* Drop archived forms and sort the default form first for the picker. */
|
|
434
|
+
const forms = useMemo<Array<DataForm>>(() => {
|
|
435
|
+
const list = formsQuery.data ?? providedDataSource?.forms ?? [];
|
|
436
|
+
|
|
437
|
+
return list
|
|
438
|
+
.filter(form => form.archived !== true)
|
|
439
|
+
.slice()
|
|
440
|
+
.sort((a, b) => {
|
|
441
|
+
if (a.is_default !== b.is_default) return a.is_default ? -1 : 1;
|
|
442
|
+
|
|
443
|
+
return (a.name || '').localeCompare(b.name || '');
|
|
444
|
+
});
|
|
445
|
+
}, [formsQuery.data, providedDataSource?.forms]);
|
|
446
|
+
|
|
447
|
+
const viewsQuery = useQuery({
|
|
448
|
+
queryKey: viewsKey,
|
|
449
|
+
queryFn: () => dataViewsClient.list({ appId }),
|
|
450
|
+
enabled: queryEnabled && enableDataViews,
|
|
451
|
+
staleTime
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
const invalidateViews = useCallback(
|
|
455
|
+
() => {
|
|
456
|
+
void queryClient.invalidateQueries({ queryKey: viewsKey });
|
|
457
|
+
},
|
|
458
|
+
[queryClient, viewsKey]
|
|
459
|
+
);
|
|
460
|
+
|
|
461
|
+
/*
|
|
462
|
+
* Id of a view that was just created (or duplicated — the dialog routes a
|
|
463
|
+
* copy through `onViewCreate` too). It cannot be selected inside `onSuccess`:
|
|
464
|
+
* the views query has not refetched yet, so the id is not in `views` and the
|
|
465
|
+
* `activeViewId` resolver would discard it. An effect below claims it once
|
|
466
|
+
* the refetch lands.
|
|
467
|
+
*/
|
|
468
|
+
const pendingActiveViewIdRef = useRef<string | undefined>(undefined);
|
|
469
|
+
|
|
470
|
+
const createMutation = useMutation({
|
|
471
|
+
mutationFn: (view: SavedDataGridView) => dataViewsClient.create(savedViewToCreateBody(view)),
|
|
472
|
+
onSuccess: (created) => {
|
|
473
|
+
if (created?.id) pendingActiveViewIdRef.current = created.id;
|
|
474
|
+
|
|
475
|
+
invalidateViews();
|
|
476
|
+
}
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
const updateMutation = useMutation({
|
|
480
|
+
mutationFn: (view: SavedDataGridView) => dataViewsClient.update(view.id, savedViewToUpdateBody(view)),
|
|
481
|
+
onSuccess: invalidateViews
|
|
482
|
+
});
|
|
483
|
+
|
|
484
|
+
const deleteMutation = useMutation({
|
|
485
|
+
mutationFn: (viewId: string) => dataViewsClient.remove(viewId),
|
|
486
|
+
onSuccess: invalidateViews
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
/*
|
|
490
|
+
* The backend has no bulk-reorder endpoint, so we issue one PATCH per view
|
|
491
|
+
* whose `sort_order` actually changed. Optimistic cache update keeps the
|
|
492
|
+
* Manage All Views dialog in sync immediately; on failure we invalidate to
|
|
493
|
+
* snap back to server truth.
|
|
494
|
+
*/
|
|
495
|
+
/*
|
|
496
|
+
* The server-known order, captured by `onMutate` BEFORE it applies the
|
|
497
|
+
* optimistic rewrite. `onMutate` runs first and stamps every cached view
|
|
498
|
+
* with its new `sort_order`, so a `mutationFn` reading the cache compares
|
|
499
|
+
* the new order against itself: `view.sort_order === index` holds for every
|
|
500
|
+
* row, no PATCH is issued and the drag survives only until the next
|
|
501
|
+
* refetch. Diffing against this snapshot is what makes a reorder persist.
|
|
502
|
+
*/
|
|
503
|
+
const preReorderViewsRef = useRef<Array<DataView> | undefined>(undefined);
|
|
504
|
+
|
|
505
|
+
const reorderMutation = useMutation({
|
|
506
|
+
mutationFn: async (orderedViewIds: Array<string>) => {
|
|
507
|
+
const current = preReorderViewsRef.current ?? queryClient.getQueryData<Array<DataView>>(viewsKey) ?? [];
|
|
508
|
+
const byId = new Map(current.map(v => [v.id, v]));
|
|
509
|
+
const updates: Array<Promise<DataView>> = [];
|
|
510
|
+
|
|
511
|
+
orderedViewIds.forEach((viewId, index) => {
|
|
512
|
+
const view = byId.get(viewId);
|
|
513
|
+
|
|
514
|
+
if (!view || view.sort_order === index) return;
|
|
515
|
+
|
|
516
|
+
updates.push(dataViewsClient.update(viewId, { sort_order: index }));
|
|
517
|
+
});
|
|
518
|
+
|
|
519
|
+
await Promise.all(updates);
|
|
520
|
+
},
|
|
521
|
+
onMutate: async (orderedViewIds) => {
|
|
522
|
+
await queryClient.cancelQueries({ queryKey: viewsKey });
|
|
523
|
+
|
|
524
|
+
const previous = queryClient.getQueryData<Array<DataView>>(viewsKey);
|
|
525
|
+
|
|
526
|
+
preReorderViewsRef.current = previous;
|
|
527
|
+
|
|
528
|
+
if (previous) {
|
|
529
|
+
const byId = new Map(previous.map(v => [v.id, v]));
|
|
530
|
+
const reordered: Array<DataView> = [];
|
|
531
|
+
|
|
532
|
+
orderedViewIds.forEach((id, index) => {
|
|
533
|
+
const view = byId.get(id);
|
|
534
|
+
|
|
535
|
+
if (view) reordered.push({ ...view, sort_order: index });
|
|
536
|
+
});
|
|
537
|
+
|
|
538
|
+
queryClient.setQueryData<Array<DataView>>(viewsKey, reordered);
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
return { previous };
|
|
542
|
+
},
|
|
543
|
+
onError: (_err, _vars, ctx) => {
|
|
544
|
+
if (ctx?.previous) {
|
|
545
|
+
queryClient.setQueryData(viewsKey, ctx.previous);
|
|
546
|
+
}
|
|
547
|
+
},
|
|
548
|
+
onSettled: () => {
|
|
549
|
+
preReorderViewsRef.current = undefined;
|
|
550
|
+
invalidateViews();
|
|
551
|
+
}
|
|
552
|
+
});
|
|
553
|
+
|
|
554
|
+
const userViews = useMemo<Array<SavedDataGridView>>(
|
|
555
|
+
() => {
|
|
556
|
+
const list = (viewsQuery.data ?? []).slice();
|
|
557
|
+
|
|
558
|
+
/*
|
|
559
|
+
* Sort by persisted `sort_order` (nulls last), falling back to name so
|
|
560
|
+
* legacy views without an order still render deterministically. This
|
|
561
|
+
* is what makes drag-reordering visible in the manage-views dialog
|
|
562
|
+
* survive a refetch.
|
|
563
|
+
*/
|
|
564
|
+
list.sort((a, b) => {
|
|
565
|
+
const aOrder = a.sort_order ?? Number.POSITIVE_INFINITY;
|
|
566
|
+
const bOrder = b.sort_order ?? Number.POSITIVE_INFINITY;
|
|
567
|
+
|
|
568
|
+
if (aOrder !== bOrder) return aOrder - bOrder;
|
|
569
|
+
|
|
570
|
+
return a.name.localeCompare(b.name);
|
|
571
|
+
});
|
|
572
|
+
|
|
573
|
+
return list.map(dataViewToSavedView);
|
|
574
|
+
},
|
|
575
|
+
[viewsQuery.data]
|
|
576
|
+
);
|
|
577
|
+
|
|
578
|
+
const normalizedSystemViews = useMemo<Array<SavedDataGridView>>(
|
|
579
|
+
() => (systemViews ?? []).map(view => ({ ...view, isSystem: true })),
|
|
580
|
+
[systemViews]
|
|
581
|
+
);
|
|
582
|
+
|
|
583
|
+
const views = useMemo<Array<SavedDataGridView>>(
|
|
584
|
+
() => [...normalizedSystemViews, ...userViews],
|
|
585
|
+
[normalizedSystemViews, userViews]
|
|
586
|
+
);
|
|
587
|
+
|
|
588
|
+
/*
|
|
589
|
+
* Initialize from localStorage on first render so the resolved
|
|
590
|
+
* `activeViewId` can lock onto the user's last selection on the very
|
|
591
|
+
* first paint (e.g. when React Query hands back cached views
|
|
592
|
+
* synchronously after a route navigation). Falling back to ''
|
|
593
|
+
* lets the memo below pick a default once views arrive.
|
|
594
|
+
*/
|
|
595
|
+
const [activeViewIdState, setActiveViewIdState] = useState<string>(
|
|
596
|
+
() => readStoredViewId() ?? ''
|
|
597
|
+
);
|
|
598
|
+
|
|
599
|
+
/*
|
|
600
|
+
* Derive the active view id from the current views every render. The
|
|
601
|
+
* priority chain is:
|
|
602
|
+
* 1. The currently selected view (if it still exists)
|
|
603
|
+
* 2. The persisted localStorage value (if it still exists)
|
|
604
|
+
* 3. The view marked `isDefault` on the backend
|
|
605
|
+
* 4. The first available view
|
|
606
|
+
*
|
|
607
|
+
* Computing during render — instead of correcting state in an effect —
|
|
608
|
+
* means the resolved id is available on the first render that has
|
|
609
|
+
* cached views, so downstream hooks (`useKivaDataGrid`) don't have
|
|
610
|
+
* to wait for a follow-up state transition that may never arrive.
|
|
611
|
+
*/
|
|
612
|
+
const activeViewId = useMemo(() => {
|
|
613
|
+
if (views.length === 0) return '';
|
|
614
|
+
|
|
615
|
+
if (activeViewIdState && views.some(v => v.id === activeViewIdState)) {
|
|
616
|
+
return activeViewIdState;
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
const stored = readStoredViewId();
|
|
620
|
+
|
|
621
|
+
if (stored && views.some(v => v.id === stored)) {
|
|
622
|
+
return stored;
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
return views.find(v => v.isDefault)?.id ?? views[0]?.id ?? '';
|
|
626
|
+
}, [views, activeViewIdState, readStoredViewId]);
|
|
627
|
+
|
|
628
|
+
const setActiveViewId = useCallback((viewId: string) => {
|
|
629
|
+
setActiveViewIdState(viewId);
|
|
630
|
+
}, []);
|
|
631
|
+
|
|
632
|
+
/*
|
|
633
|
+
* Claim a just-created view as soon as the refetch makes it real. Without
|
|
634
|
+
* this a user who creates or duplicates a view is left looking at the tab
|
|
635
|
+
* they started on and has to hunt for the new one.
|
|
636
|
+
*/
|
|
637
|
+
useEffect(() => {
|
|
638
|
+
const pending = pendingActiveViewIdRef.current;
|
|
639
|
+
|
|
640
|
+
if (!pending) return;
|
|
641
|
+
|
|
642
|
+
if (views.some(view => view.id === pending)) {
|
|
643
|
+
pendingActiveViewIdRef.current = undefined;
|
|
644
|
+
setActiveViewIdState(pending);
|
|
645
|
+
}
|
|
646
|
+
}, [views]);
|
|
647
|
+
|
|
648
|
+
const hasNoViews
|
|
649
|
+
= enableDataViews && viewsQuery.isSuccess && views.length === 0;
|
|
650
|
+
|
|
651
|
+
/*
|
|
652
|
+
* A data source with no view at all renders in raw field order and says
|
|
653
|
+
* nothing about it — the failure the team hit was noticing months later that
|
|
654
|
+
* a grid had been shipping technical column order all along. Say it once, in
|
|
655
|
+
* development, where it is cheap to fix.
|
|
656
|
+
*/
|
|
657
|
+
const warnedRef = useRef(false);
|
|
658
|
+
|
|
659
|
+
useEffect(() => {
|
|
660
|
+
if (!hasNoViews || warnedRef.current) return;
|
|
661
|
+
if (process.env.NODE_ENV === 'production') return;
|
|
662
|
+
|
|
663
|
+
warnedRef.current = true;
|
|
664
|
+
console.warn(
|
|
665
|
+
`[kiva] "${appSlug}/${dataSourceSlug}" has no saved data view. `
|
|
666
|
+
+ 'The grid will use the data source\'s raw field order. Create an "All" '
|
|
667
|
+
+ 'view for it, or pass one through `systemViews`.'
|
|
668
|
+
);
|
|
669
|
+
}, [appSlug, dataSourceSlug, hasNoViews]);
|
|
670
|
+
|
|
671
|
+
/*
|
|
672
|
+
* Persist the resolved active view id (not just user-clicked ones) so
|
|
673
|
+
* defaults picked by the memo above also survive reloads.
|
|
674
|
+
*/
|
|
675
|
+
useEffect(() => {
|
|
676
|
+
if (activeViewId) writeStoredViewId(activeViewId);
|
|
677
|
+
}, [activeViewId, writeStoredViewId]);
|
|
678
|
+
|
|
679
|
+
const onViewChange = useCallback(
|
|
680
|
+
(view: SavedDataGridView) => {
|
|
681
|
+
setActiveViewId(view.id);
|
|
682
|
+
},
|
|
683
|
+
[setActiveViewId]
|
|
684
|
+
);
|
|
685
|
+
|
|
686
|
+
/*
|
|
687
|
+
* Relation fields whose target data source slug + app slug are exposed
|
|
688
|
+
* directly on the field metadata (`relation_data_source_app_slug` /
|
|
689
|
+
* `relation_data_source_slug`). We use these to eagerly load the first
|
|
690
|
+
* batch of related records for the query builder's "is one of" /
|
|
691
|
+
* "is none of" multi-select picker.
|
|
692
|
+
*/
|
|
693
|
+
const relationTargets = useMemo<Array<RelationTarget>>(() => {
|
|
694
|
+
const rawFields = resolvedDataSource?.fields ?? [];
|
|
695
|
+
|
|
696
|
+
return rawFields
|
|
697
|
+
.map(field => extractRelationTarget(field))
|
|
698
|
+
.filter((target): target is RelationTarget => target !== null);
|
|
699
|
+
}, [resolvedDataSource?.fields]);
|
|
700
|
+
|
|
701
|
+
const relationOptionsQuery = useQuery<Record<string, Array<KivaEnumOption>>>({
|
|
702
|
+
queryKey: [
|
|
703
|
+
'kiva',
|
|
704
|
+
'queryBuilderRelationOptions',
|
|
705
|
+
appSlug,
|
|
706
|
+
dataSourceSlug,
|
|
707
|
+
relationTargets.map(t => `${t.fieldSlug}:${t.appSlug ?? ''}/${t.dataSourceSlug ?? ''}#${t.relationDataSourceId ?? ''}`)
|
|
708
|
+
],
|
|
709
|
+
queryFn: async () => {
|
|
710
|
+
const result: Record<string, Array<KivaEnumOption>> = {};
|
|
711
|
+
|
|
712
|
+
/*
|
|
713
|
+
* Some fields only carry `relationDataSourceId` (a UUID), not the target
|
|
714
|
+
* app / data-source slugs. Resolve those via the shared inventory list
|
|
715
|
+
* (a cache hit on the bootstrap `/v1/apps/data-sources` request) once,
|
|
716
|
+
* only when at least one target needs it.
|
|
717
|
+
*/
|
|
718
|
+
const needsLookup = relationTargets.some(t => !(t.appSlug && t.dataSourceSlug) && t.relationDataSourceId);
|
|
719
|
+
let idToSlug: Map<string, { appSlug: string; slug: string }> | null = null;
|
|
720
|
+
|
|
721
|
+
if (needsLookup) {
|
|
722
|
+
idToSlug = new Map<string, { appSlug: string; slug: string }>();
|
|
723
|
+
|
|
724
|
+
const list = await inventory.listDataSources();
|
|
725
|
+
|
|
726
|
+
for (const entry of list) {
|
|
727
|
+
const record = entry as Record<string, unknown>;
|
|
728
|
+
const id = typeof record.id === 'string' ? record.id : null;
|
|
729
|
+
const slug = typeof record.slug === 'string' ? record.slug : null;
|
|
730
|
+
const targetAppSlug = typeof record.appSlug === 'string' ? record.appSlug : typeof record.app_slug === 'string' ? record.app_slug : null;
|
|
731
|
+
|
|
732
|
+
if (id && slug && targetAppSlug) {
|
|
733
|
+
idToSlug.set(id, { appSlug: targetAppSlug, slug });
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
await Promise.all(
|
|
739
|
+
relationTargets.map(async (target) => {
|
|
740
|
+
let resolved: { appSlug: string; slug: string } | undefined;
|
|
741
|
+
|
|
742
|
+
if (target.appSlug && target.dataSourceSlug) {
|
|
743
|
+
resolved = { appSlug: target.appSlug, slug: target.dataSourceSlug };
|
|
744
|
+
} else if (target.relationDataSourceId) {
|
|
745
|
+
resolved = idToSlug?.get(target.relationDataSourceId);
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
if (!resolved) return;
|
|
749
|
+
|
|
750
|
+
/*
|
|
751
|
+
* Eagerly fetch the first 200 records of the related data source.
|
|
752
|
+
* The multi-select chip popover uses Command's built-in fuzzy search
|
|
753
|
+
* for filtering. For data sources with > 200 records, additional
|
|
754
|
+
* loading would need a search-aware async value editor.
|
|
755
|
+
*/
|
|
756
|
+
const response = await client.get<{ data?: Array<Record<string, unknown>> } | Array<Record<string, unknown>>>(
|
|
757
|
+
`/v1/apps/${resolved.appSlug}/data-sources/${resolved.slug}/items`,
|
|
758
|
+
{ columns: 'id, name, autonumber_id', limit: 200 } as Parameters<typeof client.get>[1]
|
|
759
|
+
);
|
|
760
|
+
|
|
761
|
+
const items = Array.isArray(response) ? response : (response?.data ?? []);
|
|
762
|
+
|
|
763
|
+
result[target.fieldSlug] = items
|
|
764
|
+
.map((item) => {
|
|
765
|
+
const id = typeof item.id === 'string' ? item.id : null;
|
|
766
|
+
|
|
767
|
+
if (!id) return null;
|
|
768
|
+
|
|
769
|
+
const autonumber = typeof item.autonumber_id === 'string' || typeof item.autonumber_id === 'number' ? String(item.autonumber_id) : undefined;
|
|
770
|
+
const label = typeof item.name === 'string' ? item.name : (autonumber ?? id);
|
|
771
|
+
|
|
772
|
+
return {
|
|
773
|
+
name: id,
|
|
774
|
+
value: id,
|
|
775
|
+
label,
|
|
776
|
+
/*
|
|
777
|
+
* Surface `autonumber_id` (already fetched above) as a short,
|
|
778
|
+
* human-friendly disambiguator so the picker shows e.g. `#1042`
|
|
779
|
+
* instead of the last 6 chars of the raw UUID. Skip it when the
|
|
780
|
+
* label already IS the autonumber (avoids "1042 · #1042").
|
|
781
|
+
*/
|
|
782
|
+
...(autonumber && autonumber !== label ? { secondaryLabel: autonumber } : {})
|
|
783
|
+
} as KivaEnumOption;
|
|
784
|
+
})
|
|
785
|
+
.filter((option): option is KivaEnumOption => option !== null);
|
|
786
|
+
})
|
|
787
|
+
);
|
|
788
|
+
|
|
789
|
+
return result;
|
|
790
|
+
},
|
|
791
|
+
enabled: queryEnabled && relationTargets.length > 0,
|
|
792
|
+
staleTime
|
|
793
|
+
});
|
|
794
|
+
|
|
795
|
+
const fields = useMemo<Array<FullField>>(() => {
|
|
796
|
+
if (overrideFields) return overrideFields;
|
|
797
|
+
|
|
798
|
+
const rawFields = resolvedDataSource?.fields ?? [];
|
|
799
|
+
const relationOptions = relationOptionsQuery.data ?? {};
|
|
800
|
+
|
|
801
|
+
return rawFields
|
|
802
|
+
.map((field) => {
|
|
803
|
+
const base = dsFieldToFullField(field);
|
|
804
|
+
|
|
805
|
+
const loadedRelationOptions = relationOptions[field.slug];
|
|
806
|
+
|
|
807
|
+
if (loadedRelationOptions && loadedRelationOptions.length > 0) {
|
|
808
|
+
base.values = loadedRelationOptions;
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
return mapField ? mapField(field, base) : base;
|
|
812
|
+
})
|
|
813
|
+
.filter((field): field is FullField => field !== null);
|
|
814
|
+
}, [
|
|
815
|
+
resolvedDataSource?.fields,
|
|
816
|
+
relationOptionsQuery.data,
|
|
817
|
+
overrideFields,
|
|
818
|
+
mapField
|
|
819
|
+
]);
|
|
820
|
+
|
|
821
|
+
const onViewCreate = useCallback(
|
|
822
|
+
(view: SavedDataGridView) => {
|
|
823
|
+
createMutation.mutate(view);
|
|
824
|
+
},
|
|
825
|
+
[createMutation]
|
|
826
|
+
);
|
|
827
|
+
|
|
828
|
+
const onViewSave = useCallback(
|
|
829
|
+
(view: SavedDataGridView) => {
|
|
830
|
+
updateMutation.mutate(view);
|
|
831
|
+
},
|
|
832
|
+
[updateMutation]
|
|
833
|
+
);
|
|
834
|
+
|
|
835
|
+
const onViewDelete = useCallback(
|
|
836
|
+
(viewId: string) => {
|
|
837
|
+
deleteMutation.mutate(viewId);
|
|
838
|
+
},
|
|
839
|
+
[deleteMutation]
|
|
840
|
+
);
|
|
841
|
+
|
|
842
|
+
const systemViewIds = useMemo(
|
|
843
|
+
() => new Set(normalizedSystemViews.map(v => v.id)),
|
|
844
|
+
[normalizedSystemViews]
|
|
845
|
+
);
|
|
846
|
+
|
|
847
|
+
const onViewReorder = useCallback(
|
|
848
|
+
(orderedViewIds: Array<string>) => {
|
|
849
|
+
/*
|
|
850
|
+
* System views are developer-defined and not persisted on the backend,
|
|
851
|
+
* so we drop them from the payload before issuing PATCH requests. Their
|
|
852
|
+
* relative position is fixed (always before user views) by the
|
|
853
|
+
* `[...systemViews, ...userViews]` composition.
|
|
854
|
+
*/
|
|
855
|
+
const userOnly = orderedViewIds.filter(id => !systemViewIds.has(id));
|
|
856
|
+
|
|
857
|
+
reorderMutation.mutate(userOnly);
|
|
858
|
+
},
|
|
859
|
+
[reorderMutation, systemViewIds]
|
|
860
|
+
);
|
|
861
|
+
|
|
862
|
+
const isLoading = dataSourceQuery.isLoading || viewsQuery.isLoading;
|
|
863
|
+
const error = (dataSourceQuery.error ?? viewsQuery.error) as Error | null;
|
|
864
|
+
|
|
865
|
+
const isSaving = createMutation.isPending || updateMutation.isPending;
|
|
866
|
+
|
|
867
|
+
const gridViewSelectProps = useMemo<Pick<DataGridViewSelectProps<unknown>, ManagedProps>>(
|
|
868
|
+
() => ({
|
|
869
|
+
views,
|
|
870
|
+
activeViewId,
|
|
871
|
+
fields,
|
|
872
|
+
forms,
|
|
873
|
+
onViewChange,
|
|
874
|
+
onViewCreate,
|
|
875
|
+
onViewSave,
|
|
876
|
+
onViewDelete,
|
|
877
|
+
onViewHide,
|
|
878
|
+
onViewUnhide,
|
|
879
|
+
onViewReorder,
|
|
880
|
+
hiddenViewIds,
|
|
881
|
+
disabled: error !== null,
|
|
882
|
+
defaultRowGroupingColumn,
|
|
883
|
+
isSaving,
|
|
884
|
+
isLoading
|
|
885
|
+
}),
|
|
886
|
+
[
|
|
887
|
+
views,
|
|
888
|
+
activeViewId,
|
|
889
|
+
fields,
|
|
890
|
+
forms,
|
|
891
|
+
onViewChange,
|
|
892
|
+
onViewCreate,
|
|
893
|
+
onViewSave,
|
|
894
|
+
onViewDelete,
|
|
895
|
+
onViewHide,
|
|
896
|
+
onViewUnhide,
|
|
897
|
+
onViewReorder,
|
|
898
|
+
hiddenViewIds,
|
|
899
|
+
error,
|
|
900
|
+
defaultRowGroupingColumn,
|
|
901
|
+
isSaving,
|
|
902
|
+
isLoading
|
|
903
|
+
]
|
|
904
|
+
);
|
|
905
|
+
|
|
906
|
+
const refetch = useCallback(() => {
|
|
907
|
+
void dataSourceQuery.refetch();
|
|
908
|
+
void viewsQuery.refetch();
|
|
909
|
+
}, [dataSourceQuery, viewsQuery]);
|
|
910
|
+
|
|
911
|
+
return {
|
|
912
|
+
gridViewSelectProps,
|
|
913
|
+
views,
|
|
914
|
+
fields,
|
|
915
|
+
forms,
|
|
916
|
+
dataSource: resolvedDataSource,
|
|
917
|
+
activeViewId,
|
|
918
|
+
setActiveViewId,
|
|
919
|
+
isLoading,
|
|
920
|
+
error,
|
|
921
|
+
refetch,
|
|
922
|
+
hasNoViews
|
|
923
|
+
};
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
/*
|
|
927
|
+
* ---------------------------------------------------------------------------
|
|
928
|
+
* Mappers (module-local — kept in this file because the registry generator
|
|
929
|
+
* does not bundle transitive files for hooks).
|
|
930
|
+
* ---------------------------------------------------------------------------
|
|
931
|
+
*/
|
|
932
|
+
|
|
933
|
+
function savedViewToColumnsPayload(view: SavedDataGridView): Record<string, unknown> {
|
|
934
|
+
return {
|
|
935
|
+
visibility: view.columnVisibility,
|
|
936
|
+
order: view.columnOrder,
|
|
937
|
+
pinning: view.columnPinning,
|
|
938
|
+
grouping: view.grouping ?? [],
|
|
939
|
+
rowHeight: view.rowHeight,
|
|
940
|
+
displayMode: view.displayMode,
|
|
941
|
+
/*
|
|
942
|
+
* Paging configuration travels under `columns` so the backend's opaque
|
|
943
|
+
* JSON column survives every round-trip without schema changes. The
|
|
944
|
+
* view editor populates these from its draft state; `applyViewToTable`
|
|
945
|
+
* later reads them back to drive the table's pagination state.
|
|
946
|
+
*/
|
|
947
|
+
pagingEnabled: view.pagingEnabled,
|
|
948
|
+
pagingMode: view.pagingMode,
|
|
949
|
+
pageSize: view.pageSize,
|
|
950
|
+
/*
|
|
951
|
+
* Inline-editing configuration also lives under `columns` for the same
|
|
952
|
+
* reason. `inlineEditingEnabled` is the master toggle and
|
|
953
|
+
* `readOnlyColumns` is the per-column override list — both are
|
|
954
|
+
* recomputed by `useKivaDataGrid` against identity / metadata-locked
|
|
955
|
+
* fields before being applied to the table.
|
|
956
|
+
*/
|
|
957
|
+
inlineEditingEnabled: view.inlineEditingEnabled,
|
|
958
|
+
readOnlyColumns: view.readOnlyColumns,
|
|
959
|
+
/*
|
|
960
|
+
* Per-column runtime overrides toggled from the column header dropdown
|
|
961
|
+
* (e.g. relation `showAutonumber`). Travels under the opaque `columns`
|
|
962
|
+
* payload so the backend doesn't need a dedicated schema for it.
|
|
963
|
+
*/
|
|
964
|
+
columnOptions: view.columnOptions,
|
|
965
|
+
/*
|
|
966
|
+
* Id of the saved form bound to this view. Travels under the opaque
|
|
967
|
+
* `columns` blob (same rationale as paging / columnOptions) so the
|
|
968
|
+
* view↔form binding round-trips without a backend schema change.
|
|
969
|
+
* `useKivaDataGrid` reads it back to drive `useKivaFormView`.
|
|
970
|
+
*/
|
|
971
|
+
formId: view.formId,
|
|
972
|
+
/*
|
|
973
|
+
* Gallery display-mode config (card slot bindings + display settings).
|
|
974
|
+
* Travels under the opaque `columns` blob like the other view-only knobs
|
|
975
|
+
* so the backend needs no dedicated schema. Read back by both
|
|
976
|
+
* `useKivaDataGrid` (in-grid gallery mode) and `useKivaDataGallery`.
|
|
977
|
+
*/
|
|
978
|
+
galleryCardConfig: view.galleryCardConfig,
|
|
979
|
+
galleryDisplayConfig: view.galleryDisplayConfig
|
|
980
|
+
};
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
function savedViewToFiltersPayload(view: SavedDataGridView): Record<string, unknown> {
|
|
984
|
+
return {
|
|
985
|
+
columnFilters: view.columnFilters ?? [],
|
|
986
|
+
filterQuery: view.filterQuery
|
|
987
|
+
};
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
function savedViewToSortPayload(view: SavedDataGridView): Record<string, unknown> {
|
|
991
|
+
return {
|
|
992
|
+
sorting: view.sorting ?? []
|
|
993
|
+
};
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
function savedViewToColorRulesPayload(view: SavedDataGridView): Record<string, unknown> {
|
|
997
|
+
return {
|
|
998
|
+
row: view.rowColorRules ?? [],
|
|
999
|
+
cell: view.cellColorRules ?? []
|
|
1000
|
+
};
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
function savedViewToCreateBody(view: SavedDataGridView): CreateDataViewBody {
|
|
1004
|
+
const body: CreateDataViewBody = {
|
|
1005
|
+
name: view.name,
|
|
1006
|
+
columns: savedViewToColumnsPayload(view),
|
|
1007
|
+
filters: savedViewToFiltersPayload(view),
|
|
1008
|
+
sort: savedViewToSortPayload(view),
|
|
1009
|
+
color_rules: savedViewToColorRulesPayload(view)
|
|
1010
|
+
};
|
|
1011
|
+
|
|
1012
|
+
if (view.description && view.description.length > 0) {
|
|
1013
|
+
body.description = view.description;
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
/*
|
|
1017
|
+
* The editor has a full icon picker and the grid renders `view.icon`, but
|
|
1018
|
+
* neither mapper ever carried it, so a picked icon lived only until the next
|
|
1019
|
+
* reload. `DataView.icon` / `CreateDataViewBody.icon` / `UpdateDataViewBody.icon`
|
|
1020
|
+
* have always existed on the API contract.
|
|
1021
|
+
*
|
|
1022
|
+
* Sent only when set, matching `description`: both bodies type the field as
|
|
1023
|
+
* `string | undefined`, so there is no value that means "clear this" without
|
|
1024
|
+
* guessing how the API reads an empty string. Clearing either one is
|
|
1025
|
+
* therefore still a no-op — see the note on this hook in packages/ui/CLAUDE.md.
|
|
1026
|
+
*/
|
|
1027
|
+
if (view.icon && view.icon.length > 0) {
|
|
1028
|
+
body.icon = view.icon;
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
return body;
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
function savedViewToUpdateBody(view: SavedDataGridView): UpdateDataViewBody {
|
|
1035
|
+
const body: UpdateDataViewBody = {
|
|
1036
|
+
name: view.name,
|
|
1037
|
+
columns: savedViewToColumnsPayload(view),
|
|
1038
|
+
filters: savedViewToFiltersPayload(view),
|
|
1039
|
+
sort: savedViewToSortPayload(view),
|
|
1040
|
+
color_rules: savedViewToColorRulesPayload(view)
|
|
1041
|
+
};
|
|
1042
|
+
|
|
1043
|
+
if (view.description && view.description.length > 0) {
|
|
1044
|
+
body.description = view.description;
|
|
1045
|
+
}
|
|
1046
|
+
|
|
1047
|
+
/*
|
|
1048
|
+
* The editor has a full icon picker and the grid renders `view.icon`, but
|
|
1049
|
+
* neither mapper ever carried it, so a picked icon lived only until the next
|
|
1050
|
+
* reload. `DataView.icon` / `CreateDataViewBody.icon` / `UpdateDataViewBody.icon`
|
|
1051
|
+
* have always existed on the API contract.
|
|
1052
|
+
*
|
|
1053
|
+
* Sent only when set, matching `description`: both bodies type the field as
|
|
1054
|
+
* `string | undefined`, so there is no value that means "clear this" without
|
|
1055
|
+
* guessing how the API reads an empty string. Clearing either one is
|
|
1056
|
+
* therefore still a no-op — see the note on this hook in packages/ui/CLAUDE.md.
|
|
1057
|
+
*/
|
|
1058
|
+
if (view.icon && view.icon.length > 0) {
|
|
1059
|
+
body.icon = view.icon;
|
|
1060
|
+
}
|
|
1061
|
+
|
|
1062
|
+
return body;
|
|
1063
|
+
}
|
|
1064
|
+
|
|
1065
|
+
function pickRecord(value: Record<string, unknown> | null | undefined, key: string): unknown {
|
|
1066
|
+
if (!value) return undefined;
|
|
1067
|
+
|
|
1068
|
+
return value[key];
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
function dataViewToSavedView(dv: DataView): SavedDataGridView {
|
|
1072
|
+
const columns = dv.columns ?? {};
|
|
1073
|
+
const filters = dv.filters ?? {};
|
|
1074
|
+
const sort = dv.sort ?? {};
|
|
1075
|
+
const colorRules = dv.color_rules ?? {};
|
|
1076
|
+
|
|
1077
|
+
return {
|
|
1078
|
+
id: dv.id,
|
|
1079
|
+
name: dv.name,
|
|
1080
|
+
description: dv.description ?? undefined,
|
|
1081
|
+
icon: dv.icon ?? undefined,
|
|
1082
|
+
columnVisibility: (pickRecord(columns, 'visibility') as Record<string, boolean>) ?? {},
|
|
1083
|
+
columnOrder: (pickRecord(columns, 'order') as Array<string>) ?? [],
|
|
1084
|
+
columnPinning: (pickRecord(columns, 'pinning') as SavedDataGridView['columnPinning']) ?? { left: [], right: [] },
|
|
1085
|
+
grouping: (pickRecord(columns, 'grouping') as Array<string>) ?? [],
|
|
1086
|
+
rowHeight: normalizeRowHeight(pickRecord(columns, 'rowHeight')),
|
|
1087
|
+
displayMode: pickRecord(columns, 'displayMode') as SavedDataGridView['displayMode'],
|
|
1088
|
+
sorting: (pickRecord(sort, 'sorting') as SavedDataGridView['sorting']) ?? [],
|
|
1089
|
+
columnFilters: (pickRecord(filters, 'columnFilters') as SavedDataGridView['columnFilters']) ?? [],
|
|
1090
|
+
filterQuery: pickRecord(filters, 'filterQuery') as SavedDataGridView['filterQuery'],
|
|
1091
|
+
rowColorRules: (pickRecord(colorRules, 'row') as SavedDataGridView['rowColorRules']) ?? [],
|
|
1092
|
+
cellColorRules: (pickRecord(colorRules, 'cell') as SavedDataGridView['cellColorRules']) ?? [],
|
|
1093
|
+
/*
|
|
1094
|
+
* Mirrors the writer side in `savedViewToColumnsPayload` — paging is
|
|
1095
|
+
* stashed inside the opaque `columns` blob so the backend doesn't need
|
|
1096
|
+
* a dedicated schema for it.
|
|
1097
|
+
*/
|
|
1098
|
+
pagingEnabled: pickRecord(columns, 'pagingEnabled') as boolean | undefined,
|
|
1099
|
+
pagingMode: pickRecord(columns, 'pagingMode') as SavedDataGridView['pagingMode'],
|
|
1100
|
+
pageSize: pickRecord(columns, 'pageSize') as number | undefined,
|
|
1101
|
+
inlineEditingEnabled: pickRecord(columns, 'inlineEditingEnabled') as boolean | undefined,
|
|
1102
|
+
readOnlyColumns: (pickRecord(columns, 'readOnlyColumns') as Array<string>) ?? undefined,
|
|
1103
|
+
columnOptions: (pickRecord(columns, 'columnOptions') as SavedDataGridView['columnOptions']) ?? undefined,
|
|
1104
|
+
formId: (pickRecord(columns, 'formId') as string | undefined) || undefined,
|
|
1105
|
+
/*
|
|
1106
|
+
* Mirror the writer side — gallery config is stashed inside the opaque
|
|
1107
|
+
* `columns` blob (see `savedViewToColumnsPayload`).
|
|
1108
|
+
*/
|
|
1109
|
+
galleryCardConfig: (pickRecord(columns, 'galleryCardConfig') as SavedDataGridView['galleryCardConfig']) ?? undefined,
|
|
1110
|
+
galleryDisplayConfig: (pickRecord(columns, 'galleryDisplayConfig') as SavedDataGridView['galleryDisplayConfig']) ?? undefined,
|
|
1111
|
+
isDefault: dv.is_default
|
|
1112
|
+
};
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
const FIELD_TYPE_MAP: Record<string, { inputType?: FullField['inputType']; valueEditorType?: FullField['valueEditorType'] }> = {
|
|
1116
|
+
'field-text': { inputType: 'text', valueEditorType: 'text' },
|
|
1117
|
+
'field-code': { inputType: 'text', valueEditorType: 'text' },
|
|
1118
|
+
'field-email': { inputType: 'text', valueEditorType: 'text' },
|
|
1119
|
+
'field-phone': { inputType: 'text', valueEditorType: 'text' },
|
|
1120
|
+
'field-url': { inputType: 'text', valueEditorType: 'text' },
|
|
1121
|
+
'field-color': { inputType: 'text', valueEditorType: 'text' },
|
|
1122
|
+
'field-icon': { inputType: 'text', valueEditorType: 'text' },
|
|
1123
|
+
'field-textarea': { inputType: 'text', valueEditorType: 'textarea' },
|
|
1124
|
+
'field-htmlEditor': { inputType: 'text', valueEditorType: 'textarea' },
|
|
1125
|
+
'field-docEditor': { inputType: 'text', valueEditorType: 'textarea' },
|
|
1126
|
+
'field-number': { inputType: 'number', valueEditorType: 'text' },
|
|
1127
|
+
'field-money': { inputType: 'number', valueEditorType: 'text' },
|
|
1128
|
+
'field-percent': { inputType: 'number', valueEditorType: 'text' },
|
|
1129
|
+
'field-currency': { inputType: 'number', valueEditorType: 'text' },
|
|
1130
|
+
'field-duration': { inputType: 'number', valueEditorType: 'text' },
|
|
1131
|
+
'field-rating': { inputType: 'number', valueEditorType: 'text' },
|
|
1132
|
+
'field-autonumber': { inputType: 'number', valueEditorType: 'text' },
|
|
1133
|
+
'field-identity': { inputType: 'number', valueEditorType: 'text' },
|
|
1134
|
+
'field-date': { inputType: 'date', valueEditorType: 'text' },
|
|
1135
|
+
'field-dateRange': { inputType: 'date', valueEditorType: 'text' },
|
|
1136
|
+
'field-dateTime': { inputType: 'datetime-local', valueEditorType: 'text' },
|
|
1137
|
+
'field-time': { inputType: 'time', valueEditorType: 'text' },
|
|
1138
|
+
'field-checkbox': { inputType: 'text', valueEditorType: 'checkbox' },
|
|
1139
|
+
'field-switch': { inputType: 'text', valueEditorType: 'checkbox' },
|
|
1140
|
+
'field-enum': { inputType: 'text', valueEditorType: 'select' },
|
|
1141
|
+
'field-select': { inputType: 'text', valueEditorType: 'select' },
|
|
1142
|
+
'field-radioGroup': { inputType: 'text', valueEditorType: 'radio' },
|
|
1143
|
+
'field-status': { inputType: 'text', valueEditorType: 'select' },
|
|
1144
|
+
'field-systemEnum': { inputType: 'text', valueEditorType: 'select' },
|
|
1145
|
+
'field-multiSelect': { inputType: 'text', valueEditorType: 'multiselect' },
|
|
1146
|
+
'field-tagSelect': { inputType: 'text', valueEditorType: 'multiselect' }
|
|
1147
|
+
};
|
|
1148
|
+
|
|
1149
|
+
function dsFieldToFullField(field: DataSourceField): FullField {
|
|
1150
|
+
const mapping = FIELD_TYPE_MAP[field.type] ?? { inputType: 'text', valueEditorType: 'text' };
|
|
1151
|
+
const options = extractFieldOptions(field);
|
|
1152
|
+
const filterGroup: FilterGroup = getFilterGroupForFieldType(field.type);
|
|
1153
|
+
|
|
1154
|
+
/*
|
|
1155
|
+
* Per-operator value-editor type so operators like "empty"/"between"/
|
|
1156
|
+
* "weekdays" render the right editor (or no editor at all). Falls back to
|
|
1157
|
+
* the field's natural editor type derived from its Kiva field type.
|
|
1158
|
+
*/
|
|
1159
|
+
const mappedEditorType = typeof mapping.valueEditorType === 'function' ? undefined : mapping.valueEditorType;
|
|
1160
|
+
const fallbackValueEditorType = mappedEditorType ?? FILTER_GROUP_VALUE_EDITOR_TYPE[filterGroup];
|
|
1161
|
+
const fallbackInputType = mapping.inputType ?? FILTER_GROUP_INPUT_TYPE[filterGroup];
|
|
1162
|
+
|
|
1163
|
+
const fullField: FullField = {
|
|
1164
|
+
name: field.slug,
|
|
1165
|
+
value: field.slug,
|
|
1166
|
+
label: field.name,
|
|
1167
|
+
inputType: fallbackInputType,
|
|
1168
|
+
valueEditorType: (operator: string) => resolveValueEditorType(filterGroup, operator, fallbackValueEditorType),
|
|
1169
|
+
operators: getOperatorsForGroup(filterGroup),
|
|
1170
|
+
/*
|
|
1171
|
+
* Keep filterGroup + fieldType on the field so consumers (and the
|
|
1172
|
+
* QueryBuilderKiva component) can reason about it without re-deriving.
|
|
1173
|
+
*/
|
|
1174
|
+
filterGroup,
|
|
1175
|
+
fieldType: field.type
|
|
1176
|
+
} as FullField & { filterGroup: FilterGroup; fieldType: string };
|
|
1177
|
+
|
|
1178
|
+
if (options && options.length > 0) {
|
|
1179
|
+
fullField.values = options;
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
return fullField;
|
|
1183
|
+
}
|
|
1184
|
+
|
|
1185
|
+
interface KivaEnumOption {
|
|
1186
|
+
name: string;
|
|
1187
|
+
value: string;
|
|
1188
|
+
label: string;
|
|
1189
|
+
icon?: string;
|
|
1190
|
+
color?: string;
|
|
1191
|
+
[key: string]: unknown;
|
|
1192
|
+
}
|
|
1193
|
+
|
|
1194
|
+
const RELATION_FIELD_TYPES = new Set(['field-relation', 'field-relatedField']);
|
|
1195
|
+
|
|
1196
|
+
interface RelationTarget {
|
|
1197
|
+
fieldSlug: string;
|
|
1198
|
+
/**
|
|
1199
|
+
* Target app / data-source slug when the field metadata exposes them inline
|
|
1200
|
+
* (`relation_data_source_app_slug` / `relation_data_source_slug`). May be
|
|
1201
|
+
* absent — in which case `relationDataSourceId` is resolved to slugs via the
|
|
1202
|
+
* inventory data-source list inside the query.
|
|
1203
|
+
*/
|
|
1204
|
+
appSlug?: string;
|
|
1205
|
+
dataSourceSlug?: string;
|
|
1206
|
+
/** UUID of the related data source, resolved to slugs when inline slugs are missing. */
|
|
1207
|
+
relationDataSourceId?: string;
|
|
1208
|
+
}
|
|
1209
|
+
|
|
1210
|
+
function extractRelationTarget(field: DataSourceField): RelationTarget | null {
|
|
1211
|
+
if (!RELATION_FIELD_TYPES.has(field.type)) return null;
|
|
1212
|
+
|
|
1213
|
+
const raw = field as Record<string, unknown>;
|
|
1214
|
+
const targetAppSlug
|
|
1215
|
+
= typeof raw.relation_data_source_app_slug === 'string' ? raw.relation_data_source_app_slug : typeof raw.relationDataSourceAppSlug === 'string' ? raw.relationDataSourceAppSlug : undefined;
|
|
1216
|
+
const targetDataSourceSlug
|
|
1217
|
+
= typeof raw.relation_data_source_slug === 'string' ? raw.relation_data_source_slug : typeof raw.relationDataSourceSlug === 'string' ? raw.relationDataSourceSlug : undefined;
|
|
1218
|
+
const relationDataSourceId
|
|
1219
|
+
= typeof raw.relation_data_source_id === 'string' ? raw.relation_data_source_id : typeof raw.relationDataSourceId === 'string' ? raw.relationDataSourceId : undefined;
|
|
1220
|
+
|
|
1221
|
+
/*
|
|
1222
|
+
* Load records when the field exposes the target slugs inline OR a
|
|
1223
|
+
* `relationDataSourceId` we can translate to slugs via the inventory list.
|
|
1224
|
+
* Without either, there's no path to the related items endpoint.
|
|
1225
|
+
*/
|
|
1226
|
+
if (!(targetAppSlug && targetDataSourceSlug) && !relationDataSourceId) return null;
|
|
1227
|
+
|
|
1228
|
+
return {
|
|
1229
|
+
fieldSlug: field.slug,
|
|
1230
|
+
appSlug: targetAppSlug,
|
|
1231
|
+
dataSourceSlug: targetDataSourceSlug,
|
|
1232
|
+
relationDataSourceId
|
|
1233
|
+
};
|
|
1234
|
+
}
|
|
1235
|
+
|
|
1236
|
+
function extractFieldOptions(field: DataSourceField): Array<KivaEnumOption> | null {
|
|
1237
|
+
const raw = field as Record<string, unknown>;
|
|
1238
|
+
const source = Array.isArray(raw.enums) ? raw.enums : Array.isArray(raw.options) ? raw.options : null;
|
|
1239
|
+
|
|
1240
|
+
if (!source) return null;
|
|
1241
|
+
|
|
1242
|
+
const result: Array<KivaEnumOption> = [];
|
|
1243
|
+
|
|
1244
|
+
for (const entry of source) {
|
|
1245
|
+
if (!entry || typeof entry !== 'object') continue;
|
|
1246
|
+
|
|
1247
|
+
const option = entry as Record<string, unknown>;
|
|
1248
|
+
/*
|
|
1249
|
+
* Prefer the enum option's UUID `id` so multi-value LIST operators
|
|
1250
|
+
* ("is one of" / "is none of") send arrays of stable identifiers to the
|
|
1251
|
+
* backend. Fall back to slug / value when no id is present (e.g. inline
|
|
1252
|
+
* options that haven't been persisted yet).
|
|
1253
|
+
*/
|
|
1254
|
+
const id = typeof option.id === 'string' ? option.id : undefined;
|
|
1255
|
+
const slug = typeof option.slug === 'string' ? option.slug : typeof option.value === 'string' ? option.value : undefined;
|
|
1256
|
+
const identifier = id ?? slug;
|
|
1257
|
+
const label = typeof option.name === 'string' ? option.name : typeof option.label === 'string' ? option.label : (slug ?? identifier);
|
|
1258
|
+
const icon = typeof option.icon === 'string' ? option.icon : undefined;
|
|
1259
|
+
const color = typeof option.color === 'string' ? option.color : undefined;
|
|
1260
|
+
|
|
1261
|
+
if (identifier && label) {
|
|
1262
|
+
result.push({
|
|
1263
|
+
name: identifier,
|
|
1264
|
+
value: identifier,
|
|
1265
|
+
label,
|
|
1266
|
+
...(icon ? { icon } : {}),
|
|
1267
|
+
...(color ? { color } : {})
|
|
1268
|
+
});
|
|
1269
|
+
}
|
|
1270
|
+
}
|
|
1271
|
+
|
|
1272
|
+
return result;
|
|
1273
|
+
}
|