1 import { getUnixTime } from 'date-fns';
3 import { queryCalendarAlarms } from '@proton/shared/lib/api/calendars';
4 import type { Api } from '@proton/shared/lib/interfaces';
5 import type { CalendarAlarm } from '@proton/shared/lib/interfaces/calendar';
7 const MAX_FETCH_ITERATIONS = 100;
9 const getPaginatedAlarms = async (api: Api, calendarID: string, dateRange: Date[]) => {
10 let results: CalendarAlarm[] = [];
15 End: getUnixTime(dateRange[1]),
19 let lastStart: number | undefined = getUnixTime(dateRange[0]);
22 while (lastStart !== undefined && iterations < MAX_FETCH_ITERATIONS) {
23 // https://github.com/microsoft/TypeScript/issues/36687
25 const { Alarms = [] } = await api<{ Alarms: CalendarAlarm[] }>(
26 queryCalendarAlarms(calendarID, { ...params, Start: lastStart })
28 const lastAlarm = Alarms.length > 0 ? Alarms[Alarms.length - 1] : undefined;
29 results = results.concat(Alarms);
30 lastStart = Alarms.length === PageSize && lastAlarm ? lastAlarm.Occurrence : undefined;
37 export default getPaginatedAlarms;