175 lines
7.4 KiB
TypeScript
175 lines
7.4 KiB
TypeScript
// AK-P3 Interim-UI (AD-28) — Notification-Center preview screen composed from
|
|
// ~/ui. Pure presentational component: renders the (live-merged) VM passed as a
|
|
// prop; mark-read/bulk-clear run through injected action callbacks (M014), the
|
|
// SSE subscription lives in the hosting route (useNotificationsStream). Final
|
|
// Subframe optics land as a tracked design-nachzug once David's D-P3 ships.
|
|
|
|
import { useState } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
import type { NotificationCenterItemVM, NotificationCenterVM } from "@hivegrid/view-models";
|
|
import { Alert } from "~/ui/components/Alert";
|
|
import { BadgeFeedback } from "~/ui/components/BadgeFeedback";
|
|
import { Button } from "~/ui/components/Button";
|
|
import { EmptyState } from "~/ui/components/EmptyState";
|
|
import { Tabs } from "~/ui/components/Tabs";
|
|
|
|
export type NotificationCenterActions = {
|
|
onMarkRead: (deliveryId: string) => void;
|
|
onMarkAllRead: () => void;
|
|
};
|
|
|
|
export type NotificationCenterPageProps = {
|
|
vm: NotificationCenterVM;
|
|
/** i18n key describing the SSE connection state (sr-only live region). */
|
|
connectionStatusKey: string;
|
|
actions: NotificationCenterActions;
|
|
};
|
|
|
|
type BadgeFarbe = "subtle-positive" | "subtle-warning" | "subtle-negative" | "subtle-neutral";
|
|
|
|
function statusFarbe(status: string): BadgeFarbe {
|
|
switch (status) {
|
|
case "READ":
|
|
return "subtle-positive";
|
|
case "FAILED":
|
|
return "subtle-negative";
|
|
case "PENDING":
|
|
return "subtle-warning";
|
|
default:
|
|
return "subtle-neutral";
|
|
}
|
|
}
|
|
|
|
export function NotificationCenterPage({ vm, connectionStatusKey, actions }: NotificationCenterPageProps) {
|
|
const { t, i18n } = useTranslation();
|
|
const [activeTab, setActiveTab] = useState<"all" | "unread">("all");
|
|
|
|
// Loading / not-ready: keep the marker so the shell + e2e see a stable surface.
|
|
if (vm.status !== "ready" || !vm.data) {
|
|
return <div className="flex w-full flex-col gap-6 p-6" data-ui-id="ui.notifications.center" />;
|
|
}
|
|
|
|
const { items, totalCount, unreadCount, emptyHeadlineKey, emptyBodyKey, unreadEmptyKey } = vm.data;
|
|
const visibleItems = activeTab === "unread" ? items.filter((item) => !item.isRead) : items;
|
|
const numberFormatter = new Intl.NumberFormat(i18n.language);
|
|
|
|
return (
|
|
<div className="flex w-full flex-col gap-6 p-6" data-ui-id="ui.notifications.center">
|
|
{/* Header: title + subtitle (0px rhythm) */}
|
|
<div className="flex flex-col gap-0">
|
|
<span className="text-heading-2 font-heading-2 text-text-neutral-primary">{t("notifications.center.title")}</span>
|
|
<span className="text-body-default-regular-150 font-body-default-regular-150 text-text-neutral-secondary">
|
|
{t("notifications.center.diagnostics")}
|
|
</span>
|
|
<span className="sr-only" role="status">
|
|
{t(connectionStatusKey)}
|
|
</span>
|
|
</div>
|
|
|
|
{totalCount === 0 ? (
|
|
<div data-ui-id="ui.notifications.center.empty">
|
|
<EmptyState title={t(emptyHeadlineKey)} body={t(emptyBodyKey)} />
|
|
</div>
|
|
) : (
|
|
<>
|
|
<div className="flex flex-wrap items-center justify-between gap-4">
|
|
<div className="max-w-[360px] flex-1" data-ui-id="ui.notifications.center.tabs">
|
|
<Tabs>
|
|
<Tabs.Item active={activeTab === "all"} onClick={() => setActiveTab("all")} role="tab" aria-selected={activeTab === "all"}>
|
|
{`${t("notifications.center.tabs.all")} (${numberFormatter.format(totalCount)})`}
|
|
</Tabs.Item>
|
|
<Tabs.Item
|
|
active={activeTab === "unread"}
|
|
onClick={() => setActiveTab("unread")}
|
|
role="tab"
|
|
aria-selected={activeTab === "unread"}
|
|
>
|
|
{`${t("notifications.center.tabs.unread")} (${numberFormatter.format(unreadCount)})`}
|
|
</Tabs.Item>
|
|
</Tabs>
|
|
</div>
|
|
{unreadCount > 0 ? (
|
|
<Button variant="secondary" onClick={() => actions.onMarkAllRead()} data-ui-id="ui.notifications.center.bulkClear">
|
|
{t("notifications.center.bulkClear")}
|
|
</Button>
|
|
) : null}
|
|
</div>
|
|
|
|
{visibleItems.length === 0 ? (
|
|
<div data-ui-id="ui.notifications.center.unreadEmpty">
|
|
<EmptyState title={t(unreadEmptyKey)} body={t(emptyBodyKey)} />
|
|
</div>
|
|
) : (
|
|
<ul className="flex list-none flex-col gap-4 p-0" aria-label={String(t("notifications.center.listLabel"))}>
|
|
{visibleItems.map((item) => (
|
|
<NotificationCard key={item.id} item={item} onMarkRead={actions.onMarkRead} />
|
|
))}
|
|
</ul>
|
|
)}
|
|
</>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function NotificationCard({ item, onMarkRead }: { item: NotificationCenterItemVM; onMarkRead: (deliveryId: string) => void }) {
|
|
const { t, i18n } = useTranslation();
|
|
const dateTimeFormatter = new Intl.DateTimeFormat(i18n.language, { dateStyle: "medium", timeStyle: "short" });
|
|
|
|
const messageLabel = i18n.exists(item.messageKey) ? String(t(item.messageKey)) : String(t("notifications.center.unknownMessage"));
|
|
const enumLabel = (key: string, fallbackKey: string) => (i18n.exists(key) ? String(t(key)) : String(t(fallbackKey)));
|
|
const formatTimestamp = (iso: string | null) =>
|
|
iso ? dateTimeFormatter.format(new Date(iso)) : String(t("notifications.center.timestamps.notRecorded"));
|
|
|
|
return (
|
|
<li
|
|
className="flex flex-col gap-4 rounded-md border border-solid border-border-default bg-surface-neutral-primary p-4"
|
|
data-row-id={item.id}
|
|
data-read={item.isRead}
|
|
>
|
|
<div className="flex flex-wrap items-start justify-between gap-4">
|
|
<div className="flex flex-col gap-0">
|
|
{item.deeplinkHref ? (
|
|
<a
|
|
href={item.deeplinkHref}
|
|
className="text-body-default-medium-0 font-body-default-medium-0 text-text-feedback-link-default no-underline"
|
|
data-ui-id="ui.notifications.center.deeplink"
|
|
>
|
|
{messageLabel}
|
|
</a>
|
|
) : (
|
|
<span className="text-body-default-medium-0 font-body-default-medium-0 text-text-neutral-primary">{messageLabel}</span>
|
|
)}
|
|
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-secondary">
|
|
{enumLabel(item.categoryKey, "notifications.center.categories.unknown")} ·{" "}
|
|
{enumLabel(item.channelKey, "notifications.center.channels.unknown")} · {formatTimestamp(item.sentAtIso ?? item.createdAtIso)}
|
|
</span>
|
|
</div>
|
|
<BadgeFeedback farbe={statusFarbe(item.status)} size="small">
|
|
{enumLabel(item.statusKey, "notifications.center.statuses.unknown")}
|
|
</BadgeFeedback>
|
|
</div>
|
|
|
|
{item.bounceKey ? (
|
|
<div data-ui-id="ui.notifications.center.bounce">
|
|
<Alert variant="error" title={t(item.bounceKey)} description={item.lastErrorRedacted ?? undefined} />
|
|
</div>
|
|
) : null}
|
|
|
|
{!item.isRead ? (
|
|
<div className="flex">
|
|
<Button
|
|
size="small"
|
|
variant="secondary"
|
|
onClick={() => onMarkRead(item.id)}
|
|
aria-label={String(t("notifications.center.markReadLabel", { message: messageLabel }))}
|
|
data-ui-id="ui.notifications.center.markRead"
|
|
>
|
|
{t("notifications.center.markRead")}
|
|
</Button>
|
|
</div>
|
|
) : null}
|
|
</li>
|
|
);
|
|
}
|