Update hivegrid-cloud reference to latest main
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e98d38e716
commit
7b21524c86
3781 files changed
+373484
-225607
No files matched your search
+71
-26
@@ -1,16 +1,27 @@
|
||||
import { ActionFunctionArgs, LoaderFunctionArgs, useLoaderData } from "react-router";
|
||||
// AK-P3 — Notification-Center route (`/app/:tenant/notifications`).
|
||||
// Loader builds the NotificationCenterVM once (deeplink from payloadMetadata,
|
||||
// M002 bounce keys); the route hosts the SSE subscription (useNotificationsStream)
|
||||
// and injects the live-merged VM + action callbacks into the interim
|
||||
// <NotificationCenterPage /> screen (AD-28).
|
||||
|
||||
import { useMemo } from "react";
|
||||
import { ActionFunctionArgs, LoaderFunctionArgs, useFetcher, useLoaderData } from "react-router";
|
||||
import {
|
||||
getNotificationDeliveryDiagnostics,
|
||||
markAllInAppNotificationDeliveriesRead,
|
||||
markInAppNotificationDeliveryRead,
|
||||
redactNotificationDiagnosticText,
|
||||
type NotificationDeliveryStatus,
|
||||
} from "@toolctrl/db/notifications.server";
|
||||
} from "@hivegrid/db/notifications.server";
|
||||
import {
|
||||
NotificationCenter,
|
||||
type NotificationCenterItem,
|
||||
} from "~/components/notifications/NotificationCenter";
|
||||
buildNotificationCenterVM,
|
||||
mergeNotificationCenterItems,
|
||||
type NotificationCenterItemInput,
|
||||
type NotificationCenterVM,
|
||||
} from "@hivegrid/view-models";
|
||||
import { NotificationCenterPage } from "~/components/screens/notifications/NotificationCenterPage";
|
||||
import { useNotificationsStream } from "~/components/notifications/useNotificationsStream";
|
||||
import ServerError from "~/components/ui/errors/ServerError";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import { requireAuth } from "~/utils/loaders.middleware";
|
||||
import { getTenantIdFromUrl } from "~/utils/services/.server/urlService";
|
||||
@@ -25,6 +36,7 @@ type NotificationDeliveryDiagnosticRecord = {
|
||||
status: NotificationDeliveryStatus | string;
|
||||
attemptsMade: number;
|
||||
lastError?: string | null;
|
||||
payloadMetadata?: unknown;
|
||||
createdAt?: Date | string | null;
|
||||
updatedAt?: Date | string | null;
|
||||
sentAt?: Date | string | null;
|
||||
@@ -33,7 +45,7 @@ type NotificationDeliveryDiagnosticRecord = {
|
||||
|
||||
type LoaderData = {
|
||||
csrf?: string;
|
||||
items: NotificationCenterItem[];
|
||||
vm: NotificationCenterVM;
|
||||
streamUrl: string;
|
||||
};
|
||||
|
||||
@@ -51,19 +63,20 @@ function serializeTimestamp(value: Date | string | null | undefined): string | n
|
||||
return value instanceof Date ? value.toISOString() : value;
|
||||
}
|
||||
|
||||
function toNotificationCenterItem(record: NotificationDeliveryDiagnosticRecord): NotificationCenterItem {
|
||||
function toItemInput(record: NotificationDeliveryDiagnosticRecord): NotificationCenterItemInput {
|
||||
return {
|
||||
id: record.id,
|
||||
messageKey: record.messageKey,
|
||||
category: record.category,
|
||||
channel: record.channel,
|
||||
status: record.status,
|
||||
status: String(record.status),
|
||||
attemptsMade: record.attemptsMade,
|
||||
lastError: record.lastError ? redactNotificationDiagnosticText(record.lastError) : null,
|
||||
createdAt: serializeTimestamp(record.createdAt),
|
||||
updatedAt: serializeTimestamp(record.updatedAt),
|
||||
sentAt: serializeTimestamp(record.sentAt),
|
||||
readAt: serializeTimestamp(record.readAt),
|
||||
payloadMetadata: record.payloadMetadata,
|
||||
createdAtIso: serializeTimestamp(record.createdAt),
|
||||
updatedAtIso: serializeTimestamp(record.updatedAt),
|
||||
sentAtIso: serializeTimestamp(record.sentAt),
|
||||
readAtIso: serializeTimestamp(record.readAt),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -92,7 +105,10 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
||||
|
||||
const data: LoaderData = {
|
||||
csrf: userInfo.csrf,
|
||||
items: records.filter(isUserVisibleInAppDelivery).map(toNotificationCenterItem),
|
||||
vm: buildNotificationCenterVM({
|
||||
items: records.filter(isUserVisibleInAppDelivery).map(toItemInput),
|
||||
streamAvailable: true,
|
||||
}),
|
||||
streamUrl: UrlUtils.getModulePath(params, "notifications/stream"),
|
||||
};
|
||||
|
||||
@@ -109,6 +125,16 @@ export const action = async ({ request, params }: ActionFunctionArgs) => {
|
||||
const formData = await request.formData();
|
||||
const actionName = getRequiredFormValue(formData, "action");
|
||||
|
||||
if (actionName === "mark-all-read") {
|
||||
const cleared = await markAllInAppNotificationDeliveriesRead({
|
||||
tenantId,
|
||||
userId: userInfo.userId,
|
||||
readAt: new Date(),
|
||||
});
|
||||
|
||||
return Response.json({ ok: true, cleared });
|
||||
}
|
||||
|
||||
if (actionName !== "mark-read") {
|
||||
return Response.json({ error: t("notifications.errors.invalidAction") }, { status: 400 });
|
||||
}
|
||||
@@ -131,21 +157,40 @@ export const action = async ({ request, params }: ActionFunctionArgs) => {
|
||||
|
||||
export default function NotificationsRoute() {
|
||||
const data = useLoaderData<LoaderData>();
|
||||
const { t } = useTranslation();
|
||||
const fetcher = useFetcher();
|
||||
|
||||
const initialItems = data.vm.status === "ready" && data.vm.data ? data.vm.data.items : [];
|
||||
const stream = useNotificationsStream(data.streamUrl, initialItems);
|
||||
|
||||
// Live-merge: the screen renders one VM; SSE updates flow in through the hook.
|
||||
const liveVm = useMemo<NotificationCenterVM>(() => {
|
||||
if (data.vm.status !== "ready" || !data.vm.data) return data.vm;
|
||||
const items = mergeNotificationCenterItems(data.vm.data.items, stream.items);
|
||||
return {
|
||||
status: "ready",
|
||||
data: {
|
||||
...data.vm.data,
|
||||
items,
|
||||
totalCount: items.length,
|
||||
unreadCount: items.filter((item) => !item.isRead).length,
|
||||
},
|
||||
};
|
||||
}, [data.vm, stream.items]);
|
||||
|
||||
function submit(form: Record<string, string>) {
|
||||
fetcher.submit({ ...form, ...(data.csrf ? { csrf: data.csrf } : {}) }, { method: "post" });
|
||||
}
|
||||
|
||||
return (
|
||||
<main className="relative z-0 flex-1 pb-8">
|
||||
<div className="mx-auto grid max-w-5xl gap-5 px-4 py-5 sm:px-8">
|
||||
<div className="space-y-2">
|
||||
<h1 className="text-2xl font-semibold text-foreground">{t("notifications.title")}</h1>
|
||||
<p className="text-muted-foreground text-sm">{t("notifications.description")}</p>
|
||||
</div>
|
||||
<NotificationCenter
|
||||
csrf={data.csrf}
|
||||
items={data.items}
|
||||
streamUrl={data.streamUrl}
|
||||
title={t("notifications.center.title")}
|
||||
emptyMessage={t("notifications.center.empty")}
|
||||
<div className="mx-auto max-w-5xl">
|
||||
<NotificationCenterPage
|
||||
vm={liveVm}
|
||||
connectionStatusKey={stream.connectionStatusKey}
|
||||
actions={{
|
||||
onMarkRead: (deliveryId) => submit({ action: "mark-read", deliveryId }),
|
||||
onMarkAllRead: () => submit({ action: "mark-all-read" }),
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
Reference in new issue
Block a user