153 lines
5.6 KiB
TypeScript
153 lines
5.6 KiB
TypeScript
import type { CalendarViewEventRow } from "./calendar-view.vm.js";
|
|
|
|
/**
|
|
* AK-P16a · pure month/week grid derivation for the interim calendar view
|
|
* (AD-28). Buckets the loader's events into day cells for a CSS-grid month
|
|
* (Monday-first weeks) or a single week. Day assignment uses a fixed IANA
|
|
* timezone (app default Europe/Berlin) so server and client render the same
|
|
* cells (hydration rule).
|
|
*/
|
|
|
|
export interface CalendarDayCell {
|
|
/** ISO calendar date `YYYY-MM-DD`. */
|
|
readonly dateIso: string;
|
|
readonly dayOfMonth: number;
|
|
/** False for leading/trailing days that pad the month grid to full weeks. */
|
|
readonly inAnchorMonth: boolean;
|
|
readonly isToday: boolean;
|
|
readonly events: readonly CalendarViewEventRow[];
|
|
}
|
|
|
|
export interface BuildCalendarGridInput {
|
|
readonly events: readonly CalendarViewEventRow[];
|
|
/** ISO calendar date `YYYY-MM-DD` inside the month/week to render. */
|
|
readonly anchorIso: string;
|
|
readonly todayIso: string;
|
|
/** IANA timezone used to bucket event timestamps into days. */
|
|
readonly timeZone?: string;
|
|
}
|
|
|
|
export const CALENDAR_GRID_DEFAULT_TIME_ZONE = "Europe/Berlin";
|
|
|
|
const DAY_MS = 86_400_000;
|
|
|
|
function assertDateIso(dateIso: string): void {
|
|
if (!/^\d{4}-\d{2}-\d{2}$/u.test(dateIso)) {
|
|
throw new Error(`Expected ISO calendar date (YYYY-MM-DD), got: ${dateIso}`);
|
|
}
|
|
}
|
|
|
|
function utcDate(dateIso: string): Date {
|
|
assertDateIso(dateIso);
|
|
return new Date(`${dateIso}T00:00:00Z`);
|
|
}
|
|
|
|
function toIso(date: Date): string {
|
|
return date.toISOString().slice(0, 10);
|
|
}
|
|
|
|
/** Add whole days to an ISO calendar date (pure UTC math). */
|
|
export function addDaysIso(dateIso: string, days: number): string {
|
|
return toIso(new Date(utcDate(dateIso).getTime() + days * DAY_MS));
|
|
}
|
|
|
|
/** Add whole months to an ISO calendar date, clamping to the 1st for grid anchors. */
|
|
export function addMonthsIso(dateIso: string, months: number): string {
|
|
const date = utcDate(dateIso);
|
|
const shifted = new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth() + months, 1));
|
|
return toIso(shifted);
|
|
}
|
|
|
|
/** Monday of the ISO week containing the date. */
|
|
export function startOfWeekIso(dateIso: string): string {
|
|
const date = utcDate(dateIso);
|
|
const weekday = date.getUTCDay(); // 0 = Sunday
|
|
const offset = weekday === 0 ? 6 : weekday - 1;
|
|
return addDaysIso(dateIso, -offset);
|
|
}
|
|
|
|
function dayKeyFormatter(timeZone: string): Intl.DateTimeFormat {
|
|
// en-CA yields YYYY-MM-DD directly.
|
|
return new Intl.DateTimeFormat("en-CA", { timeZone, year: "numeric", month: "2-digit", day: "2-digit" });
|
|
}
|
|
|
|
/** Bucket events by the calendar dates they overlap in the given timezone. */
|
|
function bucketEventsByDay(events: readonly CalendarViewEventRow[], timeZone: string): Map<string, CalendarViewEventRow[]> {
|
|
const formatter = dayKeyFormatter(timeZone);
|
|
const byDay = new Map<string, CalendarViewEventRow[]>();
|
|
const sorted = events.slice().sort((a, b) => a.startsAt.localeCompare(b.startsAt) || a.id.localeCompare(b.id));
|
|
for (const event of sorted) {
|
|
const startsAt = new Date(event.startsAt);
|
|
const endsAt = new Date(event.endsAt);
|
|
if (Number.isNaN(startsAt.getTime())) {
|
|
continue;
|
|
}
|
|
const startKey = formatter.format(startsAt);
|
|
// Treat the end as exclusive when the event has an extent, so an all-day
|
|
// event ending at midnight does not spill into the next day cell.
|
|
const effectiveEnd = !Number.isNaN(endsAt.getTime()) && endsAt.getTime() > startsAt.getTime() ? new Date(endsAt.getTime() - 1) : startsAt;
|
|
const endKey = formatter.format(effectiveEnd);
|
|
for (let key = startKey, guard = 0; guard < 62; key = addDaysIso(key, 1), guard += 1) {
|
|
const bucket = byDay.get(key);
|
|
if (bucket) {
|
|
bucket.push(event);
|
|
} else {
|
|
byDay.set(key, [event]);
|
|
}
|
|
if (key === endKey) {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return byDay;
|
|
}
|
|
|
|
function buildCell(dateIso: string, anchorMonth: string, todayIso: string, byDay: ReadonlyMap<string, CalendarViewEventRow[]>): CalendarDayCell {
|
|
return {
|
|
dateIso,
|
|
dayOfMonth: Number(dateIso.slice(8, 10)),
|
|
inAnchorMonth: dateIso.slice(0, 7) === anchorMonth,
|
|
isToday: dateIso === todayIso,
|
|
events: byDay.get(dateIso) ?? [],
|
|
};
|
|
}
|
|
|
|
/** Monday-first month grid (full weeks) around the anchor's month. */
|
|
export function buildCalendarMonthGrid(input: BuildCalendarGridInput): CalendarDayCell[][] {
|
|
assertDateIso(input.anchorIso);
|
|
assertDateIso(input.todayIso);
|
|
const timeZone = input.timeZone ?? CALENDAR_GRID_DEFAULT_TIME_ZONE;
|
|
const anchorMonth = input.anchorIso.slice(0, 7);
|
|
const firstOfMonth = `${anchorMonth}-01`;
|
|
const gridStart = startOfWeekIso(firstOfMonth);
|
|
const firstOfNextMonth = addMonthsIso(firstOfMonth, 1);
|
|
const byDay = bucketEventsByDay(input.events, timeZone);
|
|
|
|
const weeks: CalendarDayCell[][] = [];
|
|
let cursor = gridStart;
|
|
do {
|
|
const week: CalendarDayCell[] = [];
|
|
for (let i = 0; i < 7; i += 1) {
|
|
week.push(buildCell(cursor, anchorMonth, input.todayIso, byDay));
|
|
cursor = addDaysIso(cursor, 1);
|
|
}
|
|
weeks.push(week);
|
|
} while (cursor < firstOfNextMonth);
|
|
return weeks;
|
|
}
|
|
|
|
/** Monday-first single week containing the anchor. */
|
|
export function buildCalendarWeekDays(input: BuildCalendarGridInput): CalendarDayCell[] {
|
|
assertDateIso(input.anchorIso);
|
|
assertDateIso(input.todayIso);
|
|
const timeZone = input.timeZone ?? CALENDAR_GRID_DEFAULT_TIME_ZONE;
|
|
const anchorMonth = input.anchorIso.slice(0, 7);
|
|
const byDay = bucketEventsByDay(input.events, timeZone);
|
|
const start = startOfWeekIso(input.anchorIso);
|
|
const days: CalendarDayCell[] = [];
|
|
for (let i = 0; i < 7; i += 1) {
|
|
days.push(buildCell(addDaysIso(start, i), anchorMonth, input.todayIso, byDay));
|
|
}
|
|
return days;
|
|
}
|