98 lines
4.2 KiB
TypeScript
98 lines
4.2 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. Baseline functional UI
|
|
// (data-ui-id markers); the bespoke month/week grid follows David's D-P16 delta.
|
|
|
|
import { type LoaderFunctionArgs, useLoaderData } from "react-router";
|
|
import { useTranslation } from "react-i18next";
|
|
import { buildCalendarViewVM, calendarSourceKey, type CalendarViewEventRow, type CalendarViewVM } from "@hivegrid/view-models";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
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 ServerError from "~/components/ui/errors/ServerError";
|
|
|
|
type LoaderData = { title: string; vm: CalendarViewVM };
|
|
|
|
const WINDOW_PAST_DAYS = 7;
|
|
const WINDOW_FUTURE_DAYS = 30;
|
|
|
|
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}`;
|
|
|
|
if (!ctx.hasModule) {
|
|
return Response.json({ title, vm: buildCalendarViewVM({ events: [], rangeStart: "", rangeEnd: "" }) } satisfies LoaderData);
|
|
}
|
|
try {
|
|
requireCalendarPermission(ctx, CALENDAR_PERMISSIONS.connectOwn);
|
|
} catch {
|
|
throw notFoundResponse();
|
|
}
|
|
|
|
const now = new Date();
|
|
const rangeStart = new Date(now.getTime() - WINDOW_PAST_DAYS * 86_400_000);
|
|
const rangeEnd = new Date(now.getTime() + WINDOW_FUTURE_DAYS * 86_400_000);
|
|
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: rangeStart.toISOString().slice(0, 10), rangeEnd: rangeEnd.toISOString().slice(0, 10) });
|
|
return Response.json({ title, vm } satisfies LoaderData);
|
|
};
|
|
|
|
export default function CalendarViewRoute() {
|
|
const { t } = useTranslation();
|
|
const { vm } = useLoaderData<LoaderData>();
|
|
if (vm.status !== "ready" || !vm.data) {
|
|
return <main className="mx-auto max-w-5xl px-4 py-10" data-ui-id="ui.calendar.list" />;
|
|
}
|
|
const { events, emptyHeadlineKey, emptyBodyKey } = vm.data;
|
|
|
|
return (
|
|
<main className="mx-auto max-w-5xl px-4 py-10" data-ui-id="ui.calendar.list">
|
|
<h1 className="text-foreground text-xl font-semibold">{t("calendar.view.title")}</h1>
|
|
<p className="text-muted-foreground mt-1 text-sm">{t("calendar.view.subtitle")}</p>
|
|
{events.length === 0 ? (
|
|
<div className="mt-10 text-center" data-ui-id="ui.calendar.list.empty">
|
|
<p className="text-foreground font-medium">{t(emptyHeadlineKey)}</p>
|
|
<p className="text-muted-foreground mt-1 text-sm">{t(emptyBodyKey)}</p>
|
|
</div>
|
|
) : (
|
|
<ul className="mt-6 divide-border divide-y" data-ui-id="ui.calendar.list.events">
|
|
{events.map((event) => (
|
|
<li key={event.id} className="flex items-center justify-between py-3" data-row-id={event.id}>
|
|
<div>
|
|
<span className="text-foreground font-medium">{event.title || t("calendar.source." + event.source)}</span>
|
|
<p className="text-muted-foreground text-xs">{new Date(event.startsAt).toLocaleString()}</p>
|
|
</div>
|
|
<span className="text-muted-foreground text-xs" data-source={event.source}>
|
|
{t(event.sourceKey)}
|
|
</span>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
)}
|
|
</main>
|
|
);
|
|
}
|
|
|
|
export function ErrorBoundary() {
|
|
return <ServerError />;
|
|
}
|
|
|
|
export const handle = { moduleKey: CALENDAR_MODULE_KEY };
|