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
+36
-19
@@ -5,7 +5,7 @@
|
||||
// <NotificationCenterPage /> screen (AD-28).
|
||||
|
||||
import { useMemo } from "react";
|
||||
import { ActionFunctionArgs, LoaderFunctionArgs, useFetcher, useLoaderData } from "react-router";
|
||||
import { ActionFunctionArgs, LoaderFunctionArgs, useFetcher, useLoaderData, useNavigate } from "react-router";
|
||||
import {
|
||||
getNotificationDeliveryDiagnostics,
|
||||
markAllInAppNotificationDeliveriesRead,
|
||||
@@ -15,6 +15,7 @@ import {
|
||||
} from "@hivegrid/db/notifications.server";
|
||||
import {
|
||||
buildNotificationCenterVM,
|
||||
deriveAutomationFallbackDeeplink,
|
||||
mergeNotificationCenterItems,
|
||||
type NotificationCenterItemInput,
|
||||
type NotificationCenterVM,
|
||||
@@ -27,6 +28,7 @@ import { requireAuth } from "~/utils/loaders.middleware";
|
||||
import { getTenantIdFromUrl } from "~/utils/services/.server/urlService";
|
||||
import { getUserInfo, validateCSRFToken } from "~/utils/session.server";
|
||||
import UrlUtils from "~/utils/app/UrlUtils";
|
||||
import { useSaveToast, type SaveActionResult } from "~/hooks/use-save-toast";
|
||||
|
||||
type NotificationDeliveryDiagnosticRecord = {
|
||||
id: string;
|
||||
@@ -44,6 +46,8 @@ type NotificationDeliveryDiagnosticRecord = {
|
||||
};
|
||||
|
||||
type LoaderData = {
|
||||
/** Page title consumed by the shell breadcrumb (useTitleData). */
|
||||
title: string;
|
||||
csrf?: string;
|
||||
vm: NotificationCenterVM;
|
||||
streamUrl: string;
|
||||
@@ -63,9 +67,13 @@ function serializeTimestamp(value: Date | string | null | undefined): string | n
|
||||
return value instanceof Date ? value.toISOString() : value;
|
||||
}
|
||||
|
||||
function toItemInput(record: NotificationDeliveryDiagnosticRecord): NotificationCenterItemInput {
|
||||
function toItemInput(record: NotificationDeliveryDiagnosticRecord, tenantSlug: string | null): NotificationCenterItemInput {
|
||||
return {
|
||||
id: record.id,
|
||||
// Consulted by the VM only when the record carries no stored deeplink,
|
||||
// which is the case for automation notifications written before the
|
||||
// producers started setting one.
|
||||
deeplink: deriveAutomationFallbackDeeplink(record.payloadMetadata, tenantSlug),
|
||||
messageKey: record.messageKey,
|
||||
category: record.category,
|
||||
channel: record.channel,
|
||||
@@ -93,6 +101,7 @@ function getRequiredFormValue(formData: FormData, key: string): string | null {
|
||||
|
||||
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
||||
await requireAuth({ request, params });
|
||||
const { t } = await getTranslations(request);
|
||||
const tenantId = await getTenantIdFromUrl(params);
|
||||
const userInfo = await getUserInfo(request);
|
||||
const records = (await getNotificationDeliveryDiagnostics({
|
||||
@@ -104,9 +113,10 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
||||
})) as NotificationDeliveryDiagnosticRecord[];
|
||||
|
||||
const data: LoaderData = {
|
||||
title: t("notifications.center.title"),
|
||||
csrf: userInfo.csrf,
|
||||
vm: buildNotificationCenterVM({
|
||||
items: records.filter(isUserVisibleInAppDelivery).map(toItemInput),
|
||||
items: records.filter(isUserVisibleInAppDelivery).map((record) => toItemInput(record, params.tenant ?? null)),
|
||||
streamAvailable: true,
|
||||
}),
|
||||
streamUrl: UrlUtils.getModulePath(params, "notifications/stream"),
|
||||
@@ -117,7 +127,6 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
||||
|
||||
export const action = async ({ request, params }: ActionFunctionArgs) => {
|
||||
await requireAuth({ request, params });
|
||||
const { t } = await getTranslations(request);
|
||||
const tenantId = await getTenantIdFromUrl(params);
|
||||
const userInfo = await getUserInfo(request);
|
||||
|
||||
@@ -132,17 +141,17 @@ export const action = async ({ request, params }: ActionFunctionArgs) => {
|
||||
readAt: new Date(),
|
||||
});
|
||||
|
||||
return Response.json({ ok: true, cleared });
|
||||
return Response.json({ intent: actionName, ok: true, cleared });
|
||||
}
|
||||
|
||||
if (actionName !== "mark-read") {
|
||||
return Response.json({ error: t("notifications.errors.invalidAction") }, { status: 400 });
|
||||
return Response.json({ intent: actionName, error: "notifications.errors.invalidAction" }, { status: 400 });
|
||||
}
|
||||
|
||||
const deliveryId = getRequiredFormValue(formData, "deliveryId");
|
||||
|
||||
if (!deliveryId) {
|
||||
return Response.json({ error: t("notifications.errors.deliveryRequired") }, { status: 400 });
|
||||
return Response.json({ intent: actionName, error: "notifications.errors.deliveryRequired" }, { status: 400 });
|
||||
}
|
||||
|
||||
await markInAppNotificationDeliveryRead({
|
||||
@@ -152,16 +161,19 @@ export const action = async ({ request, params }: ActionFunctionArgs) => {
|
||||
readAt: new Date(),
|
||||
});
|
||||
|
||||
return Response.json({ ok: true });
|
||||
return Response.json({ intent: actionName, ok: true });
|
||||
};
|
||||
|
||||
export default function NotificationsRoute() {
|
||||
const data = useLoaderData<LoaderData>();
|
||||
const fetcher = useFetcher();
|
||||
const navigate = useNavigate();
|
||||
|
||||
const initialItems = data.vm.status === "ready" && data.vm.data ? data.vm.data.items : [];
|
||||
const stream = useNotificationsStream(data.streamUrl, initialItems);
|
||||
|
||||
useSaveToast(fetcher.data as SaveActionResult, { reportSuccess: false });
|
||||
|
||||
// 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;
|
||||
@@ -182,17 +194,22 @@ export default function NotificationsRoute() {
|
||||
}
|
||||
|
||||
return (
|
||||
<main className="relative z-0 flex-1 pb-8">
|
||||
<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>
|
||||
// Full width so the screen's page header bar sits flush; the screen centers its own body.
|
||||
<main className="relative z-0 w-full flex-1 pb-8">
|
||||
<NotificationCenterPage
|
||||
vm={liveVm}
|
||||
connectionStatusKey={stream.connectionStatusKey}
|
||||
actions={{
|
||||
onMarkRead: (deliveryId) => submit({ action: "mark-read", deliveryId }),
|
||||
onMarkAllRead: () => submit({ action: "mark-all-read" }),
|
||||
onOpen: (item) => {
|
||||
// Fire-and-forget mark-read (fetchers persist across the client-side
|
||||
// navigation), then follow the deeplink.
|
||||
if (!item.isRead) submit({ action: "mark-read", deliveryId: item.id });
|
||||
if (item.deeplinkHref) navigate(item.deeplinkHref);
|
||||
},
|
||||
}}
|
||||
/>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user