@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,159 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
// @ts-nocheck
|
|
4
|
+
/* eslint-disable */
|
|
5
|
+
import { useMemo } from 'react';
|
|
6
|
+
|
|
7
|
+
import { addDays, isValid, parseISO } from 'date-fns';
|
|
8
|
+
|
|
9
|
+
import { cn } from '@kivacore/ui-pro-shared/lib/utils';
|
|
10
|
+
|
|
11
|
+
import { useUiTranslation } from '@kivacore/ui-pro-shared/hooks/kiva/use-ui-translation';
|
|
12
|
+
|
|
13
|
+
import { type IEvent } from '../../interfaces';
|
|
14
|
+
|
|
15
|
+
import { useCalendar } from '../../contexts/calendar-context';
|
|
16
|
+
import { useDragDrop } from '../../contexts/dnd-context';
|
|
17
|
+
import { DroppableArea } from '../../dnd/droppable-area';
|
|
18
|
+
|
|
19
|
+
import { type TimelineColumn, type TimelineRow, type TimelineSection } from './lib/timeline-model';
|
|
20
|
+
import { layoutRowEvents } from './lib/timeline-layout';
|
|
21
|
+
import { TimelineEventBar } from './timeline-event-bar';
|
|
22
|
+
import { TimelineDayLabelStrip, TimelineGridHeader } from './timeline-grid-header';
|
|
23
|
+
|
|
24
|
+
const LANE_HEIGHT = 22;
|
|
25
|
+
const LANE_GAP = 4;
|
|
26
|
+
const BAND_PADDING = 6;
|
|
27
|
+
const CONTROL_SPACE = 26;
|
|
28
|
+
|
|
29
|
+
interface TimelineRowViewProps {
|
|
30
|
+
row: TimelineRow;
|
|
31
|
+
section: TimelineSection;
|
|
32
|
+
events: Array<IEvent>;
|
|
33
|
+
maxLanes: number;
|
|
34
|
+
expanded: boolean;
|
|
35
|
+
onToggleExpanded: (rowKey: string) => void;
|
|
36
|
+
gutterWidth: number;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function TimelineRowView({
|
|
40
|
+
row,
|
|
41
|
+
section,
|
|
42
|
+
events,
|
|
43
|
+
maxLanes,
|
|
44
|
+
expanded,
|
|
45
|
+
onToggleExpanded,
|
|
46
|
+
gutterWidth
|
|
47
|
+
}: TimelineRowViewProps) {
|
|
48
|
+
const { weekStartsOn, onDaySelect } = useCalendar();
|
|
49
|
+
const { isDragging, draggedEvent } = useDragDrop();
|
|
50
|
+
const { t } = useUiTranslation();
|
|
51
|
+
|
|
52
|
+
const layout = useMemo(
|
|
53
|
+
() => layoutRowEvents(events, row, { maxLanes, expanded }),
|
|
54
|
+
[
|
|
55
|
+
events,
|
|
56
|
+
row,
|
|
57
|
+
maxLanes,
|
|
58
|
+
expanded
|
|
59
|
+
]
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
const showControl = layout.hiddenCount > 0 || (expanded && layout.lanes.length > maxLanes);
|
|
63
|
+
/*
|
|
64
|
+
* Sections without a two-level header (13 weeks) get a per-row date strip
|
|
65
|
+
* above the band, so the bars start below the dates instead of covering them.
|
|
66
|
+
*/
|
|
67
|
+
const showDayLabels = !section.perRowHeader;
|
|
68
|
+
const lanePitch = LANE_HEIGHT + LANE_GAP;
|
|
69
|
+
const laneCount = Math.max(layout.visibleLanes.length, 1);
|
|
70
|
+
const minBandHeight = section.variant === 'weeks' ? 78 : 64;
|
|
71
|
+
const bandHeight = Math.max(
|
|
72
|
+
minBandHeight,
|
|
73
|
+
BAND_PADDING * 2 + laneCount * lanePitch + (showControl ? CONTROL_SPACE : 0)
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
/*
|
|
77
|
+
* Week columns can't express a weekday, so a bar dropped on a week keeps the
|
|
78
|
+
* weekday it started on instead of jumping to the start of the week.
|
|
79
|
+
*/
|
|
80
|
+
const dropDateFor = (column: TimelineColumn): Date => {
|
|
81
|
+
if (row.granularity === 'day' || !draggedEvent) return column.start;
|
|
82
|
+
|
|
83
|
+
const originalStart = parseISO(draggedEvent.startDate);
|
|
84
|
+
|
|
85
|
+
if (!isValid(originalStart)) return column.start;
|
|
86
|
+
|
|
87
|
+
return addDays(column.start, (originalStart.getDay() - weekStartsOn + 7) % 7);
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
return (
|
|
91
|
+
<div className={cn('flex border-b last:border-b-0', row.containsToday && 'bg-primary/2')}>
|
|
92
|
+
<div
|
|
93
|
+
className="shrink-0 border-r px-3 py-2"
|
|
94
|
+
style={{ width: gutterWidth }}>
|
|
95
|
+
<p className="text-sm font-semibold">{row.label}</p>
|
|
96
|
+
{row.subLabel && (
|
|
97
|
+
<p className="text-xs text-muted-foreground">{row.subLabel}</p>
|
|
98
|
+
)}
|
|
99
|
+
</div>
|
|
100
|
+
|
|
101
|
+
<div className="min-w-0 flex-1">
|
|
102
|
+
{section.perRowHeader && <TimelineGridHeader groups={row.groups} />}
|
|
103
|
+
{showDayLabels && <TimelineDayLabelStrip groups={row.groups} />}
|
|
104
|
+
|
|
105
|
+
<div className="relative" style={{ height: bandHeight }}>
|
|
106
|
+
<div
|
|
107
|
+
className={cn(
|
|
108
|
+
'absolute inset-0 flex',
|
|
109
|
+
isDragging ? 'z-20' : 'z-0'
|
|
110
|
+
)}>
|
|
111
|
+
{row.groups.map(group => (
|
|
112
|
+
<div
|
|
113
|
+
key={group.key}
|
|
114
|
+
className="flex min-w-0 border-r last:border-r-0"
|
|
115
|
+
style={{ flexGrow: group.weight, flexBasis: 0 }}>
|
|
116
|
+
{group.columns.map(column => (
|
|
117
|
+
<DroppableArea
|
|
118
|
+
key={column.key}
|
|
119
|
+
date={dropDateFor(column)}
|
|
120
|
+
className={cn(
|
|
121
|
+
'min-w-0 basis-0 grow border-r last:border-r-0',
|
|
122
|
+
column.weekend && 'bg-muted/20',
|
|
123
|
+
column.containsToday && 'bg-primary/5',
|
|
124
|
+
showDayLabels && column.monthStart && 'border-l-2 border-l-foreground/25'
|
|
125
|
+
)}>
|
|
126
|
+
<button
|
|
127
|
+
type="button"
|
|
128
|
+
aria-label={column.start.toDateString()}
|
|
129
|
+
className="size-full cursor-default"
|
|
130
|
+
onClick={() => onDaySelect?.(column.start)} />
|
|
131
|
+
</DroppableArea>
|
|
132
|
+
))}
|
|
133
|
+
</div>
|
|
134
|
+
))}
|
|
135
|
+
</div>
|
|
136
|
+
|
|
137
|
+
<div className={cn('absolute inset-0 z-10', isDragging && 'pointer-events-none')}>
|
|
138
|
+
{layout.visibleLanes.map((lane, laneIndex) => lane.map(segment => (
|
|
139
|
+
<TimelineEventBar
|
|
140
|
+
key={segment.key}
|
|
141
|
+
segment={segment}
|
|
142
|
+
top={BAND_PADDING + laneIndex * lanePitch}
|
|
143
|
+
height={LANE_HEIGHT} />
|
|
144
|
+
)))}
|
|
145
|
+
</div>
|
|
146
|
+
|
|
147
|
+
{showControl && (
|
|
148
|
+
<button
|
|
149
|
+
type="button"
|
|
150
|
+
className="absolute bottom-1 left-1 z-30 rounded-md border bg-background px-2 py-0.5 text-xs font-medium shadow-sm hover:bg-accent"
|
|
151
|
+
onClick={() => onToggleExpanded(row.key)}>
|
|
152
|
+
{expanded ? t('ui.calendar.showLess', 'Show less') : `+${layout.hiddenCount} ${t('ui.calendar.more', 'more')}`}
|
|
153
|
+
</button>
|
|
154
|
+
)}
|
|
155
|
+
</div>
|
|
156
|
+
</div>
|
|
157
|
+
</div>
|
|
158
|
+
);
|
|
159
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
// @ts-nocheck
|
|
4
|
+
/* eslint-disable */
|
|
5
|
+
import { useUiTranslation } from '@kivacore/ui-pro-shared/hooks/kiva/use-ui-translation';
|
|
6
|
+
|
|
7
|
+
import { type IEvent } from '../../interfaces';
|
|
8
|
+
|
|
9
|
+
import { type TimelineSection } from './lib/timeline-model';
|
|
10
|
+
import { TimelineRowView } from './timeline-row';
|
|
11
|
+
import { TimelineSharedHeader } from './timeline-grid-header';
|
|
12
|
+
|
|
13
|
+
interface TimelineSectionViewProps {
|
|
14
|
+
section: TimelineSection;
|
|
15
|
+
events: Array<IEvent>;
|
|
16
|
+
maxLanes: number;
|
|
17
|
+
expandedRows: Set<string>;
|
|
18
|
+
onToggleRow: (rowKey: string) => void;
|
|
19
|
+
gutterWidth: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function TimelineSectionView({
|
|
23
|
+
section,
|
|
24
|
+
events,
|
|
25
|
+
maxLanes,
|
|
26
|
+
expandedRows,
|
|
27
|
+
onToggleRow,
|
|
28
|
+
gutterWidth
|
|
29
|
+
}: TimelineSectionViewProps) {
|
|
30
|
+
const { t } = useUiTranslation();
|
|
31
|
+
|
|
32
|
+
return (
|
|
33
|
+
<section className="border-b last:border-b-0">
|
|
34
|
+
<header className="sticky top-0 z-40 flex items-baseline gap-2 border-b bg-muted/60 px-4 py-2 backdrop-blur">
|
|
35
|
+
<h3 className="text-sm font-semibold">{section.label}</h3>
|
|
36
|
+
{section.subLabel && (
|
|
37
|
+
<span className="text-xs text-muted-foreground">{section.subLabel}</span>
|
|
38
|
+
)}
|
|
39
|
+
</header>
|
|
40
|
+
|
|
41
|
+
{!section.perRowHeader && section.sharedHeader && (
|
|
42
|
+
<TimelineSharedHeader
|
|
43
|
+
labels={section.sharedHeader}
|
|
44
|
+
gutterLabel={t('ui.calendar.week', 'Week')}
|
|
45
|
+
gutterWidth={gutterWidth} />
|
|
46
|
+
)}
|
|
47
|
+
|
|
48
|
+
{section.rows.map(row => (
|
|
49
|
+
<TimelineRowView
|
|
50
|
+
key={row.key}
|
|
51
|
+
row={row}
|
|
52
|
+
section={section}
|
|
53
|
+
events={events}
|
|
54
|
+
maxLanes={maxLanes}
|
|
55
|
+
expanded={expandedRows.has(row.key)}
|
|
56
|
+
onToggleExpanded={onToggleRow}
|
|
57
|
+
gutterWidth={gutterWidth} />
|
|
58
|
+
))}
|
|
59
|
+
</section>
|
|
60
|
+
);
|
|
61
|
+
}
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
// @ts-nocheck
|
|
4
|
+
/* eslint-disable */
|
|
5
|
+
import {
|
|
6
|
+
useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState
|
|
7
|
+
} from 'react';
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
type TTimelineNavigationMode, type TTimelineView, type TWeekStartsOn
|
|
11
|
+
} from '../../types';
|
|
12
|
+
|
|
13
|
+
import {
|
|
14
|
+
type TimelineSection,
|
|
15
|
+
buildTimelineSection,
|
|
16
|
+
normalizeTimelineAnchor,
|
|
17
|
+
shiftTimelineAnchor,
|
|
18
|
+
supportsContinuousMode
|
|
19
|
+
} from './lib/timeline-model';
|
|
20
|
+
|
|
21
|
+
/** Periods rendered ahead of the anchor when a continuous view first mounts. */
|
|
22
|
+
const CONTINUOUS_INITIAL_AFTER = 2;
|
|
23
|
+
/** Hard cap on how far scrolling can grow the window in either direction. */
|
|
24
|
+
const MAX_EXTENSION = 12;
|
|
25
|
+
/** Delay before the grown window is reported, so a fast scroll fetches once. */
|
|
26
|
+
const REPORT_DEBOUNCE_MS = 150;
|
|
27
|
+
|
|
28
|
+
interface UseTimelineSectionsOptions {
|
|
29
|
+
view: TTimelineView;
|
|
30
|
+
/** The calendar's selected date — the period it lands in anchors the window. */
|
|
31
|
+
anchor: Date;
|
|
32
|
+
mode: TTimelineNavigationMode;
|
|
33
|
+
weekStartsOn: TWeekStartsOn;
|
|
34
|
+
today: Date;
|
|
35
|
+
/** Receives the rendered window; drives `onVisibleRangeChange` upstream. */
|
|
36
|
+
onRangeChange?: (range: { start: Date; end: Date } | null) => void;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface UseTimelineSectionsResult {
|
|
40
|
+
sections: Array<TimelineSection>;
|
|
41
|
+
range: { start: Date; end: Date } | null;
|
|
42
|
+
/** Attach to the view root — its scroll parent becomes the observer root. */
|
|
43
|
+
containerRef: (node: HTMLDivElement | null) => void;
|
|
44
|
+
/** Sentinels that append a period when scrolled into view (continuous only). */
|
|
45
|
+
startSentinelRef: (node: HTMLDivElement | null) => void;
|
|
46
|
+
endSentinelRef: (node: HTMLDivElement | null) => void;
|
|
47
|
+
isContinuous: boolean;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function getScrollParent(node: HTMLElement | null): HTMLElement | null {
|
|
51
|
+
let current = node?.parentElement ?? null;
|
|
52
|
+
|
|
53
|
+
while (current) {
|
|
54
|
+
const { overflowY } = window.getComputedStyle(current);
|
|
55
|
+
|
|
56
|
+
if (overflowY === 'auto' || overflowY === 'scroll') return current;
|
|
57
|
+
current = current.parentElement;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function useTimelineSections({
|
|
64
|
+
view,
|
|
65
|
+
anchor,
|
|
66
|
+
mode,
|
|
67
|
+
weekStartsOn,
|
|
68
|
+
today,
|
|
69
|
+
onRangeChange
|
|
70
|
+
}: UseTimelineSectionsOptions): UseTimelineSectionsResult {
|
|
71
|
+
const isContinuous = mode === 'continuous' && supportsContinuousMode(view);
|
|
72
|
+
const periodAnchor = useMemo(
|
|
73
|
+
() => normalizeTimelineAnchor(view, anchor, weekStartsOn),
|
|
74
|
+
[view, anchor, weekStartsOn]
|
|
75
|
+
);
|
|
76
|
+
const resetKey = `${view}|${isContinuous ? 'continuous' : 'single'}|${periodAnchor.getTime()}`;
|
|
77
|
+
|
|
78
|
+
const [window_, setWindow] = useState(() => ({
|
|
79
|
+
key: resetKey,
|
|
80
|
+
before: 0,
|
|
81
|
+
after: isContinuous ? CONTINUOUS_INITIAL_AFTER : 0
|
|
82
|
+
}));
|
|
83
|
+
|
|
84
|
+
/*
|
|
85
|
+
* Switching view / mode / period restarts the window. Assigning during render
|
|
86
|
+
* (React 19) keeps the first paint free of a stale multi-period window.
|
|
87
|
+
*/
|
|
88
|
+
if (window_.key !== resetKey) {
|
|
89
|
+
setWindow({
|
|
90
|
+
key: resetKey,
|
|
91
|
+
before: 0,
|
|
92
|
+
after: isContinuous ? CONTINUOUS_INITIAL_AFTER : 0
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const before = window_.key === resetKey ? window_.before : 0;
|
|
97
|
+
const after = window_.key === resetKey ? window_.after : (isContinuous ? CONTINUOUS_INITIAL_AFTER : 0);
|
|
98
|
+
|
|
99
|
+
const sections = useMemo(() => {
|
|
100
|
+
const offsets: Array<number> = [];
|
|
101
|
+
|
|
102
|
+
for (let offset = -before; offset <= after; offset += 1) offsets.push(offset);
|
|
103
|
+
|
|
104
|
+
return offsets.map(offset => buildTimelineSection(
|
|
105
|
+
view,
|
|
106
|
+
shiftTimelineAnchor(view, periodAnchor, offset, weekStartsOn),
|
|
107
|
+
weekStartsOn,
|
|
108
|
+
today
|
|
109
|
+
));
|
|
110
|
+
}, [
|
|
111
|
+
view,
|
|
112
|
+
periodAnchor,
|
|
113
|
+
weekStartsOn,
|
|
114
|
+
today,
|
|
115
|
+
before,
|
|
116
|
+
after
|
|
117
|
+
]);
|
|
118
|
+
|
|
119
|
+
const range = useMemo(() => {
|
|
120
|
+
const first = sections[0];
|
|
121
|
+
const last = sections[sections.length - 1];
|
|
122
|
+
|
|
123
|
+
if (!first || !last) return null;
|
|
124
|
+
|
|
125
|
+
return { start: first.start, end: last.end };
|
|
126
|
+
}, [sections]);
|
|
127
|
+
|
|
128
|
+
const reportRef = useRef(onRangeChange);
|
|
129
|
+
|
|
130
|
+
useEffect(() => {
|
|
131
|
+
reportRef.current = onRangeChange;
|
|
132
|
+
}, [onRangeChange]);
|
|
133
|
+
|
|
134
|
+
const scrollerRef = useRef<HTMLElement | null>(null);
|
|
135
|
+
|
|
136
|
+
const rangeKey = range ? `${range.start.getTime()}-${range.end.getTime()}` : '';
|
|
137
|
+
|
|
138
|
+
useEffect(() => {
|
|
139
|
+
if (!range) return undefined;
|
|
140
|
+
const timer = setTimeout(() => reportRef.current?.(range), REPORT_DEBOUNCE_MS);
|
|
141
|
+
|
|
142
|
+
return () => clearTimeout(timer);
|
|
143
|
+
/* `rangeKey` stands in for `range` so a new object with equal bounds is a no-op. */
|
|
144
|
+
}, [rangeKey, range]);
|
|
145
|
+
|
|
146
|
+
/*
|
|
147
|
+
* Prepending needs scroll compensation: the start sentinel is the first child
|
|
148
|
+
* and keeps intersecting after a prepend, so without this one navigation
|
|
149
|
+
* cascades to the extension cap — visible as periods flying past. We record
|
|
150
|
+
* the scroll height, restore the offset in a layout effect, and refuse a
|
|
151
|
+
* second prepend until that landed. `hasScrolledRef` additionally keeps mount
|
|
152
|
+
* from pulling in a period nobody scrolled toward.
|
|
153
|
+
*/
|
|
154
|
+
const pendingPrependRef = useRef<number | null>(null);
|
|
155
|
+
const hasScrolledRef = useRef(false);
|
|
156
|
+
|
|
157
|
+
const extend = useCallback((edge: 'before' | 'after') => {
|
|
158
|
+
if (edge === 'before') {
|
|
159
|
+
if (!hasScrolledRef.current) return;
|
|
160
|
+
if (pendingPrependRef.current !== null) return;
|
|
161
|
+
pendingPrependRef.current = scrollerRef.current?.scrollHeight ?? 0;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
setWindow((prev) => {
|
|
165
|
+
if (prev.key !== resetKey) return prev;
|
|
166
|
+
const current = edge === 'before' ? prev.before : prev.after;
|
|
167
|
+
|
|
168
|
+
if (current >= MAX_EXTENSION) return prev;
|
|
169
|
+
|
|
170
|
+
return edge === 'before' ? { ...prev, before: current + 1 } : { ...prev, after: current + 1 };
|
|
171
|
+
});
|
|
172
|
+
}, [resetKey]);
|
|
173
|
+
|
|
174
|
+
const rootRef = useRef<HTMLDivElement | null>(null);
|
|
175
|
+
const startRef = useRef<HTMLDivElement | null>(null);
|
|
176
|
+
const endRef = useRef<HTMLDivElement | null>(null);
|
|
177
|
+
const [nodesReady, setNodesReady] = useState(0);
|
|
178
|
+
|
|
179
|
+
const containerRef = useCallback((node: HTMLDivElement | null) => {
|
|
180
|
+
rootRef.current = node;
|
|
181
|
+
scrollerRef.current = getScrollParent(node);
|
|
182
|
+
setNodesReady(count => count + 1);
|
|
183
|
+
}, []);
|
|
184
|
+
|
|
185
|
+
const startSentinelRef = useCallback((node: HTMLDivElement | null) => {
|
|
186
|
+
startRef.current = node;
|
|
187
|
+
setNodesReady(count => count + 1);
|
|
188
|
+
}, []);
|
|
189
|
+
|
|
190
|
+
const endSentinelRef = useCallback((node: HTMLDivElement | null) => {
|
|
191
|
+
endRef.current = node;
|
|
192
|
+
setNodesReady(count => count + 1);
|
|
193
|
+
}, []);
|
|
194
|
+
|
|
195
|
+
useLayoutEffect(() => {
|
|
196
|
+
const previousHeight = pendingPrependRef.current;
|
|
197
|
+
|
|
198
|
+
if (previousHeight === null) return;
|
|
199
|
+
pendingPrependRef.current = null;
|
|
200
|
+
|
|
201
|
+
const scroller = scrollerRef.current;
|
|
202
|
+
|
|
203
|
+
if (!scroller) return;
|
|
204
|
+
|
|
205
|
+
const delta = scroller.scrollHeight - previousHeight;
|
|
206
|
+
|
|
207
|
+
if (delta > 0) scroller.scrollTop += delta;
|
|
208
|
+
}, [before]);
|
|
209
|
+
|
|
210
|
+
/*
|
|
211
|
+
* A new period (or view / mode) starts at its own beginning instead of
|
|
212
|
+
* wherever the previous one was scrolled to — which is also what stops the
|
|
213
|
+
* sentinels from firing against a stale offset right after a prev/next.
|
|
214
|
+
*/
|
|
215
|
+
useLayoutEffect(() => {
|
|
216
|
+
pendingPrependRef.current = null;
|
|
217
|
+
hasScrolledRef.current = false;
|
|
218
|
+
if (scrollerRef.current) scrollerRef.current.scrollTop = 0;
|
|
219
|
+
}, [resetKey, nodesReady]);
|
|
220
|
+
|
|
221
|
+
useEffect(() => {
|
|
222
|
+
const scroller = scrollerRef.current;
|
|
223
|
+
|
|
224
|
+
if (!scroller) return undefined;
|
|
225
|
+
|
|
226
|
+
const onScroll = () => {
|
|
227
|
+
hasScrolledRef.current = true;
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
scroller.addEventListener('scroll', onScroll, { passive: true });
|
|
231
|
+
|
|
232
|
+
return () => scroller.removeEventListener('scroll', onScroll);
|
|
233
|
+
}, [nodesReady]);
|
|
234
|
+
|
|
235
|
+
useEffect(() => {
|
|
236
|
+
if (!isContinuous) return undefined;
|
|
237
|
+
if (typeof IntersectionObserver === 'undefined') return undefined;
|
|
238
|
+
|
|
239
|
+
const startNode = startRef.current;
|
|
240
|
+
const endNode = endRef.current;
|
|
241
|
+
|
|
242
|
+
if (!startNode && !endNode) return undefined;
|
|
243
|
+
|
|
244
|
+
const observer = new IntersectionObserver((entries) => {
|
|
245
|
+
for (const entry of entries) {
|
|
246
|
+
if (!entry.isIntersecting) continue;
|
|
247
|
+
if (entry.target === startNode) extend('before');
|
|
248
|
+
if (entry.target === endNode) extend('after');
|
|
249
|
+
}
|
|
250
|
+
}, {
|
|
251
|
+
root: scrollerRef.current,
|
|
252
|
+
/*
|
|
253
|
+
* Only look ahead downward — an upward margin re-arms the start sentinel
|
|
254
|
+
* before the user has actually scrolled back to it.
|
|
255
|
+
*/
|
|
256
|
+
rootMargin: '0px 0px 240px 0px'
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
if (startNode) observer.observe(startNode);
|
|
260
|
+
if (endNode) observer.observe(endNode);
|
|
261
|
+
|
|
262
|
+
return () => observer.disconnect();
|
|
263
|
+
}, [
|
|
264
|
+
isContinuous,
|
|
265
|
+
extend,
|
|
266
|
+
nodesReady,
|
|
267
|
+
sections.length
|
|
268
|
+
]);
|
|
269
|
+
|
|
270
|
+
return {
|
|
271
|
+
sections,
|
|
272
|
+
range,
|
|
273
|
+
containerRef,
|
|
274
|
+
startSentinelRef,
|
|
275
|
+
endSentinelRef,
|
|
276
|
+
isContinuous
|
|
277
|
+
};
|
|
278
|
+
}
|