81 lines
3.9 KiB
TypeScript
81 lines
3.9 KiB
TypeScript
// AK-P16a · calendar view route (M25 Block B). Loader builds CalendarViewVM from
|
|
// the current user's own events (all sources) with source markers (DoD 5). Module
|
|
// off -> locked empty; missing calendar.connect_own -> 404. Interim month/week
|
|
// grid from ~/ui (AD-28); final Subframe optics follow David's D-P16 delta.
|
|
|
|
import { type LoaderFunctionArgs, useLoaderData } from "react-router";
|
|
import { addDaysIso, addMonthsIso, buildCalendarViewVM, calendarSourceKey, startOfWeekIso, type CalendarViewEventRow, type CalendarViewVM } from "@hivegrid/view-models";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
import { APP_TIME_ZONE } from "~/locale/format";
|
|
import { loadCalendarContext, requireCalendarPermission, CALENDAR_PERMISSIONS } from "~/modules/calendar/routes/calendarAccess.server";
|
|
import { CALENDAR_MODULE_KEY, notFoundResponse } from "~/modules/calendar/routes/calendarRoutePaths";
|
|
import { listEventsForUser } from "~/modules/calendar/db/calendar.db.server";
|
|
import { CalendarViewPage, type CalendarViewMode } from "~/components/screens/calendar/CalendarViewPage";
|
|
import ServerError from "~/components/ui/errors/ServerError";
|
|
|
|
type LoaderData = { title: string; vm: CalendarViewVM; view: CalendarViewMode; anchorIso: string; todayIso: string };
|
|
|
|
const DATE_RE = /^\d{4}-\d{2}-\d{2}$/u;
|
|
|
|
/** Today as a `YYYY-MM-DD` in the fixed app time zone (hydration rule). */
|
|
function todayIsoInAppTz(now: Date): string {
|
|
return new Intl.DateTimeFormat("en-CA", { timeZone: APP_TIME_ZONE, year: "numeric", month: "2-digit", day: "2-digit" }).format(now);
|
|
}
|
|
|
|
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
|
const { t } = await getTranslations(request);
|
|
const ctx = await loadCalendarContext({ request, params });
|
|
const title = `${t("calendar.view.title")} | ${process.env.APP_NAME}`;
|
|
|
|
const url = new URL(request.url);
|
|
const view: CalendarViewMode = url.searchParams.get("view") === "week" ? "week" : "month";
|
|
const todayIso = todayIsoInAppTz(new Date());
|
|
const anchorParam = url.searchParams.get("anchor");
|
|
const anchorIso = anchorParam && DATE_RE.test(anchorParam) ? anchorParam : todayIso;
|
|
|
|
if (!ctx.hasModule) {
|
|
return Response.json({ title, vm: buildCalendarViewVM({ events: [], rangeStart: "", rangeEnd: "" }), view, anchorIso, todayIso } satisfies LoaderData);
|
|
}
|
|
try {
|
|
requireCalendarPermission(ctx, CALENDAR_PERMISSIONS.connectOwn);
|
|
} catch {
|
|
throw notFoundResponse();
|
|
}
|
|
|
|
// Resolve the event window for the rendered grid (padded so the leading/
|
|
// trailing padding days of the month grid carry their events too).
|
|
const windowStartIso = view === "month" ? addDaysIso(`${anchorIso.slice(0, 7)}-01`, -7) : startOfWeekIso(anchorIso);
|
|
const windowEndIso = view === "month" ? addDaysIso(addMonthsIso(anchorIso, 1), 7) : addDaysIso(startOfWeekIso(anchorIso), 7);
|
|
const rangeStart = new Date(`${windowStartIso}T00:00:00Z`);
|
|
const rangeEnd = new Date(`${windowEndIso}T23:59:59Z`);
|
|
const events = await listEventsForUser(ctx.tenantId, ctx.userId, rangeStart, rangeEnd);
|
|
|
|
const rows: CalendarViewEventRow[] = events.map((e) => {
|
|
const link = e.internalEntityLink as { type?: string } | null;
|
|
return {
|
|
id: e.id,
|
|
source: e.source,
|
|
sourceKey: calendarSourceKey(e.source),
|
|
title: e.title ?? "",
|
|
startsAt: e.startsAt.toISOString(),
|
|
endsAt: e.endsAt.toISOString(),
|
|
isAllDay: e.isAllDay,
|
|
linkType: link?.type ?? null,
|
|
};
|
|
});
|
|
|
|
const vm = buildCalendarViewVM({ events: rows, rangeStart: windowStartIso, rangeEnd: windowEndIso });
|
|
return Response.json({ title, vm, view, anchorIso, todayIso } satisfies LoaderData);
|
|
};
|
|
|
|
export default function CalendarViewRoute() {
|
|
const { vm, view, anchorIso, todayIso } = useLoaderData<LoaderData>();
|
|
return <CalendarViewPage vm={vm} view={view} anchorIso={anchorIso} todayIso={todayIso} />;
|
|
}
|
|
|
|
export function ErrorBoundary() {
|
|
return <ServerError />;
|
|
}
|
|
|
|
export const handle = { moduleKey: CALENDAR_MODULE_KEY };
|