Bound continuous calendar event loading
This commit is contained in:
@@ -123,6 +123,45 @@ export function daysForMode(
|
||||
return daysBetween(range.start, range.end);
|
||||
}
|
||||
|
||||
export function continuousEventWindow(
|
||||
days: Date[],
|
||||
viewport: ContinuousViewport,
|
||||
options: {overscanWeeks?: number;minimumWeeks?: number;stepWeeks?: number;} = {},
|
||||
): Range {
|
||||
if (days.length === 0) {
|
||||
const now = startOfDay(new Date());
|
||||
return { start: now, end: addDays(now, 7) };
|
||||
}
|
||||
const totalWeeks = Math.max(1, Math.ceil(days.length / 7));
|
||||
const overscanWeeks = Math.max(0, Math.floor(options.overscanWeeks ?? 2));
|
||||
const minimumWeeks = Math.max(1, Math.floor(options.minimumWeeks ?? 10));
|
||||
const stepWeeks = Math.max(1, Math.floor(options.stepWeeks ?? 4));
|
||||
const firstVisibleWeek = Math.max(
|
||||
0,
|
||||
Math.min(totalWeeks - 1, Math.floor(viewport.scrollTop / CONTINUOUS_WEEK_ROW_HEIGHT)),
|
||||
);
|
||||
const visibleWeeks = Math.max(
|
||||
1,
|
||||
Math.ceil(Math.max(viewport.height, CONTINUOUS_WEEK_ROW_HEIGHT) / CONTINUOUS_WEEK_ROW_HEIGHT),
|
||||
);
|
||||
const desiredWeeks = Math.min(
|
||||
totalWeeks,
|
||||
Math.max(minimumWeeks, visibleWeeks + overscanWeeks * 2),
|
||||
);
|
||||
let startWeek = Math.max(
|
||||
0,
|
||||
Math.floor(Math.max(0, firstVisibleWeek - overscanWeeks) / stepWeeks) * stepWeeks,
|
||||
);
|
||||
if (startWeek + desiredWeeks > totalWeeks) {
|
||||
startWeek = Math.max(0, totalWeeks - desiredWeeks);
|
||||
}
|
||||
const endWeek = Math.min(totalWeeks, startWeek + desiredWeeks);
|
||||
return {
|
||||
start: days[startWeek * 7],
|
||||
end: addDays(days[Math.min(days.length - 1, endWeek * 7 - 1)], 1),
|
||||
};
|
||||
}
|
||||
|
||||
export function groupEventsByDay(
|
||||
events: CalendarEvent[],
|
||||
): Map<string, CalendarEvent[]> {
|
||||
|
||||
Reference in New Issue
Block a user