Repeat multi-day all-day events across the visible days

An all-day event was only listed on the day it started, so a holiday spanning several days disappeared after the first one. groupEvents now fans an all-day event out to every day it covers (its end is exclusive, per RFC 5545), clamped to the visible window so a long event cannot spread without bound.

The widget also now shows three day groups in total: today plus the next two days that have events when today has events, or the next three days that have events when today is empty.

Drop the duplicated inline day stepping in favor of a small addDays helper, and cover the new grouping with unit tests.
This commit is contained in:
raucao committed 2026-10-29 20:00:07 +04:00
1 parent a6542f1419
commit c94fc9070b
2 files changed
+169 -17

No files matched your search

+57 -15
View File
@@ -7,10 +7,12 @@ export const CALENDAR_EVENTS_NAMESPACE = 'events';
// the credentials, fetch, and query messages so they share one session.
export const CALENDAR_ACTOR = { id: 'caldav:dashtab', type: 'person' };
// Fetch a week ahead; the dashboard shows today plus the next two days that
// have events (empty days are skipped).
// Fetch a week ahead; the dashboard shows three day groups in total: today
// plus the next two days that have events when today itself has events, or the
// next three days that have events when today is empty (empty days are
// skipped).
export const CALENDAR_FETCH_DAYS = 7;
export const CALENDAR_VISIBLE_DAYS = 2;
export const CALENDAR_VISIBLE_DAYS = 3;
// Background refresh interval for the events cache.
export const CALENDAR_REFRESH_INTERVAL = 900000;
@@ -73,6 +75,12 @@ function startOfDay(date) {
return day;
}
function addDays(date, count) {
const next = new Date(date);
next.setDate(next.getDate() + count);
return next;
}
function dayKey(date) {
return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
}
@@ -108,45 +116,79 @@ function compareEvents(a, b) {
}
// Splits events into a prominent "today" list (now through local midnight) and
// up to `visibleDays` future days that have events, skipping empty ones.
// the future days that follow it. At most `visibleDays` day groups are shown in
// total: today counts as one when it has events, and an empty today is skipped
// so its slot goes to the next day that has events. A multi-day all-day event
// appears on every covered day inside the window.
export function groupEvents(
events,
now = new Date(),
visibleDays = CALENDAR_VISIBLE_DAYS
) {
const today = startOfDay(now);
const tomorrow = new Date(today);
tomorrow.setDate(tomorrow.getDate() + 1);
const tomorrow = addDays(today, 1);
// Future all-day events are clamped to the visible window so a very long
// event cannot fan out across an unbounded number of days.
const windowEnd = addDays(today, visibleDays + 1);
const todayEvents = [];
const futureByDay = new Map();
const bucketFor = (day) => {
const key = dayKey(day);
const bucket = futureByDay.get(key) ?? {
key,
date: startOfDay(day),
events: [],
};
futureByDay.set(key, bucket);
return bucket;
};
for (const event of events) {
const start = eventStart(event);
if (!start) {
continue;
}
// An all-day event spans whole days, so it shows up on every day it
// covers (end is exclusive), not just the day it starts.
if (event.allDay) {
const end = eventEnd(event) ?? start;
const first = startOfDay(start);
for (
let day = first;
day < end && day < windowEnd;
day = addDays(day, 1)
) {
if (day.getTime() === today.getTime()) {
todayEvents.push(event);
} else if (day > today) {
bucketFor(day).events.push(event);
}
}
continue;
}
if (start < tomorrow && (eventEnd(event) ?? start) >= now) {
todayEvents.push(event);
continue;
}
if (start >= tomorrow) {
const key = dayKey(start);
const bucket = futureByDay.get(key) ?? {
key,
date: startOfDay(start),
events: [],
};
bucket.events.push(event);
futureByDay.set(key, bucket);
bucketFor(start).events.push(event);
}
}
// Today occupies one of the visible slots when it has events; otherwise the
// empty day is skipped and one more future day fills the list.
const futureLimit = todayEvents.length > 0 ? visibleDays - 1 : visibleDays;
const days = [...futureByDay.values()]
.sort((a, b) => a.date - b.date)
.slice(0, visibleDays)
.slice(0, Math.max(futureLimit, 0))
.map((day) => ({ ...day, events: day.events.sort(compareEvents) }));
return { today: todayEvents.sort(compareEvents), days };
+112 -2
View File
@@ -94,7 +94,7 @@ module('Unit | Config | calendar', function () {
{ id: 'day3', startTime: '2026-10-30T09:00:00' },
];
const grouped = groupEvents(events, now, 2);
const grouped = groupEvents(events, now, 3);
assert.deepEqual(
grouped.today.map((event) => event.id),
@@ -115,7 +115,7 @@ module('Unit | Config | calendar', function () {
const grouped = groupEvents(
[{ id: 'holiday', startTime: '2026-10-27', allDay: true }],
now,
2
3
);
assert.deepEqual(
@@ -124,6 +124,116 @@ module('Unit | Config | calendar', function () {
);
});
test('groupEvents repeats a multi-day all-day event on every day it covers', function (assert) {
const now = new Date(2026, 9, 27, 15, 0);
const grouped = groupEvents(
[
{
id: 'trip',
startTime: '2026-10-27',
endTime: '2026-10-30',
allDay: true,
},
],
now,
3
);
assert.deepEqual(
grouped.today.map((event) => event.id),
['trip']
);
assert.deepEqual(
grouped.days.map((day) => day.key),
['2026-10-28', '2026-10-29']
);
assert.deepEqual(
grouped.days.map((day) => day.events.map((event) => event.id)),
[['trip'], ['trip']]
);
});
test('groupEvents shows the next three days when today is empty', function (assert) {
const now = new Date(2026, 9, 27, 15, 0);
const grouped = groupEvents(
[
{
id: 'conf',
startTime: '2026-10-28',
endTime: '2026-10-31',
allDay: true,
},
],
now,
3
);
assert.deepEqual(grouped.today, []);
assert.deepEqual(
grouped.days.map((day) => day.key),
['2026-10-28', '2026-10-29', '2026-10-30']
);
assert.deepEqual(
grouped.days.map((day) => day.events.map((event) => event.id)),
[['conf'], ['conf'], ['conf']]
);
});
test('groupEvents caps the future days at the visible limit', function (assert) {
const now = new Date(2026, 9, 27, 15, 0);
const grouped = groupEvents(
[
{
id: 'sprint',
startTime: '2026-10-28',
endTime: '2026-11-03',
allDay: true,
},
],
now,
3
);
assert.deepEqual(grouped.today, []);
assert.deepEqual(
grouped.days.map((day) => day.key),
['2026-10-28', '2026-10-29', '2026-10-30']
);
assert.deepEqual(
grouped.days.map((day) => day.events.map((event) => event.id)),
[['sprint'], ['sprint'], ['sprint']]
);
});
test('groupEvents shows an ongoing all-day event as today but skips past days', function (assert) {
const now = new Date(2026, 9, 27, 15, 0);
const grouped = groupEvents(
[
{
id: 'retreat',
startTime: '2026-10-25',
endTime: '2026-10-29',
allDay: true,
},
],
now,
3
);
assert.deepEqual(
grouped.today.map((event) => event.id),
['retreat']
);
assert.deepEqual(
grouped.days.map((day) => day.key),
['2026-10-28']
);
assert.deepEqual(
grouped.days.map((day) => day.events.map((event) => event.id)),
[['retreat']]
);
});
test('classifyCalendarError maps platform and connection failures', function (assert) {
assert.strictEqual(
classifyCalendarError({ name: 'SockethubConnectionError' }),