@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.
- package/package.json +187 -21
- 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,992 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
// @ts-nocheck
|
|
4
|
+
/* eslint-disable */
|
|
5
|
+
import {
|
|
6
|
+
useCallback,
|
|
7
|
+
useMemo,
|
|
8
|
+
useRef,
|
|
9
|
+
useState,
|
|
10
|
+
type ReactNode
|
|
11
|
+
} from 'react';
|
|
12
|
+
|
|
13
|
+
import { type DataSourceField } from '@kivacore/app-utils';
|
|
14
|
+
|
|
15
|
+
import { useQuery } from '@tanstack/react-query';
|
|
16
|
+
|
|
17
|
+
import {
|
|
18
|
+
type CalendarFilterItem,
|
|
19
|
+
type CalendarProviderProps,
|
|
20
|
+
type IEvent,
|
|
21
|
+
type IUser,
|
|
22
|
+
type TCalendarView,
|
|
23
|
+
type TEventColor,
|
|
24
|
+
type TEventId,
|
|
25
|
+
type TTimelineNavigationMode
|
|
26
|
+
} from '@kivacore/ui-pro-shared/components/kiva/calendar';
|
|
27
|
+
|
|
28
|
+
import {
|
|
29
|
+
useKivaDataGrid,
|
|
30
|
+
type KivaDataGridListParams,
|
|
31
|
+
type UseKivaDataGridOptions,
|
|
32
|
+
type UseKivaDataGridResult
|
|
33
|
+
} from './use-kiva-data-grid';
|
|
34
|
+
|
|
35
|
+
/*
|
|
36
|
+
* -------------------------------------------------------------------------
|
|
37
|
+
* Constants
|
|
38
|
+
* ----------------------------------------------------------------------
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
const EMPTY_ITEMS: Array<never> = [];
|
|
42
|
+
|
|
43
|
+
const DATE_FIELD_TYPES = new Set(['field-date', 'field-dateTime']);
|
|
44
|
+
|
|
45
|
+
const USER_FIELD_TYPES = new Set(['field-userSelect', 'field-userMultiSelect']);
|
|
46
|
+
|
|
47
|
+
const VALID_COLORS = new Set<TEventColor>([
|
|
48
|
+
'blue',
|
|
49
|
+
'green',
|
|
50
|
+
'red',
|
|
51
|
+
'yellow',
|
|
52
|
+
'purple',
|
|
53
|
+
'orange'
|
|
54
|
+
]);
|
|
55
|
+
|
|
56
|
+
const TITLE_FIELD_CANDIDATES = [
|
|
57
|
+
'name',
|
|
58
|
+
'title',
|
|
59
|
+
'label',
|
|
60
|
+
'subject',
|
|
61
|
+
'display_name',
|
|
62
|
+
'displayName',
|
|
63
|
+
'commercial_title',
|
|
64
|
+
'short_name'
|
|
65
|
+
] as const;
|
|
66
|
+
|
|
67
|
+
const DESCRIPTION_FIELD_CANDIDATES = [
|
|
68
|
+
'description',
|
|
69
|
+
'notes',
|
|
70
|
+
'content',
|
|
71
|
+
'details',
|
|
72
|
+
'body',
|
|
73
|
+
'summary'
|
|
74
|
+
] as const;
|
|
75
|
+
|
|
76
|
+
/*
|
|
77
|
+
* -------------------------------------------------------------------------
|
|
78
|
+
* Public types
|
|
79
|
+
* ----------------------------------------------------------------------
|
|
80
|
+
*/
|
|
81
|
+
|
|
82
|
+
export interface KivaCalendarEvent<TData> extends IEvent {
|
|
83
|
+
/** Original UUID from the Kiva record. */
|
|
84
|
+
recordId: string;
|
|
85
|
+
/** Original raw record returned by the API. */
|
|
86
|
+
item: TData;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface KivaCalendarCollection<TData> {
|
|
90
|
+
list: (params?: Record<string, unknown>) => Promise<Array<TData> | { data: Array<TData> }>;
|
|
91
|
+
create?: (data: Record<string, unknown>) => Promise<unknown>;
|
|
92
|
+
update?: (id: string, data: Record<string, unknown>) => Promise<unknown>;
|
|
93
|
+
delete?: (id: string) => Promise<unknown>;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export interface KivaCalendarEventBuilderContext<TData> {
|
|
97
|
+
item: TData;
|
|
98
|
+
index: number;
|
|
99
|
+
startDateField: DataSourceField;
|
|
100
|
+
endDateField: DataSourceField | null;
|
|
101
|
+
titleField: DataSourceField | null;
|
|
102
|
+
colorField: DataSourceField | null;
|
|
103
|
+
descriptionField: DataSourceField | null;
|
|
104
|
+
userField: DataSourceField | null;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export interface UseKivaCalendarOptions<TData>
|
|
108
|
+
extends Omit<
|
|
109
|
+
UseKivaDataGridOptions<TData>,
|
|
110
|
+
| 'data'
|
|
111
|
+
| 'enableItemsQuery'
|
|
112
|
+
| 'showSelectColumn'
|
|
113
|
+
| 'enableRowMarkers'
|
|
114
|
+
| 'collection'
|
|
115
|
+
> {
|
|
116
|
+
/** Optional collection adapter for custom data sources. */
|
|
117
|
+
collection?: KivaCalendarCollection<TData>;
|
|
118
|
+
/** Slug of the date/datetime field to use as event start. Auto-detected if omitted. */
|
|
119
|
+
startDateFieldSlug?: string;
|
|
120
|
+
/** Slug of the date/datetime field to use as event end. Auto-detected if omitted. */
|
|
121
|
+
endDateFieldSlug?: string;
|
|
122
|
+
/** Slug of the field to use as event title. Auto-detected if omitted. */
|
|
123
|
+
titleFieldSlug?: string;
|
|
124
|
+
/** Slug of the field to use as event color. Auto-detected if omitted. */
|
|
125
|
+
colorFieldSlug?: string;
|
|
126
|
+
/** Slug of the field to use as event description. Auto-detected if omitted. */
|
|
127
|
+
descriptionFieldSlug?: string;
|
|
128
|
+
/** Slug of the user/userSelect field. Used to populate the users filter. Auto-detected if omitted. */
|
|
129
|
+
userFieldSlug?: string;
|
|
130
|
+
/**
|
|
131
|
+
* Maps field values (enum option names, color strings) to `TEventColor`.
|
|
132
|
+
* Example: `{ 'Meeting': 'blue', 'Leave': 'red' }`
|
|
133
|
+
*/
|
|
134
|
+
colorMap?: Record<string, TEventColor>;
|
|
135
|
+
/**
|
|
136
|
+
* Fallback event duration in minutes when `endDateField` is absent or empty.
|
|
137
|
+
* @default 60
|
|
138
|
+
*/
|
|
139
|
+
defaultEventDurationMinutes?: number;
|
|
140
|
+
/**
|
|
141
|
+
* Override event construction entirely. Return `null` to skip a record.
|
|
142
|
+
*/
|
|
143
|
+
getEvent?: (
|
|
144
|
+
item: TData,
|
|
145
|
+
context: KivaCalendarEventBuilderContext<TData>
|
|
146
|
+
) => KivaCalendarEvent<TData> | null;
|
|
147
|
+
/** Default calendar view. */
|
|
148
|
+
defaultView?: TCalendarView;
|
|
149
|
+
/**
|
|
150
|
+
* Scope the items query to records that overlap the calendar's currently
|
|
151
|
+
* visible range. When `true`, the hook adds a filter rule
|
|
152
|
+
* `startDate < rangeEnd AND endDate > rangeStart` (overlap test) to the
|
|
153
|
+
* items query payload and refetches whenever the user navigates the
|
|
154
|
+
* calendar. Set `setVisibleRange` from `CalendarProvider`'s
|
|
155
|
+
* `onVisibleRangeChange` callback to drive it.
|
|
156
|
+
*/
|
|
157
|
+
enableDateRangeFilter?: boolean;
|
|
158
|
+
/**
|
|
159
|
+
* Event lanes a period-timeline row (`year-timeline` / `quarter` / `13weeks`)
|
|
160
|
+
* renders before the rest collapse into an expandable `+N more`.
|
|
161
|
+
* @default 3
|
|
162
|
+
*/
|
|
163
|
+
timelineMaxLanes?: number;
|
|
164
|
+
/**
|
|
165
|
+
* Initial pagination mode of the `year-timeline` view — `continuous` stacks
|
|
166
|
+
* years and appends more as the user scrolls, `single` shows one year.
|
|
167
|
+
* The user can change it from the calendar's settings menu.
|
|
168
|
+
* @default 'continuous'
|
|
169
|
+
*/
|
|
170
|
+
defaultYearTimelineMode?: TTimelineNavigationMode;
|
|
171
|
+
/**
|
|
172
|
+
* Initial pagination mode of the `quarter` view.
|
|
173
|
+
* @default 'continuous'
|
|
174
|
+
*/
|
|
175
|
+
defaultQuarterMode?: TTimelineNavigationMode;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export interface UseKivaCalendarResult<TData>
|
|
179
|
+
extends Pick<
|
|
180
|
+
UseKivaDataGridResult<TData>,
|
|
181
|
+
| 'table'
|
|
182
|
+
| 'toolbar'
|
|
183
|
+
| 'views'
|
|
184
|
+
| 'fields'
|
|
185
|
+
| 'dataSource'
|
|
186
|
+
| 'activeViewId'
|
|
187
|
+
| 'setActiveViewId'
|
|
188
|
+
> {
|
|
189
|
+
/** Transformed events ready for `CalendarProvider`. */
|
|
190
|
+
events: Array<KivaCalendarEvent<TData>>;
|
|
191
|
+
/** Unique users extracted from events, ready for `CalendarProvider`. */
|
|
192
|
+
users: Array<IUser>;
|
|
193
|
+
/** All raw items fetched from Kiva. */
|
|
194
|
+
items: Array<TData>;
|
|
195
|
+
/** Resolved field definitions. */
|
|
196
|
+
startDateField: DataSourceField | null;
|
|
197
|
+
endDateField: DataSourceField | null;
|
|
198
|
+
titleField: DataSourceField | null;
|
|
199
|
+
colorField: DataSourceField | null;
|
|
200
|
+
descriptionField: DataSourceField | null;
|
|
201
|
+
userField: DataSourceField | null;
|
|
202
|
+
/** Whether a usable start-date field was found on the data source. */
|
|
203
|
+
hasDateField: boolean;
|
|
204
|
+
/** Comma-separated columns string sent to the items endpoint. */
|
|
205
|
+
requestedColumns: string;
|
|
206
|
+
/** Full resolved query payload. */
|
|
207
|
+
resolvedListParams: KivaDataGridListParams;
|
|
208
|
+
/**
|
|
209
|
+
* Ready-to-spread props for `<CalendarProvider>` (excludes `children`).
|
|
210
|
+
* Wires CRUD callbacks to Kiva API automatically.
|
|
211
|
+
*/
|
|
212
|
+
calendarProviderProps: Omit<CalendarProviderProps, 'children'>;
|
|
213
|
+
/** Generic filter items derived from `userField` values — pass to `calendarProviderProps.filterItems`. */
|
|
214
|
+
filterItems: Array<CalendarFilterItem>;
|
|
215
|
+
/** Persist a new event to Kiva. Call from `onCreateEvent` or your own form. */
|
|
216
|
+
createRecord: (startDate: Date, endDate: Date, extraValues?: Record<string, unknown>) => Promise<void>;
|
|
217
|
+
/** Persist field changes for an existing record by its Kiva UUID. */
|
|
218
|
+
updateRecord: (recordId: string, data: Record<string, unknown>) => Promise<void>;
|
|
219
|
+
/** Delete a record by its Kiva UUID. */
|
|
220
|
+
deleteRecord: (recordId: string) => Promise<void>;
|
|
221
|
+
/** Move an event (drag-and-drop). Patches start+end date fields. */
|
|
222
|
+
moveRecord: (recordId: string, newStart: Date, newEnd: Date) => Promise<void>;
|
|
223
|
+
/** Currently active visible range — drives the date-range filter when enabled. */
|
|
224
|
+
visibleRange: { start: Date; end: Date } | null;
|
|
225
|
+
/** Setter wired to `CalendarProvider`'s `onVisibleRangeChange` callback. */
|
|
226
|
+
setVisibleRange: (range: { start: Date; end: Date } | null) => void;
|
|
227
|
+
isLoading: boolean;
|
|
228
|
+
/** `true` while a refetch is in flight (initial or otherwise). */
|
|
229
|
+
isFetching: boolean;
|
|
230
|
+
/**
|
|
231
|
+
* `true` after the first successful fetch. Use to render a skeleton only
|
|
232
|
+
* on initial mount — passing `isLoading` directly to `<Calendar>` causes
|
|
233
|
+
* its provider to unmount and reset `selectedDate` on every refetch.
|
|
234
|
+
*/
|
|
235
|
+
hasLoadedOnce: boolean;
|
|
236
|
+
error: Error | null;
|
|
237
|
+
reload: () => void;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/*
|
|
241
|
+
* -------------------------------------------------------------------------
|
|
242
|
+
* Hook
|
|
243
|
+
* ----------------------------------------------------------------------
|
|
244
|
+
*/
|
|
245
|
+
|
|
246
|
+
export function useKivaCalendar<TData>(
|
|
247
|
+
options: UseKivaCalendarOptions<TData>
|
|
248
|
+
): UseKivaCalendarResult<TData> {
|
|
249
|
+
const {
|
|
250
|
+
startDateFieldSlug,
|
|
251
|
+
endDateFieldSlug,
|
|
252
|
+
titleFieldSlug,
|
|
253
|
+
colorFieldSlug,
|
|
254
|
+
descriptionFieldSlug,
|
|
255
|
+
userFieldSlug,
|
|
256
|
+
colorMap,
|
|
257
|
+
defaultEventDurationMinutes = 60,
|
|
258
|
+
getEvent,
|
|
259
|
+
defaultView = 'month',
|
|
260
|
+
enableDateRangeFilter = false,
|
|
261
|
+
timelineMaxLanes,
|
|
262
|
+
defaultYearTimelineMode,
|
|
263
|
+
defaultQuarterMode,
|
|
264
|
+
collection,
|
|
265
|
+
staleTime = 30_000,
|
|
266
|
+
onReload,
|
|
267
|
+
enableGroupMenu = false,
|
|
268
|
+
enableRowHeightMenu = false,
|
|
269
|
+
enableDisplayMenu = false,
|
|
270
|
+
client,
|
|
271
|
+
appSlug,
|
|
272
|
+
dataSourceSlug,
|
|
273
|
+
...dataGridOptions
|
|
274
|
+
} = options;
|
|
275
|
+
|
|
276
|
+
/* ---- data grid (toolbar + saved views + field metadata) -------------- */
|
|
277
|
+
|
|
278
|
+
const dataGrid = useKivaDataGrid<TData>({
|
|
279
|
+
client,
|
|
280
|
+
appSlug,
|
|
281
|
+
dataSourceSlug,
|
|
282
|
+
...dataGridOptions,
|
|
283
|
+
collection: collection ? { list: collection.list } : undefined,
|
|
284
|
+
data: EMPTY_ITEMS as Array<TData>,
|
|
285
|
+
staleTime,
|
|
286
|
+
onReload,
|
|
287
|
+
enableItemsQuery: false,
|
|
288
|
+
showSelectColumn: false,
|
|
289
|
+
enableRowMarkers: false,
|
|
290
|
+
enableGroupMenu,
|
|
291
|
+
enableRowHeightMenu,
|
|
292
|
+
enableDisplayMenu
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
/* ---- field resolution ------------------------------------------------ */
|
|
296
|
+
|
|
297
|
+
const startDateField = useMemo(
|
|
298
|
+
() => resolveStartDateField(dataGrid.dataSource?.fields, startDateFieldSlug),
|
|
299
|
+
[dataGrid.dataSource?.fields, startDateFieldSlug]
|
|
300
|
+
);
|
|
301
|
+
|
|
302
|
+
const endDateField = useMemo(
|
|
303
|
+
() => resolveEndDateField(dataGrid.dataSource?.fields, endDateFieldSlug, startDateField?.slug),
|
|
304
|
+
[dataGrid.dataSource?.fields, endDateFieldSlug, startDateField?.slug]
|
|
305
|
+
);
|
|
306
|
+
|
|
307
|
+
const titleField = useMemo(
|
|
308
|
+
() => resolveTitleField(dataGrid.dataSource?.fields, titleFieldSlug),
|
|
309
|
+
[dataGrid.dataSource?.fields, titleFieldSlug]
|
|
310
|
+
);
|
|
311
|
+
|
|
312
|
+
const colorField = useMemo(
|
|
313
|
+
() => resolveColorField(dataGrid.dataSource?.fields, colorFieldSlug),
|
|
314
|
+
[dataGrid.dataSource?.fields, colorFieldSlug]
|
|
315
|
+
);
|
|
316
|
+
|
|
317
|
+
const descriptionField = useMemo(
|
|
318
|
+
() => resolveDescriptionField(dataGrid.dataSource?.fields, descriptionFieldSlug, titleField?.slug),
|
|
319
|
+
[dataGrid.dataSource?.fields, descriptionFieldSlug, titleField?.slug]
|
|
320
|
+
);
|
|
321
|
+
|
|
322
|
+
const userField = useMemo(
|
|
323
|
+
() => resolveUserField(dataGrid.dataSource?.fields, userFieldSlug),
|
|
324
|
+
[dataGrid.dataSource?.fields, userFieldSlug]
|
|
325
|
+
);
|
|
326
|
+
|
|
327
|
+
/* ---- required columns ------------------------------------------------ */
|
|
328
|
+
|
|
329
|
+
const requiredFieldSlugs = useMemo(() => {
|
|
330
|
+
const slugs = [
|
|
331
|
+
'id',
|
|
332
|
+
startDateField?.slug,
|
|
333
|
+
endDateField?.slug,
|
|
334
|
+
titleField?.slug,
|
|
335
|
+
colorField?.slug,
|
|
336
|
+
descriptionField?.slug,
|
|
337
|
+
userField?.slug
|
|
338
|
+
];
|
|
339
|
+
|
|
340
|
+
return slugs.filter(
|
|
341
|
+
(s, i): s is string => Boolean(s) && slugs.indexOf(s) === i
|
|
342
|
+
);
|
|
343
|
+
}, [
|
|
344
|
+
startDateField?.slug,
|
|
345
|
+
endDateField?.slug,
|
|
346
|
+
titleField?.slug,
|
|
347
|
+
colorField?.slug,
|
|
348
|
+
descriptionField?.slug,
|
|
349
|
+
userField?.slug
|
|
350
|
+
]);
|
|
351
|
+
|
|
352
|
+
const requestedColumns = useMemo(
|
|
353
|
+
() => requiredFieldSlugs.join(', '),
|
|
354
|
+
[requiredFieldSlugs]
|
|
355
|
+
);
|
|
356
|
+
|
|
357
|
+
const [visibleRange, setVisibleRange] = useState<{ start: Date; end: Date } | null>(null);
|
|
358
|
+
|
|
359
|
+
const resolvedListParams = useMemo<KivaDataGridListParams>(() => {
|
|
360
|
+
const baseFilters = dataGrid.resolvedListParams.filters;
|
|
361
|
+
|
|
362
|
+
let filters = baseFilters;
|
|
363
|
+
|
|
364
|
+
if (enableDateRangeFilter && visibleRange && startDateField) {
|
|
365
|
+
/*
|
|
366
|
+
* Overlap rule: an event overlaps the visible window when its start
|
|
367
|
+
* falls before `rangeEnd` AND its end falls after `rangeStart`.
|
|
368
|
+
* When the end-date field is absent we fall back to a single-bound
|
|
369
|
+
* test on the start field.
|
|
370
|
+
*/
|
|
371
|
+
const rangeRules: Array<{ field: string; operator: string; value: string }> = [{ field: startDateField.slug, operator: '<=', value: visibleRange.end.toISOString() }];
|
|
372
|
+
|
|
373
|
+
if (endDateField) {
|
|
374
|
+
rangeRules.push({ field: endDateField.slug, operator: '>=', value: visibleRange.start.toISOString() });
|
|
375
|
+
} else {
|
|
376
|
+
rangeRules[0] = { field: startDateField.slug, operator: '>=', value: visibleRange.start.toISOString() };
|
|
377
|
+
rangeRules.push({ field: startDateField.slug, operator: '<=', value: visibleRange.end.toISOString() });
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
const baseRules = (baseFilters as { rules?: Array<unknown> } | undefined)?.rules ?? [];
|
|
381
|
+
|
|
382
|
+
filters = {
|
|
383
|
+
combinator: 'and',
|
|
384
|
+
rules: [...baseRules, ...rangeRules]
|
|
385
|
+
} as typeof baseFilters;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
return {
|
|
389
|
+
filters,
|
|
390
|
+
orderBy: dataGrid.resolvedListParams.orderBy,
|
|
391
|
+
limit: dataGrid.resolvedListParams.limit,
|
|
392
|
+
offset: dataGrid.resolvedListParams.offset,
|
|
393
|
+
columns: requestedColumns
|
|
394
|
+
};
|
|
395
|
+
}, [
|
|
396
|
+
dataGrid.resolvedListParams,
|
|
397
|
+
requestedColumns,
|
|
398
|
+
enableDateRangeFilter,
|
|
399
|
+
visibleRange,
|
|
400
|
+
startDateField,
|
|
401
|
+
endDateField
|
|
402
|
+
]);
|
|
403
|
+
|
|
404
|
+
/* ---- items query ----------------------------------------------------- */
|
|
405
|
+
|
|
406
|
+
const itemsQuery = useQuery<Array<TData>>({
|
|
407
|
+
queryKey: [
|
|
408
|
+
'kiva',
|
|
409
|
+
'kivaCalendarItems',
|
|
410
|
+
appSlug,
|
|
411
|
+
dataSourceSlug,
|
|
412
|
+
collection ? 'collection' : 'direct',
|
|
413
|
+
resolvedListParams
|
|
414
|
+
],
|
|
415
|
+
queryFn: async () => {
|
|
416
|
+
if (collection) {
|
|
417
|
+
const result = await collection.list(resolvedListParams);
|
|
418
|
+
|
|
419
|
+
return unwrapItems<TData>(result);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
const response = await client.get<
|
|
423
|
+
Array<TData> | { data: Array<TData> }
|
|
424
|
+
>(
|
|
425
|
+
`/v1/apps/${appSlug}/data-sources/${dataSourceSlug}/items`,
|
|
426
|
+
resolvedListParams as Parameters<typeof client.get>[1]
|
|
427
|
+
);
|
|
428
|
+
|
|
429
|
+
return unwrapItems<TData>(response);
|
|
430
|
+
},
|
|
431
|
+
enabled: Boolean(startDateField) && Boolean(dataGrid.dataSource) && !dataGrid.isLoading,
|
|
432
|
+
staleTime
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
const items = useMemo<Array<TData>>(
|
|
436
|
+
() => itemsQuery.data ?? [],
|
|
437
|
+
[itemsQuery.data]
|
|
438
|
+
);
|
|
439
|
+
|
|
440
|
+
/* ---- numeric id ↔ UUID mapping --------------------------------------- */
|
|
441
|
+
|
|
442
|
+
const idMapRef = useRef(new Map<TEventId, string>());
|
|
443
|
+
const reverseIdMapRef = useRef(new Map<string, TEventId>());
|
|
444
|
+
|
|
445
|
+
/* ---- event transformation -------------------------------------------- */
|
|
446
|
+
|
|
447
|
+
const defaultDurationMs = defaultEventDurationMinutes * 60 * 1000;
|
|
448
|
+
|
|
449
|
+
const events = useMemo<Array<KivaCalendarEvent<TData>>>(() => {
|
|
450
|
+
if (!startDateField) return [];
|
|
451
|
+
|
|
452
|
+
idMapRef.current.clear();
|
|
453
|
+
reverseIdMapRef.current.clear();
|
|
454
|
+
|
|
455
|
+
const result: Array<KivaCalendarEvent<TData>> = [];
|
|
456
|
+
let numericId = 1;
|
|
457
|
+
|
|
458
|
+
items.forEach((item, index) => {
|
|
459
|
+
const itemRecord = item as Record<string, unknown>;
|
|
460
|
+
const recordId = String(itemRecord.id ?? `${index}`);
|
|
461
|
+
const context: KivaCalendarEventBuilderContext<TData> = {
|
|
462
|
+
item,
|
|
463
|
+
index,
|
|
464
|
+
startDateField,
|
|
465
|
+
endDateField,
|
|
466
|
+
titleField,
|
|
467
|
+
colorField,
|
|
468
|
+
descriptionField,
|
|
469
|
+
userField
|
|
470
|
+
};
|
|
471
|
+
|
|
472
|
+
const event = getEvent ? getEvent(item, context) : buildDefaultEvent(item, context, colorMap, defaultDurationMs, numericId);
|
|
473
|
+
|
|
474
|
+
if (!event) return;
|
|
475
|
+
|
|
476
|
+
idMapRef.current.set(event.id, recordId);
|
|
477
|
+
reverseIdMapRef.current.set(recordId, event.id);
|
|
478
|
+
result.push(event);
|
|
479
|
+
numericId++;
|
|
480
|
+
});
|
|
481
|
+
|
|
482
|
+
return result;
|
|
483
|
+
}, [
|
|
484
|
+
items,
|
|
485
|
+
startDateField,
|
|
486
|
+
endDateField,
|
|
487
|
+
titleField,
|
|
488
|
+
colorField,
|
|
489
|
+
descriptionField,
|
|
490
|
+
userField,
|
|
491
|
+
colorMap,
|
|
492
|
+
defaultDurationMs,
|
|
493
|
+
getEvent
|
|
494
|
+
]);
|
|
495
|
+
|
|
496
|
+
/* ---- unique users ---------------------------------------------------- */
|
|
497
|
+
|
|
498
|
+
const users = useMemo<Array<IUser>>(() => {
|
|
499
|
+
const seen = new Set<string>();
|
|
500
|
+
const result: Array<IUser> = [];
|
|
501
|
+
|
|
502
|
+
events.forEach((event) => {
|
|
503
|
+
if (event.user && !seen.has(event.user.id)) {
|
|
504
|
+
seen.add(event.user.id);
|
|
505
|
+
result.push(event.user);
|
|
506
|
+
}
|
|
507
|
+
});
|
|
508
|
+
|
|
509
|
+
return result;
|
|
510
|
+
}, [events]);
|
|
511
|
+
|
|
512
|
+
/* ---- filter items (generic user list for CalendarProvider) ----------- */
|
|
513
|
+
|
|
514
|
+
const filterItems = useMemo<Array<CalendarFilterItem>>(
|
|
515
|
+
() => users.map(user => ({
|
|
516
|
+
id: user.id,
|
|
517
|
+
label: user.name,
|
|
518
|
+
image: user.picturePath
|
|
519
|
+
})),
|
|
520
|
+
[users]
|
|
521
|
+
);
|
|
522
|
+
|
|
523
|
+
/* ---- CRUD helpers ---------------------------------------------------- */
|
|
524
|
+
|
|
525
|
+
const reload = useCallback(() => {
|
|
526
|
+
dataGrid.refetch();
|
|
527
|
+
void itemsQuery.refetch();
|
|
528
|
+
}, [dataGrid, itemsQuery]);
|
|
529
|
+
|
|
530
|
+
const createRecord = useCallback(async (
|
|
531
|
+
startDate: Date,
|
|
532
|
+
endDate: Date,
|
|
533
|
+
extraValues: Record<string, unknown> = {}
|
|
534
|
+
) => {
|
|
535
|
+
if (!startDateField) return;
|
|
536
|
+
|
|
537
|
+
const data: Record<string, unknown> = {
|
|
538
|
+
...extraValues,
|
|
539
|
+
[startDateField.slug]: startDate.toISOString()
|
|
540
|
+
};
|
|
541
|
+
|
|
542
|
+
if (endDateField) {
|
|
543
|
+
data[endDateField.slug] = endDate.toISOString();
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
if (collection?.create) {
|
|
547
|
+
await collection.create(data);
|
|
548
|
+
} else {
|
|
549
|
+
await client.post(
|
|
550
|
+
`/v1/apps/${appSlug}/data-sources/${dataSourceSlug}/items`,
|
|
551
|
+
data
|
|
552
|
+
);
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
reload();
|
|
556
|
+
}, [
|
|
557
|
+
startDateField,
|
|
558
|
+
endDateField,
|
|
559
|
+
collection,
|
|
560
|
+
client,
|
|
561
|
+
appSlug,
|
|
562
|
+
dataSourceSlug,
|
|
563
|
+
reload
|
|
564
|
+
]);
|
|
565
|
+
|
|
566
|
+
const updateRecord = useCallback(async (
|
|
567
|
+
recordId: string,
|
|
568
|
+
data: Record<string, unknown>
|
|
569
|
+
) => {
|
|
570
|
+
if (collection?.update) {
|
|
571
|
+
await collection.update(recordId, data);
|
|
572
|
+
} else {
|
|
573
|
+
await client.patch(
|
|
574
|
+
`/v1/apps/${appSlug}/data-sources/${dataSourceSlug}/items/${recordId}`,
|
|
575
|
+
data as Parameters<typeof client.patch>[1]
|
|
576
|
+
);
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
reload();
|
|
580
|
+
}, [
|
|
581
|
+
collection,
|
|
582
|
+
client,
|
|
583
|
+
appSlug,
|
|
584
|
+
dataSourceSlug,
|
|
585
|
+
reload
|
|
586
|
+
]);
|
|
587
|
+
|
|
588
|
+
const deleteRecord = useCallback(async (recordId: string) => {
|
|
589
|
+
if (collection?.delete) {
|
|
590
|
+
await collection.delete(recordId);
|
|
591
|
+
} else {
|
|
592
|
+
await client.delete(
|
|
593
|
+
`/v1/apps/${appSlug}/data-sources/${dataSourceSlug}/items/${recordId}`
|
|
594
|
+
);
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
reload();
|
|
598
|
+
}, [
|
|
599
|
+
collection,
|
|
600
|
+
client,
|
|
601
|
+
appSlug,
|
|
602
|
+
dataSourceSlug,
|
|
603
|
+
reload
|
|
604
|
+
]);
|
|
605
|
+
|
|
606
|
+
const moveRecord = useCallback(async (
|
|
607
|
+
recordId: string,
|
|
608
|
+
newStart: Date,
|
|
609
|
+
newEnd: Date
|
|
610
|
+
) => {
|
|
611
|
+
if (!startDateField) return;
|
|
612
|
+
|
|
613
|
+
const data: Record<string, unknown> = {
|
|
614
|
+
[startDateField.slug]: newStart.toISOString()
|
|
615
|
+
};
|
|
616
|
+
|
|
617
|
+
if (endDateField) {
|
|
618
|
+
data[endDateField.slug] = newEnd.toISOString();
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
await updateRecord(recordId, data);
|
|
622
|
+
}, [startDateField, endDateField, updateRecord]);
|
|
623
|
+
|
|
624
|
+
/* ---- calendarProviderProps ------------------------------------------ */
|
|
625
|
+
|
|
626
|
+
const calendarProviderProps = useMemo<Omit<CalendarProviderProps, 'children'>>(
|
|
627
|
+
() => ({
|
|
628
|
+
events,
|
|
629
|
+
users,
|
|
630
|
+
defaultView,
|
|
631
|
+
filterItems,
|
|
632
|
+
timelineMaxLanes,
|
|
633
|
+
defaultYearTimelineMode,
|
|
634
|
+
defaultQuarterMode,
|
|
635
|
+
/*
|
|
636
|
+
* Navigating a period refetches; overlay the body instead of swapping in
|
|
637
|
+
* a skeleton, which would unmount the provider and reset view + date.
|
|
638
|
+
*/
|
|
639
|
+
isRefreshing: itemsQuery.isFetching,
|
|
640
|
+
onEventCreate: async (event) => {
|
|
641
|
+
const recordId = idMapRef.current.get(event.id);
|
|
642
|
+
|
|
643
|
+
if (!recordId || !startDateField) return;
|
|
644
|
+
await updateRecord(recordId, buildEventPatch(event, startDateField, endDateField));
|
|
645
|
+
},
|
|
646
|
+
onEventUpdate: async (event) => {
|
|
647
|
+
const recordId = idMapRef.current.get(event.id);
|
|
648
|
+
|
|
649
|
+
if (!recordId || !startDateField) return;
|
|
650
|
+
await updateRecord(recordId, buildEventPatch(event, startDateField, endDateField));
|
|
651
|
+
},
|
|
652
|
+
onEventDelete: async (eventId) => {
|
|
653
|
+
const recordId = idMapRef.current.get(eventId);
|
|
654
|
+
|
|
655
|
+
if (!recordId) return;
|
|
656
|
+
await deleteRecord(recordId);
|
|
657
|
+
},
|
|
658
|
+
onEventDrop: async (event, newStart, newEnd) => {
|
|
659
|
+
const recordId = idMapRef.current.get(event.id);
|
|
660
|
+
|
|
661
|
+
if (!recordId) return;
|
|
662
|
+
await moveRecord(recordId, newStart, newEnd);
|
|
663
|
+
}
|
|
664
|
+
}),
|
|
665
|
+
[
|
|
666
|
+
events,
|
|
667
|
+
users,
|
|
668
|
+
defaultView,
|
|
669
|
+
filterItems,
|
|
670
|
+
timelineMaxLanes,
|
|
671
|
+
defaultYearTimelineMode,
|
|
672
|
+
defaultQuarterMode,
|
|
673
|
+
itemsQuery.isFetching,
|
|
674
|
+
startDateField,
|
|
675
|
+
endDateField,
|
|
676
|
+
updateRecord,
|
|
677
|
+
deleteRecord,
|
|
678
|
+
moveRecord
|
|
679
|
+
]
|
|
680
|
+
);
|
|
681
|
+
|
|
682
|
+
/* ---- loading / error ------------------------------------------------- */
|
|
683
|
+
|
|
684
|
+
const isLoading = dataGrid.isLoading || itemsQuery.isLoading;
|
|
685
|
+
const isFetching = dataGrid.isLoading || itemsQuery.isFetching;
|
|
686
|
+
/*
|
|
687
|
+
* `hasLoadedOnce` flips to `true` after the first successful items fetch
|
|
688
|
+
* and stays true for the lifetime of the hook. Consumers use it to
|
|
689
|
+
* distinguish the very first load (where a skeleton makes sense) from
|
|
690
|
+
* subsequent refetches (where unmounting the calendar tree would lose
|
|
691
|
+
* its internal `selectedDate` / `view` state).
|
|
692
|
+
*/
|
|
693
|
+
const hasLoadedOnceRef = useRef(false);
|
|
694
|
+
|
|
695
|
+
if (!isLoading && itemsQuery.data !== undefined) {
|
|
696
|
+
hasLoadedOnceRef.current = true;
|
|
697
|
+
}
|
|
698
|
+
const hasLoadedOnce = hasLoadedOnceRef.current;
|
|
699
|
+
const error = (dataGrid.error ?? (itemsQuery.error as Error | null)) ?? null;
|
|
700
|
+
|
|
701
|
+
return {
|
|
702
|
+
table: dataGrid.table,
|
|
703
|
+
toolbar: dataGrid.toolbar,
|
|
704
|
+
views: dataGrid.views,
|
|
705
|
+
fields: dataGrid.fields,
|
|
706
|
+
dataSource: dataGrid.dataSource,
|
|
707
|
+
activeViewId: dataGrid.activeViewId,
|
|
708
|
+
setActiveViewId: dataGrid.setActiveViewId,
|
|
709
|
+
events,
|
|
710
|
+
users,
|
|
711
|
+
items,
|
|
712
|
+
startDateField,
|
|
713
|
+
endDateField,
|
|
714
|
+
titleField,
|
|
715
|
+
colorField,
|
|
716
|
+
descriptionField,
|
|
717
|
+
userField,
|
|
718
|
+
hasDateField: Boolean(startDateField),
|
|
719
|
+
requestedColumns,
|
|
720
|
+
resolvedListParams,
|
|
721
|
+
calendarProviderProps,
|
|
722
|
+
filterItems,
|
|
723
|
+
createRecord,
|
|
724
|
+
updateRecord,
|
|
725
|
+
deleteRecord,
|
|
726
|
+
moveRecord,
|
|
727
|
+
visibleRange,
|
|
728
|
+
setVisibleRange,
|
|
729
|
+
isLoading,
|
|
730
|
+
isFetching,
|
|
731
|
+
hasLoadedOnce,
|
|
732
|
+
error,
|
|
733
|
+
reload
|
|
734
|
+
};
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
/*
|
|
738
|
+
* -------------------------------------------------------------------------
|
|
739
|
+
* Internal helpers
|
|
740
|
+
* ----------------------------------------------------------------------
|
|
741
|
+
*/
|
|
742
|
+
|
|
743
|
+
function buildDefaultEvent<TData>(
|
|
744
|
+
item: TData,
|
|
745
|
+
context: KivaCalendarEventBuilderContext<TData>,
|
|
746
|
+
colorMap: Record<string, TEventColor> | undefined,
|
|
747
|
+
defaultDurationMs: number,
|
|
748
|
+
numericId: number
|
|
749
|
+
): KivaCalendarEvent<TData> | null {
|
|
750
|
+
const itemRecord = item as Record<string, unknown>;
|
|
751
|
+
const recordId = String(itemRecord.id ?? `${context.index}`);
|
|
752
|
+
|
|
753
|
+
const rawStart = context.startDateField ? itemRecord[context.startDateField.slug] : null;
|
|
754
|
+
|
|
755
|
+
if (!rawStart) return null;
|
|
756
|
+
|
|
757
|
+
const startDate = new Date(rawStart as string);
|
|
758
|
+
|
|
759
|
+
if (isNaN(startDate.getTime())) return null;
|
|
760
|
+
|
|
761
|
+
let endDate: Date;
|
|
762
|
+
|
|
763
|
+
if (context.endDateField) {
|
|
764
|
+
const rawEnd = itemRecord[context.endDateField.slug];
|
|
765
|
+
const parsed = rawEnd ? new Date(rawEnd as string) : null;
|
|
766
|
+
|
|
767
|
+
endDate = parsed && !isNaN(parsed.getTime()) && parsed > startDate ? parsed : new Date(startDate.getTime() + defaultDurationMs);
|
|
768
|
+
} else {
|
|
769
|
+
endDate = new Date(startDate.getTime() + defaultDurationMs);
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
const title = context.titleField ? String(resolveDisplayValue(itemRecord[context.titleField.slug]) ?? `Event ${context.index + 1}`) : `Event ${context.index + 1}`;
|
|
773
|
+
|
|
774
|
+
const description = context.descriptionField ? String(resolveDisplayValue(itemRecord[context.descriptionField.slug]) ?? '') : '';
|
|
775
|
+
|
|
776
|
+
const rawColor = context.colorField ? resolveDisplayValue(itemRecord[context.colorField.slug]) : null;
|
|
777
|
+
|
|
778
|
+
const color = normalizeColor(rawColor, colorMap);
|
|
779
|
+
|
|
780
|
+
const user = context.userField ? resolveUser(itemRecord[context.userField.slug]) : null;
|
|
781
|
+
|
|
782
|
+
return {
|
|
783
|
+
id: numericId,
|
|
784
|
+
recordId,
|
|
785
|
+
startDate: startDate.toISOString(),
|
|
786
|
+
endDate: endDate.toISOString(),
|
|
787
|
+
title,
|
|
788
|
+
description,
|
|
789
|
+
color,
|
|
790
|
+
user: user ?? { id: 'unknown', name: '', picturePath: null },
|
|
791
|
+
item
|
|
792
|
+
};
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
function buildEventPatch(
|
|
796
|
+
event: IEvent,
|
|
797
|
+
startDateField: DataSourceField,
|
|
798
|
+
endDateField: DataSourceField | null
|
|
799
|
+
): Record<string, unknown> {
|
|
800
|
+
const patch: Record<string, unknown> = {
|
|
801
|
+
[startDateField.slug]: event.startDate
|
|
802
|
+
};
|
|
803
|
+
|
|
804
|
+
if (endDateField) {
|
|
805
|
+
patch[endDateField.slug] = event.endDate;
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
if (event.title) {
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
return patch;
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
function normalizeColor(
|
|
815
|
+
value: unknown,
|
|
816
|
+
colorMap?: Record<string, TEventColor>
|
|
817
|
+
): TEventColor {
|
|
818
|
+
if (!value) return 'blue';
|
|
819
|
+
|
|
820
|
+
const str = String(value).toLowerCase().trim();
|
|
821
|
+
|
|
822
|
+
if (colorMap) {
|
|
823
|
+
const mapped = colorMap[str] ?? colorMap[String(value).trim()];
|
|
824
|
+
|
|
825
|
+
if (mapped) return mapped;
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
if (VALID_COLORS.has(str as TEventColor)) {
|
|
829
|
+
return str as TEventColor;
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
return 'blue';
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
function resolveUser(value: unknown): IUser | null {
|
|
836
|
+
if (!value || typeof value !== 'object') return null;
|
|
837
|
+
|
|
838
|
+
const rec = value as Record<string, unknown>;
|
|
839
|
+
const id = String(rec.id ?? rec.user_id ?? rec.userId ?? '');
|
|
840
|
+
const name = String(rec.name ?? rec.full_name ?? rec.fullName ?? rec.email ?? '');
|
|
841
|
+
const picturePath = (rec.picture_url ?? rec.pictureUrl ?? rec.avatar ?? null) as string | null;
|
|
842
|
+
|
|
843
|
+
if (!id) return null;
|
|
844
|
+
|
|
845
|
+
return { id, name, picturePath };
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
function resolveDisplayValue(value: unknown): string | null {
|
|
849
|
+
if (value == null) return null;
|
|
850
|
+
if (typeof value === 'string') return value || null;
|
|
851
|
+
if (typeof value === 'number') return String(value);
|
|
852
|
+
|
|
853
|
+
if (typeof value === 'object') {
|
|
854
|
+
const rec = value as Record<string, unknown>;
|
|
855
|
+
|
|
856
|
+
for (const key of [
|
|
857
|
+
'name',
|
|
858
|
+
'label',
|
|
859
|
+
'title',
|
|
860
|
+
'value'
|
|
861
|
+
]) {
|
|
862
|
+
if (typeof rec[key] === 'string' && rec[key]) return rec[key] as string;
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
return null;
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
function resolveStartDateField(
|
|
870
|
+
fields: Array<DataSourceField> | undefined,
|
|
871
|
+
slug?: string
|
|
872
|
+
): DataSourceField | null {
|
|
873
|
+
if (!fields || fields.length === 0) return null;
|
|
874
|
+
if (slug) return fields.find(f => f.slug === slug) ?? null;
|
|
875
|
+
|
|
876
|
+
return fields.find(f => DATE_FIELD_TYPES.has(f.type)) ?? null;
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
function resolveEndDateField(
|
|
880
|
+
fields: Array<DataSourceField> | undefined,
|
|
881
|
+
slug: string | undefined,
|
|
882
|
+
startSlug: string | undefined
|
|
883
|
+
): DataSourceField | null {
|
|
884
|
+
if (!fields || fields.length === 0) return null;
|
|
885
|
+
if (slug) return fields.find(f => f.slug === slug) ?? null;
|
|
886
|
+
|
|
887
|
+
const endCandidates = [
|
|
888
|
+
'end_date',
|
|
889
|
+
'end_time',
|
|
890
|
+
'end_datetime',
|
|
891
|
+
'finish_date',
|
|
892
|
+
'due_date',
|
|
893
|
+
'deadline',
|
|
894
|
+
'ends_at',
|
|
895
|
+
'ended_at'
|
|
896
|
+
];
|
|
897
|
+
|
|
898
|
+
for (const candidate of endCandidates) {
|
|
899
|
+
const match = fields.find(f => f.slug === candidate && f.slug !== startSlug);
|
|
900
|
+
|
|
901
|
+
if (match) return match;
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
return fields.find(f => DATE_FIELD_TYPES.has(f.type) && f.slug !== startSlug) ?? null;
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
function resolveTitleField(
|
|
908
|
+
fields: Array<DataSourceField> | undefined,
|
|
909
|
+
slug?: string
|
|
910
|
+
): DataSourceField | null {
|
|
911
|
+
if (!fields || fields.length === 0) return null;
|
|
912
|
+
if (slug) return fields.find(f => f.slug === slug) ?? null;
|
|
913
|
+
|
|
914
|
+
for (const candidate of TITLE_FIELD_CANDIDATES) {
|
|
915
|
+
const match = fields.find(f => f.slug === candidate);
|
|
916
|
+
|
|
917
|
+
if (match) return match;
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
return null;
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
function resolveColorField(
|
|
924
|
+
fields: Array<DataSourceField> | undefined,
|
|
925
|
+
slug?: string
|
|
926
|
+
): DataSourceField | null {
|
|
927
|
+
if (!fields || fields.length === 0) return null;
|
|
928
|
+
if (slug) return fields.find(f => f.slug === slug) ?? null;
|
|
929
|
+
|
|
930
|
+
return fields.find(f => f.slug === 'color' || f.slug === 'colour') ?? null;
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
function resolveDescriptionField(
|
|
934
|
+
fields: Array<DataSourceField> | undefined,
|
|
935
|
+
slug: string | undefined,
|
|
936
|
+
titleSlug: string | undefined
|
|
937
|
+
): DataSourceField | null {
|
|
938
|
+
if (!fields || fields.length === 0) return null;
|
|
939
|
+
if (slug) return fields.find(f => f.slug === slug) ?? null;
|
|
940
|
+
|
|
941
|
+
const fieldMap = new Map(fields.map(f => [f.slug, f]));
|
|
942
|
+
|
|
943
|
+
for (const candidate of DESCRIPTION_FIELD_CANDIDATES) {
|
|
944
|
+
const match = fieldMap.get(candidate);
|
|
945
|
+
|
|
946
|
+
if (match && match.slug !== titleSlug) return match;
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
return null;
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
function resolveUserField(
|
|
953
|
+
fields: Array<DataSourceField> | undefined,
|
|
954
|
+
slug?: string
|
|
955
|
+
): DataSourceField | null {
|
|
956
|
+
if (!fields || fields.length === 0) return null;
|
|
957
|
+
if (slug) return fields.find(f => f.slug === slug) ?? null;
|
|
958
|
+
|
|
959
|
+
return (
|
|
960
|
+
fields.find(f => USER_FIELD_TYPES.has(f.type))
|
|
961
|
+
?? fields.find(f => f.slug === 'record_owner')
|
|
962
|
+
?? null
|
|
963
|
+
);
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
function unwrapItems<TData>(
|
|
967
|
+
value: Array<TData> | { data: Array<TData> } | unknown
|
|
968
|
+
): Array<TData> {
|
|
969
|
+
if (Array.isArray(value)) return value;
|
|
970
|
+
|
|
971
|
+
if (value && typeof value === 'object' && Array.isArray((value as { data?: unknown }).data)) {
|
|
972
|
+
return (value as { data: Array<TData> }).data;
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
return [];
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
/*
|
|
979
|
+
* -------------------------------------------------------------------------
|
|
980
|
+
* Re-export convenience types
|
|
981
|
+
* ----------------------------------------------------------------------
|
|
982
|
+
*/
|
|
983
|
+
|
|
984
|
+
export type {
|
|
985
|
+
IEvent,
|
|
986
|
+
IUser,
|
|
987
|
+
TCalendarView,
|
|
988
|
+
TEventColor,
|
|
989
|
+
CalendarFilterItem
|
|
990
|
+
} from '@kivacore/ui-pro-shared/components/kiva/calendar';
|
|
991
|
+
|
|
992
|
+
export type { ReactNode };
|