Add a 'days with events' preference (default 3, range 1-7) that drives the upcoming events widget, and extend the fetch window to 8 days so the largest window can populate. Add a Calendar settings pane (moved under Integrations) that lists the connected CalDAV/ICS sources in a two-column grid with add/edit/remove, and remove calendars from Manage Integrations. Turn the modal service into a stack so add/edit forms open on top of the settings dialog, with one dialog per level, per-entry title ids, and a softened stacked backdrop. Move CALENDAR_ACTOR into its own config module to break the calendar/caldav/ics import cycle.
254 lines
7.4 KiB
JavaScript
254 lines
7.4 KiB
JavaScript
// Shared configuration for the calendar integrations and their event cache.
|
|
import { CALDAV_TYPE } from './caldav';
|
|
import { ICS_TYPE } from './ics';
|
|
|
|
// Persistent cache namespace for fetched events.
|
|
export const CALENDAR_EVENTS_NAMESPACE = 'events';
|
|
|
|
// Re-exported for callers that read it from the calendar config.
|
|
export { CALENDAR_ACTOR } from './calendar-actor';
|
|
|
|
// Fetch a week-plus ahead; the dashboard shows a configurable number of day
|
|
// groups in total: today plus the next days that have events when today itself
|
|
// has events, or that many days when today is empty (empty days are skipped).
|
|
// The fetch window reaches one day past the largest visible window so the last
|
|
// day group can always be populated.
|
|
export const CALENDAR_FETCH_DAYS = 8;
|
|
export const CALENDAR_VISIBLE_DAYS = 3;
|
|
export const CALENDAR_VISIBLE_DAYS_MIN = 1;
|
|
export const CALENDAR_VISIBLE_DAYS_MAX = 7;
|
|
|
|
// Storage namespace/key for the user's chosen day count.
|
|
export const CALENDAR_VISIBLE_DAYS_KEY = 'calendar-visible-days';
|
|
|
|
// Select options for the "days with events" setting.
|
|
export const CALENDAR_VISIBLE_DAYS_OPTIONS = Array.from(
|
|
{ length: CALENDAR_VISIBLE_DAYS_MAX - CALENDAR_VISIBLE_DAYS_MIN + 1 },
|
|
(_, index) => {
|
|
const value = CALENDAR_VISIBLE_DAYS_MIN + index;
|
|
return { value, label: `${value} day${value === 1 ? '' : 's'}` };
|
|
}
|
|
);
|
|
|
|
// The integration type ids that make up a calendar source.
|
|
export const CALENDAR_TYPES = [CALDAV_TYPE, ICS_TYPE];
|
|
|
|
// Clamps a stored or selected value to a usable day count, falling back to the
|
|
// default for anything out of range.
|
|
export function normalizeVisibleDays(value) {
|
|
const days = Number(value);
|
|
|
|
if (
|
|
Number.isInteger(days) &&
|
|
days >= CALENDAR_VISIBLE_DAYS_MIN &&
|
|
days <= CALENDAR_VISIBLE_DAYS_MAX
|
|
) {
|
|
return days;
|
|
}
|
|
|
|
return CALENDAR_VISIBLE_DAYS;
|
|
}
|
|
|
|
// Background refresh interval for the events cache.
|
|
export const CALENDAR_REFRESH_INTERVAL = 900000;
|
|
|
|
// The Sockethub caldav platform id (used for context and messages).
|
|
export const CALDAV_PLATFORM = 'caldav';
|
|
|
|
// The fetch window: from now through local midnight plus CALENDAR_FETCH_DAYS.
|
|
// Returned as ISO (UTC) strings, which is what the caldav range filter wants.
|
|
export function fetchWindow(now = new Date()) {
|
|
const end = new Date(now);
|
|
end.setHours(0, 0, 0, 0);
|
|
end.setDate(end.getDate() + CALENDAR_FETCH_DAYS);
|
|
|
|
return { startTime: now.toISOString(), endTime: end.toISOString() };
|
|
}
|
|
|
|
// Parses a Sockethub item time into a Date. All-day values (`YYYY-MM-DD`) and
|
|
// values flagged `allDay` are read as local dates so they do not drift a day
|
|
// when interpreted as UTC.
|
|
export function parseEventDate(value, allDay = false) {
|
|
if (!value) {
|
|
return null;
|
|
}
|
|
|
|
if (allDay || /^\d{4}-\d{2}-\d{2}$/.test(value)) {
|
|
const [year, month, day] = value.split('-').map(Number);
|
|
return new Date(year, month - 1, day);
|
|
}
|
|
|
|
const date = new Date(value);
|
|
return Number.isNaN(date.getTime()) ? null : date;
|
|
}
|
|
|
|
// Normalizes a Sockethub event item. Only `event` items are kept; recurring
|
|
// items are dropped for now (recurrences are not expanded).
|
|
export function normalizeEvent(item) {
|
|
if (!item || item.type !== 'event' || item.recurrence) {
|
|
return null;
|
|
}
|
|
|
|
const startTime = item.startTime;
|
|
if (!startTime) {
|
|
return null;
|
|
}
|
|
|
|
return {
|
|
id: item.id ?? item.uid ?? startTime,
|
|
name: item.name ?? 'Untitled event',
|
|
startTime,
|
|
endTime: item.endTime ?? null,
|
|
allDay: Boolean(item.allDay),
|
|
location: item.location ?? null,
|
|
};
|
|
}
|
|
|
|
function startOfDay(date) {
|
|
const day = new Date(date);
|
|
day.setHours(0, 0, 0, 0);
|
|
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()}`;
|
|
}
|
|
|
|
function eventStart(event) {
|
|
return parseEventDate(event.startTime, event.allDay);
|
|
}
|
|
|
|
function eventEnd(event) {
|
|
const end = parseEventDate(event.endTime, event.allDay);
|
|
if (end) {
|
|
return end;
|
|
}
|
|
|
|
const start = eventStart(event);
|
|
if (event.allDay && start) {
|
|
const next = new Date(start);
|
|
next.setDate(next.getDate() + 1);
|
|
return next;
|
|
}
|
|
|
|
return start;
|
|
}
|
|
|
|
function compareEvents(a, b) {
|
|
if (a.allDay !== b.allDay) {
|
|
return a.allDay ? -1 : 1;
|
|
}
|
|
|
|
const aStart = eventStart(a)?.getTime() ?? 0;
|
|
const bStart = eventStart(b)?.getTime() ?? 0;
|
|
return aStart - bStart;
|
|
}
|
|
|
|
// Splits events into a prominent "today" list (now through local midnight) and
|
|
// 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 = 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) {
|
|
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, Math.max(futureLimit, 0))
|
|
.map((day) => ({ ...day, events: day.events.sort(compareEvents) }));
|
|
|
|
return { today: todayEvents.sort(compareEvents), days };
|
|
}
|
|
|
|
// Maps a Sockethub / Sockethub client failure to a user-facing message.
|
|
export function classifyCalendarError(err) {
|
|
if (err?.name === 'SockethubConnectionError') {
|
|
return 'Unable to reach the Sockethub server';
|
|
}
|
|
|
|
if (err?.name === 'SockethubPlatformError') {
|
|
switch (err.message) {
|
|
case 'caldav:authentication-failed':
|
|
return 'Invalid username or password';
|
|
case 'caldav:connection-failed':
|
|
return 'Unable to reach the calendar server';
|
|
case 'caldav:invalid-feed':
|
|
case 'caldav:feed-failed':
|
|
return 'Unable to read that calendar link';
|
|
default:
|
|
return err.message;
|
|
}
|
|
}
|
|
|
|
return 'Unable to load calendar events';
|
|
}
|