@jsb188/calendar 1.1.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 ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@jsb188/calendar",
3
+ "version": "1.1.10",
4
+ "description": "Calendar and scheduling surface for web.",
5
+ "license": "ISC",
6
+ "author": "jsb188",
7
+ "type": "module",
8
+ "exports": {
9
+ ".": "./src/main.ts",
10
+ "./constants/calendar": "./src/constants/calendar.ts",
11
+ "./libs/calendar-geometry": "./src/libs/calendar-geometry.ts",
12
+ "./libs/calendar-gesture": "./src/libs/calendar-gesture.ts",
13
+ "./libs/calendar-range": "./src/libs/calendar-range.ts",
14
+ "./libs/calendar-state": "./src/libs/calendar-state.ts",
15
+ "./libs/calendar-url": "./src/libs/calendar-url.ts",
16
+ "./types/calendar.d.ts": "./src/types/calendar.d.ts",
17
+ "./ui/CalendarAgendaUI": "./src/ui/CalendarAgendaUI.tsx",
18
+ "./ui/CalendarCoverageUI": "./src/ui/CalendarCoverageUI.tsx",
19
+ "./ui/CalendarEventDetailUI": "./src/ui/CalendarEventDetailUI.tsx",
20
+ "./ui/CalendarEventFormUI": "./src/ui/CalendarEventFormUI.tsx",
21
+ "./ui/CalendarMonthUI": "./src/ui/CalendarMonthUI.tsx",
22
+ "./ui/CalendarRailUI": "./src/ui/CalendarRailUI.tsx",
23
+ "./ui/CalendarRequestUI": "./src/ui/CalendarRequestUI.tsx",
24
+ "./ui/CalendarToolbarUI": "./src/ui/CalendarToolbarUI.tsx",
25
+ "./ui/CalendarUI": "./src/ui/CalendarUI.tsx",
26
+ "./ui/CalendarViolationUI": "./src/ui/CalendarViolationUI.tsx",
27
+ "./ui/CalendarYearUI": "./src/ui/CalendarYearUI.tsx"
28
+ },
29
+ "peerDependencies": {
30
+ "@jsb188/app": "1.1.10",
31
+ "@jsb188/graphql": "1.1.10",
32
+ "@jsb188/mday": "1.1.10",
33
+ "@jsb188/react": "1.1.10",
34
+ "@jsb188/react-web": "1.1.10",
35
+ "jotai": "^2.20.2",
36
+ "luxon": "^3.7.2",
37
+ "react": "^19.2.8",
38
+ "react-router": "^8.3.0"
39
+ },
40
+ "scripts": {
41
+ "test": "vitest run --config ./vitest.config.ts --passWithNoTests",
42
+ "build": "tsc"
43
+ }
44
+ }
@@ -0,0 +1,139 @@
1
+ /**
2
+ * Calendar surface constants.
3
+ *
4
+ * Everything here describes how the calendar is DRAWN and NAVIGATED. Nothing
5
+ * about what a schedule means lives in this package — that is
6
+ * @jsb188/mday/constants/schedule.ts, and duplicating any of it here would
7
+ * give the client a second opinion about the rules.
8
+ */
9
+
10
+ /**
11
+ * The view modes. DAY / FOUR_DAYS / WEEK are the same time-grid component at
12
+ * different widths; MONTH, YEAR and AGENDA are their own surfaces.
13
+ */
14
+
15
+ export const CALENDAR_VIEW_ENUMS = [
16
+ 'DAY',
17
+ 'FOUR_DAYS',
18
+ 'WEEK',
19
+ 'MONTH',
20
+ 'YEAR',
21
+ 'AGENDA',
22
+ ] as const;
23
+
24
+ export type CalendarViewEnum = (typeof CALENDAR_VIEW_ENUMS)[number];
25
+
26
+ // How many days each time-grid view shows. MONTH/YEAR/AGENDA are absent on
27
+ // purpose: their spans are derived from the anchor date, not from a count.
28
+ export const CALENDAR_VIEW_DAY_COUNTS: Partial<Record<CalendarViewEnum, number>> = {
29
+ DAY: 1,
30
+ FOUR_DAYS: 4,
31
+ WEEK: 7,
32
+ };
33
+
34
+ // The agenda list runs forward from its anchor rather than around it; a
35
+ // worker opening "Schedule" wants what is coming, not what they already did.
36
+ export const CALENDAR_AGENDA_DAY_COUNT = 30;
37
+
38
+ /**
39
+ * Time-grid geometry. These are the defaults; the grid scales its hour height
40
+ * to the container, so they are starting points rather than fixed sizes.
41
+ */
42
+
43
+ // Pixels per hour at 100% zoom. 48 shows a full 24-hour day in roughly a
44
+ // laptop viewport without the 15-minute lines collapsing into each other.
45
+ export const CALENDAR_HOUR_HEIGHT = 48;
46
+ export const CALENDAR_HOUR_HEIGHT_MIN = 24;
47
+ export const CALENDAR_HOUR_HEIGHT_MAX = 160;
48
+
49
+ // Width of the left-hand time gutter, and the height of the all-day band's
50
+ // single row. Both are needed by pointer math, so they cannot live in CSS
51
+ // alone.
52
+ export const CALENDAR_GUTTER_WIDTH = 56;
53
+ export const CALENDAR_ALL_DAY_ROW_HEIGHT = 22;
54
+
55
+ // The grid snaps drags and click-to-create to this many minutes. Fifteen is
56
+ // the smallest increment anybody schedules staff in, and it divides the hour
57
+ // lines evenly so the snap always lands on something drawn.
58
+ export const CALENDAR_SNAP_MINUTES = 15;
59
+
60
+ // An event shorter than this is still drawn this tall, so a 5-minute entry
61
+ // stays clickable instead of becoming a hairline.
62
+ export const CALENDAR_MIN_EVENT_MINUTES = 20;
63
+
64
+ // Click-to-create makes a block this long; a drag overrides it.
65
+ export const CALENDAR_DEFAULT_NEW_EVENT_MINUTES = 60;
66
+
67
+ // Below this pointer travel a press is a click, not a drag. Without it every
68
+ // click creates a zero-length event, because no hand is perfectly still.
69
+ export const CALENDAR_DRAG_THRESHOLD_PX = 4;
70
+
71
+ /**
72
+ * Overlap layout. When several events share a time range they split the day
73
+ * column between them; past this many they stack and the rest collapse into a
74
+ * "+N more" affordance rather than becoming unreadable slivers.
75
+ */
76
+
77
+ export const CALENDAR_MAX_OVERLAP_COLUMNS = 6;
78
+
79
+ // Each stacked event peeks out from under the one in front by this fraction
80
+ // of a column, so the ones behind stay discoverable.
81
+ export const CALENDAR_OVERLAP_STACK_RATIO = 0.12;
82
+
83
+ // A month cell shows this many events before collapsing to "+N more".
84
+ export const CALENDAR_MONTH_CELL_MAX_EVENTS = 3;
85
+
86
+ /**
87
+ * The search-param keys the page reads and writes.
88
+ *
89
+ * View and date live in the URL rather than in state because the app shell
90
+ * remounts every route on a PATHNAME change but not on a search change — so
91
+ * this is what lets somebody move from March to April without the calendar
92
+ * being torn down and rebuilt, and it makes a particular week linkable.
93
+ *
94
+ * The keys are one or two characters and every default is omitted, so the
95
+ * common case is a bare "/app/calendar" and a shared link reads as
96
+ * "/app/calendar?v=month&d=2026-07-25" rather than a query string somebody
97
+ * has to decode.
98
+ */
99
+
100
+ export const CALENDAR_SEARCH_PARAMS = {
101
+ view: 'v',
102
+ date: 'd',
103
+ mine: 'me',
104
+ // The calendars the viewer has HIDDEN; absent means show everything
105
+ hiddenCalendars: 'hide',
106
+ } as const;
107
+
108
+ /**
109
+ * URL spellings of the views. Lowercase and human-readable, because these are
110
+ * the part of the URL a person actually reads; "4day" beats "FOUR_DAYS".
111
+ */
112
+
113
+ export const CALENDAR_VIEW_SLUGS: Record<CalendarViewEnum, string> = {
114
+ DAY: 'day',
115
+ FOUR_DAYS: '4day',
116
+ WEEK: 'week',
117
+ MONTH: 'month',
118
+ YEAR: 'year',
119
+ AGENDA: 'agenda',
120
+ };
121
+
122
+ // Omitted from the URL, so an untouched calendar has no query string at all
123
+ export const CALENDAR_DEFAULT_VIEW: CalendarViewEnum = 'WEEK';
124
+
125
+ // Written into the URL and parsed back, so it must stay a plain local date
126
+ export const CALENDAR_DATE_PARAM_FORMAT = 'yyyy-MM-dd';
127
+
128
+ /**
129
+ * Occurrence status → surface treatment. DRAFT is drawn with the shared
130
+ * stripe pattern so an unpublished shift reads as provisional at a glance,
131
+ * and CANCELED is struck through rather than hidden — somebody looking for a
132
+ * shift that was cancelled needs to find it, not wonder where it went.
133
+ */
134
+
135
+ // The stripe pattern is a MASK on a :before pseudo-element, so it needs both a
136
+ // fill colour class ("*_bf") and a positioned parent ("rel", or any absolutely
137
+ // positioned element) or it renders as nothing at all. This is the same
138
+ // combination DataTable-InboundContact and VerticalDataTableDesigner use.
139
+ export const CALENDAR_DRAFT_PATTERN_CLASS_NAME = 'pattern_stripes medium_bf';
package/src/css.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ /*
2
+ * Vite resolves CSS side-effect imports natively; tsc does not know about them
3
+ * and would fail this package's isolated typecheck as soon as it follows an
4
+ * import into a sibling package that ships one.
5
+ */
6
+
7
+ declare module '*.css';
@@ -0,0 +1,426 @@
1
+ import {
2
+ CALENDAR_MAX_OVERLAP_COLUMNS,
3
+ CALENDAR_MIN_EVENT_MINUTES,
4
+ CALENDAR_OVERLAP_STACK_RATIO,
5
+ } from '../constants/calendar.ts';
6
+ import type { CalendarDayObj, CalendarEventLayoutObj, CalendarEventObj } from '../types/calendar.d.ts';
7
+ import { getDayLengthMinutes, getMinutesFromDayStart } from './calendar-range.ts';
8
+
9
+ /**
10
+ * Time-grid geometry: turning a list of events into rectangles.
11
+ *
12
+ * Two problems are solved here, and both are pure functions of the events and
13
+ * the days — no DOM, no measurement, no React. That matters because this runs
14
+ * on every scroll-free re-render of a week that may hold hundreds of shifts,
15
+ * and because the answers are exactly the kind of thing that is easy to get
16
+ * subtly wrong and impossible to notice without a test.
17
+ *
18
+ * 1. An entry that crosses midnight is drawn once per day it touches. An
19
+ * overnight shift is one event, but it is three rectangles across a
20
+ * Friday-to-Sunday weekend, and each has to be clipped to its own day.
21
+ *
22
+ * 2. Events that overlap in time share the width of their day column. The
23
+ * standard approach — cluster, then greedily assign columns — is what
24
+ * every calendar does, but the detail that matters is that the column
25
+ * COUNT is per cluster, not per event: two events at 9am and a third at
26
+ * 2pm should give the first two half the width each and the third all of
27
+ * it, not thirds all day.
28
+ */
29
+
30
+ /*
31
+ * Return whether two half-open intervals overlap.
32
+ */
33
+
34
+ function intervalsOverlap(aStart: number, aEnd: number, bStart: number, bEnd: number): boolean {
35
+ return aStart < bEnd && bStart < aEnd;
36
+ }
37
+
38
+ interface PlacedEvent {
39
+ event: CalendarEventObj;
40
+ dayIndex: number;
41
+ startMinute: number;
42
+ endMinute: number;
43
+ // The extent used for OVERLAP testing, which is at least the minimum
44
+ // drawable height — otherwise two 5-minute events an instant apart would be
45
+ // laid out full-width and then drawn on top of each other
46
+ layoutEndMinute: number;
47
+ continuesFromPreviousDay: boolean;
48
+ continuesToNextDay: boolean;
49
+ column: number;
50
+ columnCount: number;
51
+ }
52
+
53
+ /*
54
+ * Clip one event to one day, returning null when it does not touch that day.
55
+ */
56
+
57
+ function clipEventToDay(
58
+ event: CalendarEventObj,
59
+ day: CalendarDayObj,
60
+ dayIndex: number,
61
+ ): PlacedEvent | null {
62
+ const dayStartMs = day.startAt.getTime();
63
+ const dayEndMs = day.endAt.getTime();
64
+ const eventStartMs = event.startAt.getTime();
65
+ const eventEndMs = event.endAt.getTime();
66
+
67
+ // Half-open on both sides: an event ending exactly at midnight belongs to
68
+ // the day it ran through, not to the empty morning after it
69
+ if (eventStartMs >= dayEndMs || eventEndMs <= dayStartMs) {
70
+ return null;
71
+ }
72
+
73
+ // A zero-length event is still somebody's calendar entry; give it the
74
+ // minimum height rather than dropping it
75
+ const dayLengthMinutes = getDayLengthMinutes(day);
76
+ const startMinute = Math.max(0, getMinutesFromDayStart(event.startAt, day.startAt));
77
+ const endMinute = Math.min(dayLengthMinutes, getMinutesFromDayStart(event.endAt, day.startAt));
78
+
79
+ return {
80
+ event,
81
+ dayIndex,
82
+ startMinute,
83
+ endMinute,
84
+ layoutEndMinute: Math.min(dayLengthMinutes, Math.max(endMinute, startMinute + CALENDAR_MIN_EVENT_MINUTES)),
85
+ continuesFromPreviousDay: eventStartMs < dayStartMs,
86
+ continuesToNextDay: eventEndMs > dayEndMs,
87
+ column: 0,
88
+ columnCount: 1,
89
+ };
90
+ }
91
+
92
+ /*
93
+ * Assign overlap columns within one day.
94
+ *
95
+ * Events are swept in start order. A cluster is a maximal run of events joined
96
+ * by overlap — not merely pairwise-overlapping, because A-overlaps-B and
97
+ * B-overlaps-C means all three share the column budget even when A and C do
98
+ * not touch. The cluster closes when an event starts at or after every open
99
+ * event has ended, and only then is the column count known for everybody in
100
+ * it.
101
+ */
102
+
103
+ function assignOverlapColumns(placed: PlacedEvent[]): void {
104
+ placed.sort((left, right) => (
105
+ left.startMinute - right.startMinute ||
106
+ right.layoutEndMinute - left.layoutEndMinute ||
107
+ String(left.event.id).localeCompare(String(right.event.id))
108
+ ));
109
+
110
+ let cluster: PlacedEvent[] = [];
111
+ let clusterEnd = -Infinity;
112
+
113
+ const closeCluster = () => {
114
+ if (!cluster.length) {
115
+ return;
116
+ }
117
+
118
+ const columnCount = Math.max(...cluster.map((entry) => entry.column)) + 1;
119
+
120
+ for (const entry of cluster) {
121
+ entry.columnCount = columnCount;
122
+ }
123
+
124
+ cluster = [];
125
+ clusterEnd = -Infinity;
126
+ };
127
+
128
+ // Column index -> the minute that column is free from
129
+ let columnFreeFrom: number[] = [];
130
+
131
+ for (const entry of placed) {
132
+ if (entry.startMinute >= clusterEnd) {
133
+ closeCluster();
134
+ columnFreeFrom = [];
135
+ }
136
+
137
+ let column = columnFreeFrom.findIndex((freeFrom) => entry.startMinute >= freeFrom);
138
+
139
+ if (column === -1) {
140
+ column = columnFreeFrom.length;
141
+ columnFreeFrom.push(entry.layoutEndMinute);
142
+ } else {
143
+ columnFreeFrom[column] = entry.layoutEndMinute;
144
+ }
145
+
146
+ entry.column = column;
147
+ cluster.push(entry);
148
+ clusterEnd = Math.max(clusterEnd, entry.layoutEndMinute);
149
+ }
150
+
151
+ closeCluster();
152
+ }
153
+
154
+ /*
155
+ * Turn a placed event's column assignment into horizontal geometry.
156
+ *
157
+ * Up to CALENDAR_MAX_OVERLAP_COLUMNS the columns simply divide the width. Past
158
+ * that they overlap in a stack: each event still gets a readable width and
159
+ * peeks out from behind the one in front, which stays legible where a
160
+ * seventh-of-a-column sliver would not.
161
+ */
162
+
163
+ function getHorizontalGeometry(column: number, columnCount: number): { left: number; width: number } {
164
+ if (columnCount <= CALENDAR_MAX_OVERLAP_COLUMNS) {
165
+ return {
166
+ left: column / columnCount,
167
+ width: 1 / columnCount,
168
+ };
169
+ }
170
+
171
+ const left = Math.min(column * CALENDAR_OVERLAP_STACK_RATIO, 1 - CALENDAR_OVERLAP_STACK_RATIO);
172
+
173
+ return {
174
+ left,
175
+ width: 1 - left,
176
+ };
177
+ }
178
+
179
+ /**
180
+ * Lay out timed events across a range of days.
181
+ *
182
+ * All-day entries are excluded here — they belong to the band above the grid,
183
+ * which is a different layout problem (horizontal bars spanning day columns)
184
+ * and is solved by getCalendarAllDayLayout.
185
+ */
186
+
187
+ export function getCalendarTimedLayout(
188
+ events: CalendarEventObj[],
189
+ days: CalendarDayObj[],
190
+ ): CalendarEventLayoutObj[] {
191
+ const byDay: PlacedEvent[][] = days.map(() => []);
192
+
193
+ for (const event of events) {
194
+ if (event.allDay) {
195
+ continue;
196
+ }
197
+
198
+ for (let dayIndex = 0; dayIndex < days.length; dayIndex++) {
199
+ const placed = clipEventToDay(event, days[dayIndex], dayIndex);
200
+
201
+ if (placed) {
202
+ byDay[dayIndex].push(placed);
203
+ }
204
+ }
205
+ }
206
+
207
+ const layout: CalendarEventLayoutObj[] = [];
208
+
209
+ for (const dayEvents of byDay) {
210
+ assignOverlapColumns(dayEvents);
211
+
212
+ for (const entry of dayEvents) {
213
+ const { left, width } = getHorizontalGeometry(entry.column, entry.columnCount);
214
+
215
+ layout.push({
216
+ event: entry.event,
217
+ dayIndex: entry.dayIndex,
218
+ startMinute: entry.startMinute,
219
+ endMinute: entry.endMinute,
220
+ left,
221
+ width,
222
+ column: entry.column,
223
+ columnCount: entry.columnCount,
224
+ continuesFromPreviousDay: entry.continuesFromPreviousDay,
225
+ continuesToNextDay: entry.continuesToNextDay,
226
+ });
227
+ }
228
+ }
229
+
230
+ return layout;
231
+ }
232
+
233
+ /**
234
+ * One all-day bar: which day columns it spans, and which row of the band it
235
+ * sits in.
236
+ */
237
+
238
+ export interface CalendarAllDayBarObj {
239
+ event: CalendarEventObj;
240
+ // Inclusive day-column indices
241
+ startDayIndex: number;
242
+ endDayIndex: number;
243
+ row: number;
244
+ continuesFromPreviousDay: boolean;
245
+ continuesToNextDay: boolean;
246
+ }
247
+
248
+ /**
249
+ * Lay out all-day and multi-day entries as stacked horizontal bars.
250
+ *
251
+ * Rows are packed greedily in start order: each bar takes the first row where
252
+ * nothing already occupies its day span. That keeps the band as short as
253
+ * possible, which matters because it eats vertical space the time grid needs.
254
+ */
255
+
256
+ export function getCalendarAllDayLayout(
257
+ events: CalendarEventObj[],
258
+ days: CalendarDayObj[],
259
+ ): CalendarAllDayBarObj[] {
260
+ if (!days.length) {
261
+ return [];
262
+ }
263
+
264
+ const rangeStartMs = days[0].startAt.getTime();
265
+ const rangeEndMs = days[days.length - 1].endAt.getTime();
266
+
267
+ const bars = events
268
+ .filter((event) => (
269
+ // An entry belongs in the band when it is flagged all-day, or when it
270
+ // simply spans more than one of the days on screen — a three-day
271
+ // closure is not usefully drawn as three tall columns
272
+ (event.allDay || spansMultipleDays(event, days)) &&
273
+ event.startAt.getTime() < rangeEndMs &&
274
+ event.endAt.getTime() > rangeStartMs
275
+ ))
276
+ .map((event) => {
277
+ const startDayIndex = days.findIndex((day) => event.startAt.getTime() < day.endAt.getTime());
278
+ let endDayIndex = -1;
279
+
280
+ for (let index = days.length - 1; index >= 0; index--) {
281
+ if (event.endAt.getTime() > days[index].startAt.getTime()) {
282
+ endDayIndex = index;
283
+ break;
284
+ }
285
+ }
286
+
287
+ return {
288
+ event,
289
+ startDayIndex: startDayIndex === -1 ? 0 : startDayIndex,
290
+ endDayIndex: endDayIndex === -1 ? days.length - 1 : endDayIndex,
291
+ row: 0,
292
+ continuesFromPreviousDay: event.startAt.getTime() < rangeStartMs,
293
+ continuesToNextDay: event.endAt.getTime() > rangeEndMs,
294
+ };
295
+ })
296
+ .sort((left, right) => (
297
+ left.startDayIndex - right.startDayIndex ||
298
+ (right.endDayIndex - right.startDayIndex) - (left.endDayIndex - left.startDayIndex) ||
299
+ String(left.event.id).localeCompare(String(right.event.id))
300
+ ));
301
+
302
+ // Row index -> the day index that row is free from
303
+ const rowFreeFrom: number[] = [];
304
+
305
+ for (const bar of bars) {
306
+ let row = rowFreeFrom.findIndex((freeFrom) => bar.startDayIndex >= freeFrom);
307
+
308
+ if (row === -1) {
309
+ row = rowFreeFrom.length;
310
+ rowFreeFrom.push(bar.endDayIndex + 1);
311
+ } else {
312
+ rowFreeFrom[row] = bar.endDayIndex + 1;
313
+ }
314
+
315
+ bar.row = row;
316
+ }
317
+
318
+ return bars;
319
+ }
320
+
321
+ /*
322
+ * Return whether one event covers more than one of the days on screen.
323
+ */
324
+
325
+ function spansMultipleDays(event: CalendarEventObj, days: CalendarDayObj[]): boolean {
326
+ let touched = 0;
327
+
328
+ for (const day of days) {
329
+ if (intervalsOverlap(
330
+ event.startAt.getTime(),
331
+ event.endAt.getTime(),
332
+ day.startAt.getTime(),
333
+ day.endAt.getTime(),
334
+ )) {
335
+ touched++;
336
+
337
+ if (touched > 1) {
338
+ return true;
339
+ }
340
+ }
341
+ }
342
+
343
+ return false;
344
+ }
345
+
346
+ /**
347
+ * One month-grid cell: the day, the events that fit, and how many did not.
348
+ */
349
+
350
+ export interface CalendarMonthCellObj {
351
+ day: CalendarDayObj;
352
+ events: CalendarEventObj[];
353
+ // Events the cell could not show; drives the "+N more" affordance
354
+ overflowCount: number;
355
+ }
356
+
357
+ /**
358
+ * Build the month grid's cells, truncating each to what will actually fit.
359
+ *
360
+ * The truncation happens here rather than in the component so that "+N more"
361
+ * is a number the layout computed, not something a CSS overflow hid. A cell
362
+ * that silently clipped its third event would tell a manager the day is
363
+ * quieter than it is.
364
+ */
365
+
366
+ export function getCalendarMonthCells(
367
+ events: CalendarEventObj[],
368
+ days: CalendarDayObj[],
369
+ maxPerCell: number,
370
+ ): CalendarMonthCellObj[] {
371
+ const byDateKey = getCalendarEventsByDateKey(events, days);
372
+
373
+ return days.map((day) => {
374
+ const dayEvents = byDateKey.get(day.dateKey) || [];
375
+
376
+ return {
377
+ day,
378
+ events: dayEvents.slice(0, maxPerCell),
379
+ overflowCount: Math.max(0, dayEvents.length - maxPerCell),
380
+ };
381
+ });
382
+ }
383
+
384
+ /**
385
+ * Group events by the local day they should appear under, for the month grid
386
+ * and the agenda list.
387
+ *
388
+ * A multi-day entry is listed under every day it covers, because a month cell
389
+ * that omitted the middle days of a conference would be lying about those days
390
+ * being free.
391
+ */
392
+
393
+ export function getCalendarEventsByDateKey(
394
+ events: CalendarEventObj[],
395
+ days: CalendarDayObj[],
396
+ ): Map<string, CalendarEventObj[]> {
397
+ const byDateKey = new Map<string, CalendarEventObj[]>();
398
+
399
+ for (const day of days) {
400
+ byDateKey.set(day.dateKey, []);
401
+ }
402
+
403
+ for (const event of events) {
404
+ for (const day of days) {
405
+ if (intervalsOverlap(
406
+ event.startAt.getTime(),
407
+ event.endAt.getTime(),
408
+ day.startAt.getTime(),
409
+ day.endAt.getTime(),
410
+ )) {
411
+ byDateKey.get(day.dateKey)?.push(event);
412
+ }
413
+ }
414
+ }
415
+
416
+ for (const dayEvents of byDateKey.values()) {
417
+ dayEvents.sort((left, right) => (
418
+ // All-day entries head the list; below them, chronological
419
+ Number(right.allDay) - Number(left.allDay) ||
420
+ left.startAt.getTime() - right.startAt.getTime() ||
421
+ String(left.id).localeCompare(String(right.id))
422
+ ));
423
+ }
424
+
425
+ return byDateKey;
426
+ }