@openpeepshq/common 0.1.46 → 0.2.0
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/dist/lib/adminSections.js +1 -1
- package/dist/lib/capabilitiesHelpers.js +30 -40
- package/dist/lib/csvHelpers.js +1 -1
- package/dist/lib/eventIcs.js +26 -32
- package/dist/lib/eventRecurrence.d.ts +17 -0
- package/dist/lib/eventRecurrence.js +75 -18
- package/dist/lib/eventTime.js +9 -11
- package/dist/lib/groupHelpers.js +13 -34
- package/dist/lib/groupTemplateHelpers.js +6 -10
- package/dist/lib/jamHelpers.js +6 -9
- package/dist/lib/markdownHelpers.js +2 -3
- package/dist/lib/postHelpers.js +70 -97
- package/dist/lib/profileHelpers.js +17 -24
- package/dist/lib/pushHelpers.js +3 -4
- package/dist/lib/scopeHelpers.js +6 -9
- package/dist/lib/ssoLoginUrl.js +4 -6
- package/dist/lib/ssoProviders.js +35 -39
- package/dist/lib/tokenHelpers.js +5 -5
- package/dist/lib/utils.js +6 -7
- package/dist/types/admin-db.js +1 -1
- package/dist/types/api.d.ts +1 -0
- package/dist/types/api.js +1 -1
- package/dist/types/config.d.ts +3 -0
- package/dist/types/config.js +3 -0
- package/dist/types/models.js +1 -1
- package/dist/types/plugins.js +2 -2
- package/dist/zod.d.ts +1 -0
- package/dist/zod.js +1 -0
- package/package.json +36 -6
- package/src/index.ts +4 -0
- package/src/lib/adminSections.ts +84 -0
- package/src/lib/attachmentHelpers.ts +14 -0
- package/src/lib/capabilitiesHelpers.ts +532 -0
- package/src/lib/colors.ts +20 -0
- package/src/lib/csvHelpers.ts +31 -0
- package/src/lib/eventIcs.ts +265 -0
- package/src/lib/eventRecurrence.ts +495 -0
- package/src/lib/eventTime.ts +206 -0
- package/src/lib/federationHosts.ts +22 -0
- package/src/lib/feedCursor.ts +27 -0
- package/src/lib/feedFormat.ts +24 -0
- package/src/lib/groupHelpers.ts +421 -0
- package/src/lib/groupTemplateHelpers.ts +83 -0
- package/src/lib/index.ts +29 -0
- package/src/lib/jamHelpers.ts +164 -0
- package/src/lib/legacyThemeRoot.ts +14 -0
- package/src/lib/markdownHelpers.ts +122 -0
- package/src/lib/mediaDimensions.ts +21 -0
- package/src/lib/postHelpers.ts +783 -0
- package/src/lib/profileHelpers.ts +220 -0
- package/src/lib/pushHelpers.ts +34 -0
- package/src/lib/reactions.ts +52 -0
- package/src/lib/reportHelpers.ts +7 -0
- package/src/lib/scopeHelpers.ts +176 -0
- package/src/lib/ssoLoginUrl.ts +93 -0
- package/src/lib/ssoProviders.ts +472 -0
- package/src/lib/text.ts +26 -0
- package/src/lib/timeZone.ts +52 -0
- package/src/lib/tokenHelpers.ts +119 -0
- package/src/lib/utils.ts +322 -0
- package/src/types/admin-db.ts +74 -0
- package/src/types/admin-email.ts +55 -0
- package/src/types/admin-jobs.ts +17 -0
- package/src/types/admin-performance.ts +20 -0
- package/src/types/analytics.ts +318 -0
- package/src/types/api.ts +871 -0
- package/src/types/apple-app-site-association.ts +54 -0
- package/src/types/auth.ts +22 -0
- package/src/types/capabilities.ts +280 -0
- package/src/types/config.ts +546 -0
- package/src/types/db.ts +138 -0
- package/src/types/export.ts +10 -0
- package/src/types/index.ts +20 -0
- package/src/types/internal.ts +338 -0
- package/src/types/models.ts +939 -0
- package/src/types/notifications.ts +92 -0
- package/src/types/plugins.ts +150 -0
- package/src/types/roleDefaults.ts +132 -0
- package/src/types/stats.ts +195 -0
- package/src/types/stripe.ts +45 -0
- package/src/types/utils.ts +100 -0
- package/src/zod.ts +1 -0
|
@@ -0,0 +1,495 @@
|
|
|
1
|
+
import {
|
|
2
|
+
addDays,
|
|
3
|
+
addMonths,
|
|
4
|
+
addWeeks,
|
|
5
|
+
differenceInCalendarWeeks,
|
|
6
|
+
getDay,
|
|
7
|
+
} from 'date-fns';
|
|
8
|
+
import type {
|
|
9
|
+
Event,
|
|
10
|
+
EventOccurrenceException,
|
|
11
|
+
EventRecurrence,
|
|
12
|
+
RecurrenceWeekday,
|
|
13
|
+
} from '../types';
|
|
14
|
+
|
|
15
|
+
export type EventRecurrenceTranslate = (
|
|
16
|
+
key: string,
|
|
17
|
+
options?: {
|
|
18
|
+
defaultValue?: string;
|
|
19
|
+
[key: string]: string | number | undefined;
|
|
20
|
+
},
|
|
21
|
+
) => string;
|
|
22
|
+
|
|
23
|
+
const WEEKDAY_ORDER: RecurrenceWeekday[] = [
|
|
24
|
+
'MO',
|
|
25
|
+
'TU',
|
|
26
|
+
'WE',
|
|
27
|
+
'TH',
|
|
28
|
+
'FR',
|
|
29
|
+
'SA',
|
|
30
|
+
'SU',
|
|
31
|
+
];
|
|
32
|
+
|
|
33
|
+
export const OCCURRENCE_HORIZON_MS = 365 * 24 * 60 * 60 * 1000;
|
|
34
|
+
|
|
35
|
+
const WEEKDAY_INDEX: Record<RecurrenceWeekday, number> = {
|
|
36
|
+
SU: 0,
|
|
37
|
+
MO: 1,
|
|
38
|
+
TU: 2,
|
|
39
|
+
WE: 3,
|
|
40
|
+
TH: 4,
|
|
41
|
+
FR: 5,
|
|
42
|
+
SA: 6,
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const INDEX_WEEKDAY: RecurrenceWeekday[] = [
|
|
46
|
+
'SU',
|
|
47
|
+
'MO',
|
|
48
|
+
'TU',
|
|
49
|
+
'WE',
|
|
50
|
+
'TH',
|
|
51
|
+
'FR',
|
|
52
|
+
'SA',
|
|
53
|
+
];
|
|
54
|
+
|
|
55
|
+
export type ExpandedOccurrence = {
|
|
56
|
+
recurrenceId: string;
|
|
57
|
+
start: string;
|
|
58
|
+
end?: string;
|
|
59
|
+
cancelled: boolean;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export const normalizeRecurrenceId = (value: string): string => {
|
|
63
|
+
const date = new Date(value);
|
|
64
|
+
if (Number.isNaN(date.getTime())) {
|
|
65
|
+
return value;
|
|
66
|
+
}
|
|
67
|
+
return date.toISOString();
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
export const sameRecurrenceId = (a?: string, b?: string): boolean => {
|
|
71
|
+
if (!a || !b) return false;
|
|
72
|
+
return normalizeRecurrenceId(a) === normalizeRecurrenceId(b);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export const isRecurringEvent = (event: Event): boolean => !!event.recurrence;
|
|
76
|
+
|
|
77
|
+
export const eventDurationMs = (event: Event): number | undefined => {
|
|
78
|
+
if (!event.end) return undefined;
|
|
79
|
+
const ms = new Date(event.end).getTime() - new Date(event.start).getTime();
|
|
80
|
+
return Number.isFinite(ms) && ms >= 0 ? ms : undefined;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const toIso = (date: Date): string => date.toISOString();
|
|
84
|
+
|
|
85
|
+
const applyDuration = (
|
|
86
|
+
start: Date,
|
|
87
|
+
durationMs: number | undefined,
|
|
88
|
+
): string | undefined => {
|
|
89
|
+
if (durationMs == null) return undefined;
|
|
90
|
+
return toIso(new Date(start.getTime() + durationMs));
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const exceptionById = (event: Event): Map<string, EventOccurrenceException> => {
|
|
94
|
+
const map = new Map<string, EventOccurrenceException>();
|
|
95
|
+
for (const exception of event.exceptions ?? []) {
|
|
96
|
+
map.set(normalizeRecurrenceId(exception.recurrenceId), exception);
|
|
97
|
+
}
|
|
98
|
+
return map;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
export const weekdayFromDate = (date: Date): RecurrenceWeekday =>
|
|
102
|
+
INDEX_WEEKDAY[getDay(date)];
|
|
103
|
+
|
|
104
|
+
export const parseOccurrenceQuery = (
|
|
105
|
+
value?: string | null,
|
|
106
|
+
): string | undefined => {
|
|
107
|
+
if (!value) return undefined;
|
|
108
|
+
const date = new Date(value);
|
|
109
|
+
if (Number.isNaN(date.getTime())) return undefined;
|
|
110
|
+
return date.toISOString();
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const nextByFreq = (
|
|
114
|
+
date: Date,
|
|
115
|
+
freq: NonNullable<Event['recurrence']>['freq'],
|
|
116
|
+
interval: number,
|
|
117
|
+
): Date => {
|
|
118
|
+
if (freq === 'DAILY') return addDays(date, interval);
|
|
119
|
+
if (freq === 'WEEKLY') return addWeeks(date, interval);
|
|
120
|
+
return addMonths(date, interval);
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const generateRawStarts = (event: Event, untilBound: Date): Date[] => {
|
|
124
|
+
const recurrence = event.recurrence;
|
|
125
|
+
const dtstart = new Date(event.start);
|
|
126
|
+
if (!recurrence) {
|
|
127
|
+
return [dtstart];
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const interval = recurrence.interval ?? 1;
|
|
131
|
+
const until = recurrence.until ? new Date(recurrence.until) : undefined;
|
|
132
|
+
const hardStop = until && until < untilBound ? until : untilBound;
|
|
133
|
+
const starts: Date[] = [];
|
|
134
|
+
|
|
135
|
+
const pushIfValid = (date: Date) => {
|
|
136
|
+
if (date.getTime() < dtstart.getTime()) return;
|
|
137
|
+
if (date.getTime() > hardStop.getTime()) return;
|
|
138
|
+
starts.push(date);
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
if (recurrence.freq === 'WEEKLY' && (recurrence.byDay?.length ?? 0) > 0) {
|
|
142
|
+
const byDays = new Set(recurrence.byDay);
|
|
143
|
+
let cursor = dtstart;
|
|
144
|
+
while (cursor.getTime() <= hardStop.getTime()) {
|
|
145
|
+
if (
|
|
146
|
+
byDays.has(weekdayFromDate(cursor)) &&
|
|
147
|
+
differenceInCalendarWeeks(cursor, dtstart) % interval === 0
|
|
148
|
+
) {
|
|
149
|
+
pushIfValid(cursor);
|
|
150
|
+
}
|
|
151
|
+
if (recurrence.count && starts.length >= recurrence.count) break;
|
|
152
|
+
cursor = addDays(cursor, 1);
|
|
153
|
+
}
|
|
154
|
+
} else {
|
|
155
|
+
let cursor = dtstart;
|
|
156
|
+
while (cursor.getTime() <= hardStop.getTime()) {
|
|
157
|
+
pushIfValid(cursor);
|
|
158
|
+
if (recurrence.count && starts.length >= recurrence.count) break;
|
|
159
|
+
cursor = nextByFreq(cursor, recurrence.freq, interval);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const unique = [
|
|
164
|
+
...new Map(starts.map((date) => [date.getTime(), date])).values(),
|
|
165
|
+
].sort((a, b) => a.getTime() - b.getTime());
|
|
166
|
+
|
|
167
|
+
return recurrence.count ? unique.slice(0, recurrence.count) : unique;
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
const decorateOccurrence = (
|
|
171
|
+
originalStart: Date,
|
|
172
|
+
event: Event,
|
|
173
|
+
exceptions: Map<string, EventOccurrenceException>,
|
|
174
|
+
durationMs: number | undefined,
|
|
175
|
+
): ExpandedOccurrence => {
|
|
176
|
+
const recurrenceId = toIso(originalStart);
|
|
177
|
+
const exception = exceptions.get(recurrenceId);
|
|
178
|
+
if (exception?.cancelled) {
|
|
179
|
+
return {
|
|
180
|
+
recurrenceId,
|
|
181
|
+
start: exception.start ?? recurrenceId,
|
|
182
|
+
end: exception.end ?? applyDuration(originalStart, durationMs),
|
|
183
|
+
cancelled: true,
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
if (exception?.start) {
|
|
187
|
+
const start = new Date(exception.start);
|
|
188
|
+
return {
|
|
189
|
+
recurrenceId,
|
|
190
|
+
start: toIso(start),
|
|
191
|
+
end: exception.end ?? applyDuration(start, durationMs),
|
|
192
|
+
cancelled: false,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
return {
|
|
196
|
+
recurrenceId,
|
|
197
|
+
start: recurrenceId,
|
|
198
|
+
end: applyDuration(originalStart, durationMs),
|
|
199
|
+
cancelled: false,
|
|
200
|
+
};
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
export const expandEventOccurrences = (
|
|
204
|
+
event: Event,
|
|
205
|
+
window: { from: Date; to: Date },
|
|
206
|
+
): ExpandedOccurrence[] => {
|
|
207
|
+
const durationMs = eventDurationMs(event);
|
|
208
|
+
const exceptions = exceptionById(event);
|
|
209
|
+
const rawStarts = generateRawStarts(event, window.to);
|
|
210
|
+
return rawStarts
|
|
211
|
+
.map((start) => decorateOccurrence(start, event, exceptions, durationMs))
|
|
212
|
+
.filter((occurrence) => {
|
|
213
|
+
const start = new Date(occurrence.start);
|
|
214
|
+
const end = occurrence.end ? new Date(occurrence.end) : start;
|
|
215
|
+
return end >= window.from && start <= window.to;
|
|
216
|
+
});
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
export const occurrencesForIndex = (
|
|
220
|
+
event: Event,
|
|
221
|
+
now = new Date(),
|
|
222
|
+
): ExpandedOccurrence[] => {
|
|
223
|
+
if (!event.recurrence) {
|
|
224
|
+
return [
|
|
225
|
+
{
|
|
226
|
+
recurrenceId: normalizeRecurrenceId(event.start),
|
|
227
|
+
start: normalizeRecurrenceId(event.start),
|
|
228
|
+
end: event.end ? normalizeRecurrenceId(event.end) : undefined,
|
|
229
|
+
cancelled: false,
|
|
230
|
+
},
|
|
231
|
+
];
|
|
232
|
+
}
|
|
233
|
+
return expandEventOccurrences(event, {
|
|
234
|
+
from: new Date(now.getTime() - OCCURRENCE_HORIZON_MS),
|
|
235
|
+
to: new Date(now.getTime() + OCCURRENCE_HORIZON_MS),
|
|
236
|
+
});
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
export const listRsvpOccurrences = (
|
|
240
|
+
event: Event,
|
|
241
|
+
now = new Date(),
|
|
242
|
+
): ExpandedOccurrence[] =>
|
|
243
|
+
expandEventOccurrences(event, {
|
|
244
|
+
from: now,
|
|
245
|
+
to: new Date(now.getTime() + OCCURRENCE_HORIZON_MS),
|
|
246
|
+
}).filter((occurrence) => !occurrence.cancelled);
|
|
247
|
+
|
|
248
|
+
/** Matches the agenda: a missing end is one hour after start, not open forever. */
|
|
249
|
+
const IMPLIED_EVENT_DURATION_MS = 60 * 60 * 1000;
|
|
250
|
+
|
|
251
|
+
export const occurrenceHasEnded = (
|
|
252
|
+
start: string,
|
|
253
|
+
end?: string,
|
|
254
|
+
now = new Date(),
|
|
255
|
+
): boolean => {
|
|
256
|
+
const startMs = new Date(start).getTime();
|
|
257
|
+
if (!Number.isFinite(startMs)) return false;
|
|
258
|
+
const explicitEnd = end ? new Date(end).getTime() : Number.NaN;
|
|
259
|
+
const endMs = Number.isFinite(explicitEnd)
|
|
260
|
+
? explicitEnd
|
|
261
|
+
: startMs + IMPLIED_EVENT_DURATION_MS;
|
|
262
|
+
return endMs < now.getTime();
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
const findExpandedOccurrence = (
|
|
266
|
+
event: Event,
|
|
267
|
+
recurrenceId: string,
|
|
268
|
+
): ExpandedOccurrence | undefined => {
|
|
269
|
+
const anchor = new Date(recurrenceId).getTime();
|
|
270
|
+
if (!Number.isFinite(anchor)) return undefined;
|
|
271
|
+
return expandEventOccurrences(event, {
|
|
272
|
+
from: new Date(anchor - OCCURRENCE_HORIZON_MS),
|
|
273
|
+
to: new Date(anchor + OCCURRENCE_HORIZON_MS),
|
|
274
|
+
}).find((occurrence) =>
|
|
275
|
+
sameRecurrenceId(occurrence.recurrenceId, recurrenceId),
|
|
276
|
+
);
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
const occurrenceClosed = (
|
|
280
|
+
event: Event,
|
|
281
|
+
recurrenceId: string,
|
|
282
|
+
now: Date,
|
|
283
|
+
): boolean => {
|
|
284
|
+
if (!event.recurrence) {
|
|
285
|
+
return occurrenceHasEnded(event.start, event.end, now);
|
|
286
|
+
}
|
|
287
|
+
const match = findExpandedOccurrence(event, recurrenceId);
|
|
288
|
+
if (!match || match.cancelled) return true;
|
|
289
|
+
return occurrenceHasEnded(match.start, match.end, now);
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
export type EventRsvpOpenOptions = {
|
|
293
|
+
recurrenceId?: string;
|
|
294
|
+
lockToOccurrence?: boolean;
|
|
295
|
+
now?: Date;
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
/** False once the event, or the locked occurrence, is over. */
|
|
299
|
+
export const eventRsvpOpen = (
|
|
300
|
+
event: Event,
|
|
301
|
+
options?: EventRsvpOpenOptions,
|
|
302
|
+
): boolean => {
|
|
303
|
+
const now = options?.now ?? new Date();
|
|
304
|
+
if (options?.lockToOccurrence && options.recurrenceId) {
|
|
305
|
+
return !occurrenceClosed(event, options.recurrenceId, now);
|
|
306
|
+
}
|
|
307
|
+
if (!event.recurrence) {
|
|
308
|
+
return !occurrenceHasEnded(event.start, event.end, now);
|
|
309
|
+
}
|
|
310
|
+
return expandEventOccurrences(event, {
|
|
311
|
+
from: new Date(now.getTime() - OCCURRENCE_HORIZON_MS),
|
|
312
|
+
to: new Date(now.getTime() + OCCURRENCE_HORIZON_MS),
|
|
313
|
+
}).some(
|
|
314
|
+
(occurrence) =>
|
|
315
|
+
!occurrence.cancelled &&
|
|
316
|
+
!occurrenceHasEnded(occurrence.start, occurrence.end, now),
|
|
317
|
+
);
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
export type RsvpTimeTarget = {
|
|
321
|
+
occurrenceIds?: string[];
|
|
322
|
+
recurrenceId?: string;
|
|
323
|
+
};
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* True when every targeted time is already over. A series RSVP with no ids
|
|
327
|
+
* is past only when the series has nothing left open.
|
|
328
|
+
*/
|
|
329
|
+
export const rsvpTargetsPastEvent = (
|
|
330
|
+
event: Event,
|
|
331
|
+
target?: RsvpTimeTarget,
|
|
332
|
+
now = new Date(),
|
|
333
|
+
): boolean => {
|
|
334
|
+
const ids = target?.occurrenceIds?.length
|
|
335
|
+
? target.occurrenceIds
|
|
336
|
+
: target?.recurrenceId
|
|
337
|
+
? [target.recurrenceId]
|
|
338
|
+
: undefined;
|
|
339
|
+
if (!ids) return !eventRsvpOpen(event, { now });
|
|
340
|
+
return ids.every(
|
|
341
|
+
(id) =>
|
|
342
|
+
!eventRsvpOpen(event, {
|
|
343
|
+
recurrenceId: id,
|
|
344
|
+
lockToOccurrence: true,
|
|
345
|
+
now,
|
|
346
|
+
}),
|
|
347
|
+
);
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
export const defaultRsvpRecurrenceId = (
|
|
351
|
+
event: Event,
|
|
352
|
+
queryId?: string,
|
|
353
|
+
now = new Date(),
|
|
354
|
+
): string | undefined => {
|
|
355
|
+
if (queryId) return normalizeRecurrenceId(queryId);
|
|
356
|
+
return listRsvpOccurrences(event, now)[0]?.recurrenceId;
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
export const previewUpcomingOccurrences = (
|
|
360
|
+
event: Event,
|
|
361
|
+
count = 3,
|
|
362
|
+
now = new Date(),
|
|
363
|
+
): ExpandedOccurrence[] => listRsvpOccurrences(event, now).slice(0, count);
|
|
364
|
+
|
|
365
|
+
export const effectiveEventTimes = (
|
|
366
|
+
event: Event,
|
|
367
|
+
recurrenceId?: string,
|
|
368
|
+
): { start: string; end?: string } => {
|
|
369
|
+
if (!recurrenceId) {
|
|
370
|
+
return { start: event.start, end: event.end };
|
|
371
|
+
}
|
|
372
|
+
const window = {
|
|
373
|
+
from: new Date(new Date(recurrenceId).getTime() - OCCURRENCE_HORIZON_MS),
|
|
374
|
+
to: new Date(new Date(recurrenceId).getTime() + OCCURRENCE_HORIZON_MS),
|
|
375
|
+
};
|
|
376
|
+
const match = expandEventOccurrences(event, window).find((occurrence) =>
|
|
377
|
+
sameRecurrenceId(occurrence.recurrenceId, recurrenceId),
|
|
378
|
+
);
|
|
379
|
+
if (!match || match.cancelled) {
|
|
380
|
+
return { start: event.start, end: event.end };
|
|
381
|
+
}
|
|
382
|
+
return { start: match.start, end: match.end };
|
|
383
|
+
};
|
|
384
|
+
|
|
385
|
+
export const upsertEventException = (
|
|
386
|
+
event: Event,
|
|
387
|
+
exception: EventOccurrenceException,
|
|
388
|
+
): Event => {
|
|
389
|
+
const recurrenceId = normalizeRecurrenceId(exception.recurrenceId);
|
|
390
|
+
const rest = (event.exceptions ?? []).filter(
|
|
391
|
+
(existing) => !sameRecurrenceId(existing.recurrenceId, recurrenceId),
|
|
392
|
+
);
|
|
393
|
+
return {
|
|
394
|
+
...event,
|
|
395
|
+
exceptions: [...rest, { ...exception, recurrenceId }],
|
|
396
|
+
};
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
const sortWeekdays = (days: RecurrenceWeekday[]): RecurrenceWeekday[] =>
|
|
400
|
+
[...days].sort((a, b) => WEEKDAY_ORDER.indexOf(a) - WEEKDAY_ORDER.indexOf(b));
|
|
401
|
+
|
|
402
|
+
const formatUntilDate = (until: string): string =>
|
|
403
|
+
new Date(until).toLocaleDateString(undefined, {
|
|
404
|
+
month: 'short',
|
|
405
|
+
day: 'numeric',
|
|
406
|
+
year: 'numeric',
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
const formatWeekdayLabels = (
|
|
410
|
+
days: RecurrenceWeekday[],
|
|
411
|
+
t: EventRecurrenceTranslate,
|
|
412
|
+
): string =>
|
|
413
|
+
sortWeekdays(days)
|
|
414
|
+
.map((day) => t(`events.form.repeat.day.${day}`, { defaultValue: day }))
|
|
415
|
+
.join(', ');
|
|
416
|
+
|
|
417
|
+
const formatRecurrencePattern = (
|
|
418
|
+
recurrence: EventRecurrence,
|
|
419
|
+
t: EventRecurrenceTranslate,
|
|
420
|
+
start?: string,
|
|
421
|
+
): string => {
|
|
422
|
+
const interval = recurrence.interval ?? 1;
|
|
423
|
+
|
|
424
|
+
if (recurrence.freq === 'DAILY') {
|
|
425
|
+
return interval === 1
|
|
426
|
+
? t('events.repeat.daily', { defaultValue: 'Daily' })
|
|
427
|
+
: t('events.repeat.everyNDays', {
|
|
428
|
+
defaultValue: 'Every {{count}} days',
|
|
429
|
+
count: interval,
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
if (recurrence.freq === 'MONTHLY') {
|
|
434
|
+
return interval === 1
|
|
435
|
+
? t('events.repeat.monthly', { defaultValue: 'Monthly' })
|
|
436
|
+
: t('events.repeat.everyNMonths', {
|
|
437
|
+
defaultValue: 'Every {{count}} months',
|
|
438
|
+
count: interval,
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
const days =
|
|
443
|
+
recurrence.byDay?.length && recurrence.byDay.length > 0
|
|
444
|
+
? recurrence.byDay
|
|
445
|
+
: start
|
|
446
|
+
? [weekdayFromDate(new Date(start))]
|
|
447
|
+
: [];
|
|
448
|
+
const dayLabels = formatWeekdayLabels(days, t);
|
|
449
|
+
|
|
450
|
+
if (dayLabels) {
|
|
451
|
+
return interval === 1
|
|
452
|
+
? t('events.repeat.weeklyOn', {
|
|
453
|
+
defaultValue: 'Weekly on {{days}}',
|
|
454
|
+
days: dayLabels,
|
|
455
|
+
})
|
|
456
|
+
: t('events.repeat.everyNWeeksOn', {
|
|
457
|
+
defaultValue: 'Every {{count}} weeks on {{days}}',
|
|
458
|
+
count: interval,
|
|
459
|
+
days: dayLabels,
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
return interval === 1
|
|
464
|
+
? t('events.repeat.weekly', { defaultValue: 'Weekly' })
|
|
465
|
+
: t('events.repeat.everyNWeeks', {
|
|
466
|
+
defaultValue: 'Every {{count}} weeks',
|
|
467
|
+
count: interval,
|
|
468
|
+
});
|
|
469
|
+
};
|
|
470
|
+
|
|
471
|
+
export const formatEventRecurrence = (
|
|
472
|
+
recurrence: EventRecurrence | undefined,
|
|
473
|
+
t: EventRecurrenceTranslate,
|
|
474
|
+
start?: string,
|
|
475
|
+
): string => {
|
|
476
|
+
if (!recurrence) return '';
|
|
477
|
+
|
|
478
|
+
const pattern = formatRecurrencePattern(recurrence, t, start);
|
|
479
|
+
|
|
480
|
+
if (recurrence.until) {
|
|
481
|
+
return `${pattern} ${t('events.repeat.until', {
|
|
482
|
+
defaultValue: 'until {{date}}',
|
|
483
|
+
date: formatUntilDate(recurrence.until),
|
|
484
|
+
})}`;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
if (recurrence.count) {
|
|
488
|
+
return `${pattern}, ${t('events.repeat.times', {
|
|
489
|
+
defaultValue: '{{count}} times',
|
|
490
|
+
count: recurrence.count,
|
|
491
|
+
})}`;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
return pattern;
|
|
495
|
+
};
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { isIanaTimeZone } from './timeZone';
|
|
2
|
+
|
|
3
|
+
const DATETIME_LOCAL_RE =
|
|
4
|
+
/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})(?::(\d{2}))?$/;
|
|
5
|
+
|
|
6
|
+
const partNumber = (
|
|
7
|
+
parts: Intl.DateTimeFormatPart[],
|
|
8
|
+
type: Intl.DateTimeFormatPartTypes,
|
|
9
|
+
): number => Number(parts.find((part) => part.type === type)?.value);
|
|
10
|
+
|
|
11
|
+
/** Milliseconds to add to UTC to get wall time in `timeZone` at `utcMs`. */
|
|
12
|
+
const tzOffsetMs = (utcMs: number, timeZone: string): number => {
|
|
13
|
+
const parts = new Intl.DateTimeFormat('en-US', {
|
|
14
|
+
timeZone,
|
|
15
|
+
year: 'numeric',
|
|
16
|
+
month: '2-digit',
|
|
17
|
+
day: '2-digit',
|
|
18
|
+
hour: '2-digit',
|
|
19
|
+
minute: '2-digit',
|
|
20
|
+
second: '2-digit',
|
|
21
|
+
hourCycle: 'h23',
|
|
22
|
+
}).formatToParts(new Date(utcMs));
|
|
23
|
+
const hour = partNumber(parts, 'hour');
|
|
24
|
+
const asUtc = Date.UTC(
|
|
25
|
+
partNumber(parts, 'year'),
|
|
26
|
+
partNumber(parts, 'month') - 1,
|
|
27
|
+
partNumber(parts, 'day'),
|
|
28
|
+
hour === 24 ? 0 : hour,
|
|
29
|
+
partNumber(parts, 'minute'),
|
|
30
|
+
partNumber(parts, 'second'),
|
|
31
|
+
);
|
|
32
|
+
return asUtc - utcMs;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const pad2 = (value: number): string => String(value).padStart(2, '0');
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Interpret a datetime-local wall clock (`YYYY-MM-DDTHH:mm`) in `timeZone`
|
|
39
|
+
* and return a UTC ISO string.
|
|
40
|
+
*/
|
|
41
|
+
export const zonedDateTimeToUtcIso = (
|
|
42
|
+
local: string,
|
|
43
|
+
timeZone: string,
|
|
44
|
+
): string | undefined => {
|
|
45
|
+
if (!local) return undefined;
|
|
46
|
+
const match = DATETIME_LOCAL_RE.exec(local);
|
|
47
|
+
if (!match) {
|
|
48
|
+
const parsed = new Date(local);
|
|
49
|
+
return Number.isNaN(parsed.getTime()) ? undefined : parsed.toISOString();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const utcGuess = Date.UTC(
|
|
53
|
+
Number(match[1]),
|
|
54
|
+
Number(match[2]) - 1,
|
|
55
|
+
Number(match[3]),
|
|
56
|
+
Number(match[4]),
|
|
57
|
+
Number(match[5]),
|
|
58
|
+
Number(match[6] ?? 0),
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
try {
|
|
62
|
+
const offset = tzOffsetMs(utcGuess, timeZone);
|
|
63
|
+
const utcWithOffset = utcGuess - offset;
|
|
64
|
+
const offsetAtResult = tzOffsetMs(utcWithOffset, timeZone);
|
|
65
|
+
const utc =
|
|
66
|
+
offsetAtResult === offset ? utcWithOffset : utcGuess - offsetAtResult;
|
|
67
|
+
return new Date(utc).toISOString();
|
|
68
|
+
} catch {
|
|
69
|
+
return new Date(utcGuess).toISOString();
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
/** Format a UTC ISO instant as a datetime-local string in `timeZone`. */
|
|
74
|
+
export const utcIsoToZonedDateTime = (
|
|
75
|
+
iso: string,
|
|
76
|
+
timeZone: string,
|
|
77
|
+
): string => {
|
|
78
|
+
const date = new Date(iso);
|
|
79
|
+
if (Number.isNaN(date.getTime())) return '';
|
|
80
|
+
try {
|
|
81
|
+
const parts = new Intl.DateTimeFormat('en-US', {
|
|
82
|
+
timeZone,
|
|
83
|
+
year: 'numeric',
|
|
84
|
+
month: '2-digit',
|
|
85
|
+
day: '2-digit',
|
|
86
|
+
hour: '2-digit',
|
|
87
|
+
minute: '2-digit',
|
|
88
|
+
hourCycle: 'h23',
|
|
89
|
+
}).formatToParts(date);
|
|
90
|
+
const hour = partNumber(parts, 'hour');
|
|
91
|
+
const y = partNumber(parts, 'year');
|
|
92
|
+
const month = pad2(partNumber(parts, 'month'));
|
|
93
|
+
const day = pad2(partNumber(parts, 'day'));
|
|
94
|
+
const hh = pad2(hour === 24 ? 0 : hour);
|
|
95
|
+
const mm = pad2(partNumber(parts, 'minute'));
|
|
96
|
+
return `${y}-${month}-${day}T${hh}:${mm}`;
|
|
97
|
+
} catch {
|
|
98
|
+
return '';
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Keep the wall-clock time of `iso` (as seen in `fromTimeZone`) and express
|
|
104
|
+
* that same clock time in `toTimeZone`.
|
|
105
|
+
*/
|
|
106
|
+
export const reinterpretIsoInTimeZone = (
|
|
107
|
+
iso: string | undefined,
|
|
108
|
+
fromTimeZone: string,
|
|
109
|
+
toTimeZone: string,
|
|
110
|
+
): string | undefined => {
|
|
111
|
+
if (!iso) return undefined;
|
|
112
|
+
if (fromTimeZone === toTimeZone) return iso;
|
|
113
|
+
const wall = utcIsoToZonedDateTime(iso, fromTimeZone);
|
|
114
|
+
return wall ? zonedDateTimeToUtcIso(wall, toTimeZone) : iso;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
/** Build a Date whose local components match `iso` as seen in `timeZone`. */
|
|
118
|
+
export const utcIsoToWallClockDate = (iso: string, timeZone: string): Date => {
|
|
119
|
+
const local = utcIsoToZonedDateTime(iso, timeZone);
|
|
120
|
+
if (!local) return new Date(iso);
|
|
121
|
+
const [datePart, timePart] = local.split('T');
|
|
122
|
+
const [year, month, day] = datePart.split('-').map(Number);
|
|
123
|
+
const [hour, minute] = timePart.split(':').map(Number);
|
|
124
|
+
return new Date(year, month - 1, day, hour, minute);
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
/** Treat `date`'s local components as wall time in `timeZone`. */
|
|
128
|
+
export const wallClockDateToUtcIso = (date: Date, timeZone: string): string => {
|
|
129
|
+
const local = `${date.getFullYear()}-${pad2(date.getMonth() + 1)}-${pad2(
|
|
130
|
+
date.getDate(),
|
|
131
|
+
)}T${pad2(date.getHours())}:${pad2(date.getMinutes())}`;
|
|
132
|
+
return zonedDateTimeToUtcIso(local, timeZone) ?? date.toISOString();
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
/** Intl `timeZone` option when `timeZone` is a valid IANA identifier. */
|
|
136
|
+
export const eventTimeZoneOptions = (
|
|
137
|
+
timeZone?: string | null,
|
|
138
|
+
): Pick<Intl.DateTimeFormatOptions, 'timeZone'> => {
|
|
139
|
+
const resolved = isIanaTimeZone(timeZone) ? timeZone : undefined;
|
|
140
|
+
return resolved ? { timeZone: resolved } : {};
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
/** Clock time in the event timezone, always including a short zone name. */
|
|
144
|
+
export const formatEventClockTime = (
|
|
145
|
+
iso: string,
|
|
146
|
+
options?: {
|
|
147
|
+
timeZone?: string | null;
|
|
148
|
+
locale?: string;
|
|
149
|
+
},
|
|
150
|
+
): string => {
|
|
151
|
+
const timeZone = eventTimeZoneOptions(options?.timeZone).timeZone;
|
|
152
|
+
return new Date(iso).toLocaleTimeString(options?.locale, {
|
|
153
|
+
hour: '2-digit',
|
|
154
|
+
minute: '2-digit',
|
|
155
|
+
timeZoneName: 'short',
|
|
156
|
+
...(timeZone ? { timeZone } : {}),
|
|
157
|
+
});
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
export const formatEventWhen = (
|
|
161
|
+
start: string,
|
|
162
|
+
options?: {
|
|
163
|
+
end?: string | null;
|
|
164
|
+
timeZone?: string | null;
|
|
165
|
+
allDay?: boolean;
|
|
166
|
+
locale?: string;
|
|
167
|
+
},
|
|
168
|
+
): string => {
|
|
169
|
+
const timeZone = options?.timeZone || undefined;
|
|
170
|
+
const dateOptions: Intl.DateTimeFormatOptions = {
|
|
171
|
+
weekday: 'long',
|
|
172
|
+
year: 'numeric',
|
|
173
|
+
month: 'long',
|
|
174
|
+
day: 'numeric',
|
|
175
|
+
...(timeZone ? { timeZone } : {}),
|
|
176
|
+
};
|
|
177
|
+
if (options?.allDay) {
|
|
178
|
+
const startLabel = new Date(start).toLocaleDateString(
|
|
179
|
+
options.locale,
|
|
180
|
+
dateOptions,
|
|
181
|
+
);
|
|
182
|
+
if (!options.end) return startLabel;
|
|
183
|
+
const endLabel = new Date(options.end).toLocaleDateString(
|
|
184
|
+
options.locale,
|
|
185
|
+
dateOptions,
|
|
186
|
+
);
|
|
187
|
+
return `${startLabel} – ${endLabel}`;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const dateTimeOptions: Intl.DateTimeFormatOptions = {
|
|
191
|
+
...dateOptions,
|
|
192
|
+
hour: '2-digit',
|
|
193
|
+
minute: '2-digit',
|
|
194
|
+
...(timeZone ? { timeZoneName: 'short' } : {}),
|
|
195
|
+
};
|
|
196
|
+
const startLabel = new Date(start).toLocaleString(
|
|
197
|
+
options?.locale,
|
|
198
|
+
dateTimeOptions,
|
|
199
|
+
);
|
|
200
|
+
if (!options?.end) return startLabel;
|
|
201
|
+
const endLabel = new Date(options.end).toLocaleString(
|
|
202
|
+
options.locale,
|
|
203
|
+
dateTimeOptions,
|
|
204
|
+
);
|
|
205
|
+
return `${startLabel} – ${endLabel}`;
|
|
206
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** Leftmost-label wildcard only: `*.example.com` matches `a.example.com`. */
|
|
2
|
+
export const hostMatchesAllowedPattern = (
|
|
3
|
+
host: string,
|
|
4
|
+
pattern: string,
|
|
5
|
+
): boolean => {
|
|
6
|
+
const normalizedHost = host.trim().toLowerCase().replace(/\.$/, '');
|
|
7
|
+
const normalizedPattern = pattern.trim().toLowerCase().replace(/\.$/, '');
|
|
8
|
+
if (!normalizedHost || !normalizedPattern) return false;
|
|
9
|
+
if (normalizedPattern === '*') return false;
|
|
10
|
+
if (normalizedPattern.startsWith('*.')) {
|
|
11
|
+
const suffix = normalizedPattern.slice(2);
|
|
12
|
+
if (!suffix.includes('.')) return false;
|
|
13
|
+
return normalizedHost === suffix || normalizedHost.endsWith(`.${suffix}`);
|
|
14
|
+
}
|
|
15
|
+
return normalizedHost === normalizedPattern;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const hostIsAllowed = (
|
|
19
|
+
host: string,
|
|
20
|
+
allowedHosts: readonly string[],
|
|
21
|
+
): boolean =>
|
|
22
|
+
allowedHosts.some((pattern) => hostMatchesAllowedPattern(host, pattern));
|