212 lines
9.0 KiB
TypeScript
212 lines
9.0 KiB
TypeScript
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 (
|
||
<span
|
||
className={
|
||
source === "hivegrid_internal"
|
||
? "mt-1 flex h-1.5 w-1.5 flex-none rounded-round bg-surface-feedback-solid-information"
|
||
: "mt-1 flex h-1.5 w-1.5 flex-none rounded-round bg-neutral-400"
|
||
}
|
||
/>
|
||
);
|
||
}
|
||
|
||
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 (
|
||
<div className="flex w-full flex-col gap-1">
|
||
{visible.map((event) => (
|
||
<div key={event.id} className="flex w-full items-start gap-1.5" data-row-id={event.id} data-source={event.source} title={t(event.sourceKey)}>
|
||
<SourceDot source={event.source} />
|
||
<span className="min-w-0 grow truncate text-body-extra-small-regular-0 font-body-extra-small-regular-0 text-text-neutral-primary">
|
||
{event.isAllDay ? null : (
|
||
<span className="text-text-neutral-tertiary">{formatters.date(event.startsAt, { hour: "2-digit", minute: "2-digit" })} </span>
|
||
)}
|
||
{event.title || t(event.sourceKey)}
|
||
</span>
|
||
</div>
|
||
))}
|
||
{hidden > 0 ? (
|
||
<span className="text-body-extra-small-regular-0 font-body-extra-small-regular-0 text-text-neutral-tertiary">
|
||
{t("calendar.view.grid.more", { count: hidden })}
|
||
</span>
|
||
) : null}
|
||
</div>
|
||
);
|
||
}
|
||
|
||
function WeekdayHeader({ days }: { days: readonly CalendarDayCell[] }): React.JSX.Element {
|
||
const formatters = useLocaleFormatters();
|
||
return (
|
||
<div className="grid w-full grid-cols-7 border-b border-solid border-border-default">
|
||
{days.map((day) => (
|
||
<span key={day.dateIso} className="px-2 py-2 text-center text-body-extra-small-semibold-0 font-body-extra-small-semibold-0 uppercase tracking-wide text-text-neutral-tertiary">
|
||
{formatters.date(`${day.dateIso}T12:00:00Z`, { weekday: "short" })}
|
||
</span>
|
||
))}
|
||
</div>
|
||
);
|
||
}
|
||
|
||
function DayCellBox({ cell, compact }: { cell: CalendarDayCell; compact: boolean }): React.JSX.Element {
|
||
return (
|
||
<div
|
||
className={
|
||
cell.inAnchorMonth
|
||
? "flex min-h-[96px] flex-col gap-1 border-b border-r border-solid border-border-default bg-surface-neutral-primary p-2"
|
||
: "flex min-h-[96px] flex-col gap-1 border-b border-r border-solid border-border-default bg-surface-neutral-secondary p-2"
|
||
}
|
||
data-date={cell.dateIso}
|
||
>
|
||
<span
|
||
className={
|
||
cell.isToday
|
||
? "flex h-6 w-6 items-center justify-center rounded-round bg-brand-brand-surface-solid-default text-body-extra-small-semibold-0 font-body-extra-small-semibold-0 text-text-neutral-on-solid"
|
||
: cell.inAnchorMonth
|
||
? "text-body-extra-small-semibold-0 font-body-extra-small-semibold-0 text-text-neutral-primary"
|
||
: "text-body-extra-small-regular-0 font-body-extra-small-regular-0 text-text-neutral-tertiary"
|
||
}
|
||
>
|
||
{cell.dayOfMonth}
|
||
</span>
|
||
<DayEvents events={cell.events} compact={compact} />
|
||
</div>
|
||
);
|
||
}
|
||
|
||
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 <div className="flex w-full flex-col" data-ui-id="ui.calendar.list" />;
|
||
}
|
||
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 (
|
||
<div className="flex w-full flex-col" data-ui-id="ui.calendar.list">
|
||
<PageHeader
|
||
icon={<FeatherCalendarClock className="text-heading-3 font-heading-3 text-icon-neutral-secondary" />}
|
||
title={t("calendar.view.title")}
|
||
subtitle={t("calendar.view.subtitle")}
|
||
actions={
|
||
<>
|
||
<Button variant="secondary" size="medium" onClick={() => navigate(viewHref(view, todayIso))}>
|
||
{t("calendar.view.grid.today")}
|
||
</Button>
|
||
<IconButton
|
||
variant="secondary"
|
||
size="medium"
|
||
icon={<FeatherChevronLeft />}
|
||
aria-label={t("calendar.view.grid.prev")}
|
||
onClick={() => navigate(viewHref(view, prevAnchor))}
|
||
/>
|
||
<IconButton
|
||
variant="secondary"
|
||
size="medium"
|
||
icon={<FeatherChevronRight />}
|
||
aria-label={t("calendar.view.grid.next")}
|
||
onClick={() => navigate(viewHref(view, nextAnchor))}
|
||
/>
|
||
<ToggleGroup
|
||
value={view}
|
||
onValueChange={(value: string) => {
|
||
if (value === "month" || value === "week") navigate(viewHref(value, anchorIso));
|
||
}}
|
||
>
|
||
<ToggleGroup.Item value="month" icon={null} aria-label={t("calendar.view.grid.viewMonth")}>
|
||
{t("calendar.view.grid.viewMonth")}
|
||
</ToggleGroup.Item>
|
||
<ToggleGroup.Item value="week" icon={null} aria-label={t("calendar.view.grid.viewWeek")}>
|
||
{t("calendar.view.grid.viewWeek")}
|
||
</ToggleGroup.Item>
|
||
</ToggleGroup>
|
||
</>
|
||
}
|
||
/>
|
||
|
||
<div className="flex w-full flex-col gap-4 p-6 mobile:p-4">
|
||
<div className="flex w-full flex-wrap items-center justify-between gap-4">
|
||
<span className="text-heading-3 font-heading-3 text-text-neutral-primary">{rangeLabel}</span>
|
||
<div className="flex flex-wrap items-center gap-2" data-ui-id="ui.calendar.list.legend">
|
||
<BadgeFeedback farbe="subtle-information" size="small">
|
||
{t("calendar.source.hivegrid_internal")}
|
||
</BadgeFeedback>
|
||
<BadgeFeedback farbe="subtle-neutral" size="small">
|
||
{t("calendar.source.external_synced")}
|
||
</BadgeFeedback>
|
||
</div>
|
||
</div>
|
||
|
||
{/* The grid always renders — an empty range shows the calendar plus a slim hint instead of replacing it. */}
|
||
{events.length === 0 ? (
|
||
<div data-ui-id="ui.calendar.list.empty" className="text-body-small-regular-0 font-body-small-regular-0 text-text-neutral-secondary">
|
||
{t(emptyHeadlineKey)} — {t(emptyBodyKey)}
|
||
</div>
|
||
) : null}
|
||
<div className="w-full overflow-hidden rounded-lg border border-solid border-border-default" data-ui-id="ui.calendar.list.events">
|
||
<WeekdayHeader days={weeks[0] ?? []} />
|
||
<div className="grid w-full grid-cols-7 border-l border-solid border-border-default">
|
||
{weeks.flat().map((cell) => (
|
||
<DayCellBox key={cell.dateIso} cell={cell} compact={view === "month"} />
|
||
))}
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|