Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+29
-46
@@ -1,29 +1,40 @@
|
||||
// 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.
|
||||
// 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 { useTranslation } from "react-i18next";
|
||||
import { buildCalendarViewVM, calendarSourceKey, type CalendarViewEventRow, type CalendarViewVM } from "@hivegrid/view-models";
|
||||
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 };
|
||||
type LoaderData = { title: string; vm: CalendarViewVM; view: CalendarViewMode; anchorIso: string; todayIso: string };
|
||||
|
||||
const WINDOW_PAST_DAYS = 7;
|
||||
const WINDOW_FUTURE_DAYS = 30;
|
||||
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: "" }) } satisfies LoaderData);
|
||||
return Response.json({ title, vm: buildCalendarViewVM({ events: [], rangeStart: "", rangeEnd: "" }), view, anchorIso, todayIso } satisfies LoaderData);
|
||||
}
|
||||
try {
|
||||
requireCalendarPermission(ctx, CALENDAR_PERMISSIONS.connectOwn);
|
||||
@@ -31,9 +42,12 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
||||
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);
|
||||
// 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) => {
|
||||
@@ -50,44 +64,13 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
||||
};
|
||||
});
|
||||
|
||||
const vm = buildCalendarViewVM({ events: rows, rangeStart: rangeStart.toISOString().slice(0, 10), rangeEnd: rangeEnd.toISOString().slice(0, 10) });
|
||||
return Response.json({ title, vm } satisfies LoaderData);
|
||||
const vm = buildCalendarViewVM({ events: rows, rangeStart: windowStartIso, rangeEnd: windowEndIso });
|
||||
return Response.json({ title, vm, view, anchorIso, todayIso } 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>
|
||||
);
|
||||
const { vm, view, anchorIso, todayIso } = useLoaderData<LoaderData>();
|
||||
return <CalendarViewPage vm={vm} view={view} anchorIso={anchorIso} todayIso={todayIso} />;
|
||||
}
|
||||
|
||||
export function ErrorBoundary() {
|
||||
|
||||
Reference in new issue
Block a user