import { useNavigate } from "react-router";
import { useTranslation } from "react-i18next";
import { FeatherCalendarClock, FeatherChevronLeft, FeatherChevronRight } from "@subframe/core";
import {
addDaysIso,
addMonthsIso,
buildCalendarMonthGrid,
buildCalendarWeekDays,
type CalendarDayCell,
type CalendarViewEventRow,
type CalendarViewVM,
} from "@hivegrid/view-models";
import { Button } from "~/ui/components/Button";
import { IconButton } from "~/ui/components/IconButton";
import { ToggleGroup } from "~/ui/components/ToggleGroup";
import { BadgeFeedback } from "~/ui/components/BadgeFeedback";
import { useLocaleFormatters } from "~/locale/format";
import { PageHeader } from "../automation/automationUi";
/**
* AK-P16a ยท interim calendar view (AD-28): bespoke month/week event grid with
* per-event source markers (DoD 5), composed from ~/ui primitives. Navigation
* is GET-driven (`?view=&anchor=`) so the loader resolves the matching event
* window. Final Subframe optics follow David's D-P16 delta (design-nachzug).
*/
export type CalendarViewMode = "month" | "week";
export interface CalendarViewPageProps {
vm: CalendarViewVM;
view: CalendarViewMode;
/** ISO calendar dates (`YYYY-MM-DD`), resolved by the loader. */
anchorIso: string;
todayIso: string;
}
const MAX_VISIBLE_EVENTS = 3;
function viewHref(view: CalendarViewMode, anchorIso: string): string {
return `?view=${view}&anchor=${anchorIso}`;
}
function SourceDot({ source }: { source: CalendarViewEventRow["source"] }): React.JSX.Element {
return (
);
}
function DayEvents({ events, compact }: { events: readonly CalendarViewEventRow[]; compact: boolean }): React.JSX.Element {
const { t } = useTranslation();
const formatters = useLocaleFormatters();
const visible = compact ? events.slice(0, MAX_VISIBLE_EVENTS) : events;
const hidden = events.length - visible.length;
return (
{visible.map((event) => (
{event.isAllDay ? null : (
{formatters.date(event.startsAt, { hour: "2-digit", minute: "2-digit" })}
)}
{event.title || t(event.sourceKey)}
))}
{hidden > 0 ? (
{t("calendar.view.grid.more", { count: hidden })}
) : null}
);
}
function WeekdayHeader({ days }: { days: readonly CalendarDayCell[] }): React.JSX.Element {
const formatters = useLocaleFormatters();
return (
{days.map((day) => (
{formatters.date(`${day.dateIso}T12:00:00Z`, { weekday: "short" })}
))}
);
}
function DayCellBox({ cell, compact }: { cell: CalendarDayCell; compact: boolean }): React.JSX.Element {
return (
{cell.dayOfMonth}
);
}
export function CalendarViewPage({ vm, view, anchorIso, todayIso }: CalendarViewPageProps): React.JSX.Element {
const { t } = useTranslation();
const formatters = useLocaleFormatters();
const navigate = useNavigate();
if (vm.status !== "ready" || !vm.data) {
return ;
}
const { events, emptyHeadlineKey, emptyBodyKey } = vm.data;
const prevAnchor = view === "month" ? addMonthsIso(anchorIso, -1) : addDaysIso(anchorIso, -7);
const nextAnchor = view === "month" ? addMonthsIso(anchorIso, 1) : addDaysIso(anchorIso, 7);
const weeks = view === "month" ? buildCalendarMonthGrid({ events, anchorIso, todayIso }) : [buildCalendarWeekDays({ events, anchorIso, todayIso })];
const rangeLabel =
view === "month"
? formatters.date(`${anchorIso.slice(0, 7)}-01T12:00:00Z`, { month: "long", year: "numeric" })
: `${formatters.date(`${weeks[0]?.[0]?.dateIso}T12:00:00Z`, { day: "2-digit", month: "2-digit" })} โ ${formatters.date(`${weeks[0]?.[6]?.dateIso}T12:00:00Z`, { day: "2-digit", month: "2-digit", year: "numeric" })}`;
return (
}
title={t("calendar.view.title")}
subtitle={t("calendar.view.subtitle")}
actions={
<>
}
aria-label={t("calendar.view.grid.prev")}
onClick={() => navigate(viewHref(view, prevAnchor))}
/>
}
aria-label={t("calendar.view.grid.next")}
onClick={() => navigate(viewHref(view, nextAnchor))}
/>
{
if (value === "month" || value === "week") navigate(viewHref(value, anchorIso));
}}
>
{t("calendar.view.grid.viewMonth")}
{t("calendar.view.grid.viewWeek")}
>
}
/>
{rangeLabel}
{t("calendar.source.hivegrid_internal")}
{t("calendar.source.external_synced")}
{/* The grid always renders โ an empty range shows the calendar plus a slim hint instead of replacing it. */}
{events.length === 0 ? (
{t(emptyHeadlineKey)} โ {t(emptyBodyKey)}
) : null}
{weeks.flat().map((cell) => (
))}
);
}