Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,330 @@
|
||||
import "@testing-library/jest-dom";
|
||||
import { act, render, screen, within } from "@testing-library/react";
|
||||
import React from "react";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { NotificationCenter, type NotificationCenterItem } from "./NotificationCenter";
|
||||
|
||||
vi.mock("react-i18next", () => ({
|
||||
useTranslation: () => ({
|
||||
t: (key: string, options?: Record<string, string>) => {
|
||||
const translations: Record<string, string> = {
|
||||
"billing.invoice.ready": "Invoice ready",
|
||||
"notification.delivery": "Notification delivery",
|
||||
"notifications.center.categories.billing": "Billing",
|
||||
"notifications.center.categories.unknown": "Notification",
|
||||
"notifications.center.channels.in_app": "In-app",
|
||||
"notifications.center.channels.unknown": "Notification",
|
||||
"notifications.center.title": "Notification center",
|
||||
"notifications.center.empty": "No in-app notifications yet.",
|
||||
"notifications.center.diagnostics": "Delivery diagnostics for in-app notifications.",
|
||||
"notifications.center.emptyHelper": "New in-app deliveries will appear here newest-first.",
|
||||
"notifications.center.fields.attempts": "Attempts",
|
||||
"notifications.center.fields.read": "Read",
|
||||
"notifications.center.fields.sent": "Sent",
|
||||
"notifications.center.fields.updated": "Updated",
|
||||
"notifications.center.lastError": "Last error:",
|
||||
"notifications.center.listLabel": "In-app notification deliveries",
|
||||
"notifications.center.markRead": "Mark as read",
|
||||
"notifications.center.markReadLabel": `Mark ${options?.message ?? ""} as read`,
|
||||
"notifications.center.realtime.connected": "Realtime updates connected.",
|
||||
"notifications.center.realtime.connecting": "Connecting to realtime updates.",
|
||||
"notifications.center.realtime.reconnecting": "Realtime updates reconnecting.",
|
||||
"notifications.center.realtime.unavailable": "Realtime updates unavailable.",
|
||||
"notifications.center.statuses.failed": "Failed",
|
||||
"notifications.center.statuses.pending": "Pending",
|
||||
"notifications.center.statuses.read": "Read",
|
||||
"notifications.center.statuses.sent": "Unread",
|
||||
"notifications.center.statuses.skipped": "Skipped",
|
||||
"notifications.center.statuses.unknown": "Unknown",
|
||||
"notifications.center.timestamps.invalid": "Invalid timestamp",
|
||||
"notifications.center.timestamps.notRecorded": "Not recorded",
|
||||
"notifications.center.unknownMessage": "Notification delivery",
|
||||
"team.invite.created": "Team invite created",
|
||||
};
|
||||
return translations[key] ?? key;
|
||||
},
|
||||
i18n: {
|
||||
exists: (key: string) =>
|
||||
[
|
||||
"billing.invoice.ready",
|
||||
"notification.delivery",
|
||||
"notifications.center.categories.billing",
|
||||
"notifications.center.channels.in_app",
|
||||
"notifications.center.statuses.failed",
|
||||
"notifications.center.statuses.pending",
|
||||
"notifications.center.statuses.read",
|
||||
"notifications.center.statuses.sent",
|
||||
"notifications.center.statuses.skipped",
|
||||
"team.invite.created",
|
||||
].includes(key),
|
||||
},
|
||||
}),
|
||||
}));
|
||||
|
||||
type EventHandler = (event: MessageEvent) => void;
|
||||
|
||||
class MockEventSource {
|
||||
static instances: MockEventSource[] = [];
|
||||
listeners = new Map<string, EventHandler[]>();
|
||||
closed = false;
|
||||
|
||||
constructor(public url: string) {
|
||||
MockEventSource.instances.push(this);
|
||||
}
|
||||
|
||||
addEventListener(event: string, handler: EventHandler) {
|
||||
this.listeners.set(event, [...(this.listeners.get(event) ?? []), handler]);
|
||||
}
|
||||
|
||||
removeEventListener(event: string, handler: EventHandler) {
|
||||
this.listeners.set(
|
||||
event,
|
||||
(this.listeners.get(event) ?? []).filter((listener) => listener !== handler)
|
||||
);
|
||||
}
|
||||
|
||||
close() {
|
||||
this.closed = true;
|
||||
}
|
||||
|
||||
emit(event: string, data = "{}", lastEventId = "") {
|
||||
const message = new MessageEvent(event, { data, lastEventId });
|
||||
for (const listener of this.listeners.get(event) ?? []) {
|
||||
listener(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const originalEventSource = window.EventSource;
|
||||
|
||||
const baseItem: NotificationCenterItem = {
|
||||
id: "delivery-1",
|
||||
messageKey: "billing.invoice.ready",
|
||||
category: "billing",
|
||||
channel: "IN_APP",
|
||||
status: "SENT",
|
||||
attemptsMade: 1,
|
||||
sentAt: "2026-05-01T12:00:00.000Z",
|
||||
readAt: null,
|
||||
updatedAt: "2026-05-01T12:01:00.000Z",
|
||||
};
|
||||
|
||||
describe("NotificationCenter", () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
MockEventSource.instances = [];
|
||||
window.EventSource = MockEventSource as unknown as typeof EventSource;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
window.EventSource = originalEventSource;
|
||||
});
|
||||
|
||||
it("renders an accessible empty state", () => {
|
||||
render(<NotificationCenter items={[]} />);
|
||||
|
||||
expect(screen.getByRole("heading", { name: "Notification center" })).toBeInTheDocument();
|
||||
expect(screen.getByText("No in-app notifications yet.").closest('[role="status"]')).toBeInTheDocument();
|
||||
expect(screen.getByText("Delivery diagnostics for in-app notifications.")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("renders unread, read, pending, skipped, and failed diagnostics without hiding missing optional dates", () => {
|
||||
render(
|
||||
<NotificationCenter
|
||||
items={[
|
||||
baseItem,
|
||||
{ ...baseItem, id: "delivery-2", messageKey: "comment.mention", status: "READ", readAt: "2026-05-01T12:05:00.000Z" },
|
||||
{ ...baseItem, id: "delivery-3", messageKey: "report.pending", status: "PENDING", sentAt: null, updatedAt: "not-a-date" },
|
||||
{ ...baseItem, id: "delivery-4", messageKey: "alert.failed", status: "FAILED", attemptsMade: 3, lastError: "provider timeout" },
|
||||
{ ...baseItem, id: "delivery-5", messageKey: "billing.skipped", status: "SKIPPED", attemptsMade: 0, lastError: "preference_disabled" },
|
||||
]}
|
||||
/>
|
||||
);
|
||||
|
||||
expect(screen.getByText("Invoice ready")).toBeInTheDocument();
|
||||
expect(screen.getAllByText("Notification delivery")).toHaveLength(4);
|
||||
expect(screen.queryByText("comment.mention")).not.toBeInTheDocument();
|
||||
expect(screen.queryByText("report.pending")).not.toBeInTheDocument();
|
||||
expect(screen.queryByText("alert.failed")).not.toBeInTheDocument();
|
||||
expect(screen.queryByText("billing.skipped")).not.toBeInTheDocument();
|
||||
expect(screen.getAllByText("Unread")).toHaveLength(1);
|
||||
expect(screen.getByText("Read", { selector: "span" })).toBeInTheDocument();
|
||||
expect(screen.getByText("Pending")).toBeInTheDocument();
|
||||
expect(screen.getByText("Failed")).toBeInTheDocument();
|
||||
expect(screen.getByText("Skipped")).toBeInTheDocument();
|
||||
expect(screen.getByText("Invalid timestamp")).toBeInTheDocument();
|
||||
expect(screen.getAllByText("Not recorded").length).toBeGreaterThan(0);
|
||||
expect(screen.queryByText("2026-05-01T12:01:00.000Z")).not.toBeInTheDocument();
|
||||
expect(screen.getByText("provider timeout", { exact: false })).toBeInTheDocument();
|
||||
expect(screen.getByText("3")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("renders mark-read form fields only for non-read deliveries", () => {
|
||||
render(<NotificationCenter items={[baseItem, { ...baseItem, id: "delivery-2", messageKey: "already.read", status: "READ" }]} />);
|
||||
|
||||
const button = screen.getByRole("button", { name: "Mark Invoice ready as read" });
|
||||
const form = button.closest("form");
|
||||
|
||||
expect(form).toHaveAttribute("method", "post");
|
||||
expect(within(form as HTMLFormElement).getByDisplayValue("mark-read")).toHaveAttribute("name", "action");
|
||||
expect(within(form as HTMLFormElement).getByDisplayValue("delivery-1")).toHaveAttribute("name", "deliveryId");
|
||||
expect(screen.queryByRole("button", { name: "Mark Notification delivery as read" })).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("subscribes to the realtime stream and merges snapshot plus notification updates newest-first", () => {
|
||||
render(<NotificationCenter items={[baseItem]} streamUrl="https://api.example.com/v1/notifications/stream?tenantId=tenant_1&userId=user_1" />);
|
||||
|
||||
expect(MockEventSource.instances).toHaveLength(1);
|
||||
expect(MockEventSource.instances[0].url).toBe("https://api.example.com/v1/notifications/stream?tenantId=tenant_1&userId=user_1");
|
||||
|
||||
act(() => {
|
||||
MockEventSource.instances[0].emit(
|
||||
"notification.snapshot",
|
||||
JSON.stringify({
|
||||
cursor: "cursor_snapshot",
|
||||
items: [
|
||||
{ ...baseItem, id: "delivery-2", messageKey: "team.invite", updatedAt: "2026-05-01T12:03:00.000Z" },
|
||||
{ ...baseItem, id: "delivery-1", status: "READ", readAt: "2026-05-01T12:04:00.000Z", updatedAt: "2026-05-01T12:04:00.000Z" },
|
||||
],
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
expect(screen.getByText("Notification delivery")).toBeInTheDocument();
|
||||
expect(screen.queryByText("team.invite")).not.toBeInTheDocument();
|
||||
expect(screen.queryByRole("button", { name: "Mark Invoice ready as read" })).not.toBeInTheDocument();
|
||||
|
||||
act(() => {
|
||||
MockEventSource.instances[0].emit(
|
||||
"notification.update",
|
||||
JSON.stringify({ item: { ...baseItem, id: "delivery-3", messageKey: "new.message", updatedAt: "2026-05-01T12:05:00.000Z" } }),
|
||||
"cursor_notification"
|
||||
);
|
||||
});
|
||||
|
||||
expect(within(screen.getAllByRole("listitem")[0]).getByText("Notification delivery")).toBeInTheDocument();
|
||||
expect(screen.queryByText("new.message")).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("reconnects with the latest cursor, tolerates malformed events, and closes on unmount", () => {
|
||||
const { unmount } = render(
|
||||
<NotificationCenter items={[baseItem]} streamUrl="https://api.example.com/v1/notifications/stream?tenantId=tenant_1&userId=user_1" />
|
||||
);
|
||||
|
||||
act(() => {
|
||||
MockEventSource.instances[0].emit("notification.update", JSON.stringify({ cursor: "cursor_1", item: { ...baseItem, id: "delivery-2" } }));
|
||||
MockEventSource.instances[0].emit("notification.update", "not-json");
|
||||
MockEventSource.instances[0].emit("error");
|
||||
});
|
||||
|
||||
expect(MockEventSource.instances[0].closed).toBe(true);
|
||||
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(1_000);
|
||||
});
|
||||
|
||||
expect(MockEventSource.instances).toHaveLength(2);
|
||||
expect(MockEventSource.instances[1].url).toBe("https://api.example.com/v1/notifications/stream?tenantId=tenant_1&userId=user_1&afterCursor=cursor_1");
|
||||
expect(screen.getAllByText("Invoice ready")).toHaveLength(2);
|
||||
|
||||
unmount();
|
||||
|
||||
expect(MockEventSource.instances[1].closed).toBe(true);
|
||||
});
|
||||
|
||||
it("does not reconnect with the initial cursor from an empty snapshot", () => {
|
||||
render(<NotificationCenter items={[]} streamUrl="https://api.example.com/v1/notifications/stream?tenantId=tenant_1&userId=user_1" />);
|
||||
|
||||
act(() => {
|
||||
MockEventSource.instances[0].emit("notification.snapshot", JSON.stringify({ cursor: "initial", items: [] }));
|
||||
MockEventSource.instances[0].emit("error");
|
||||
vi.advanceTimersByTime(1_000);
|
||||
});
|
||||
|
||||
expect(MockEventSource.instances).toHaveLength(2);
|
||||
expect(MockEventSource.instances[1].url).toBe("https://api.example.com/v1/notifications/stream?tenantId=tenant_1&userId=user_1");
|
||||
});
|
||||
|
||||
it("backs off repeated reconnects and resets the delay after a healthy stream event", () => {
|
||||
render(<NotificationCenter items={[]} streamUrl="https://api.example.com/v1/notifications/stream?tenantId=tenant_1&userId=user_1" />);
|
||||
|
||||
act(() => {
|
||||
MockEventSource.instances[0].emit("error");
|
||||
vi.advanceTimersByTime(999);
|
||||
});
|
||||
expect(MockEventSource.instances).toHaveLength(1);
|
||||
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(1);
|
||||
});
|
||||
expect(MockEventSource.instances).toHaveLength(2);
|
||||
|
||||
act(() => {
|
||||
MockEventSource.instances[1].emit("error");
|
||||
vi.advanceTimersByTime(1_999);
|
||||
});
|
||||
expect(MockEventSource.instances).toHaveLength(2);
|
||||
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(1);
|
||||
});
|
||||
expect(MockEventSource.instances).toHaveLength(3);
|
||||
|
||||
act(() => {
|
||||
MockEventSource.instances[2].emit("notification.update", JSON.stringify({ item: { ...baseItem, id: "delivery-2" } }));
|
||||
MockEventSource.instances[2].emit("error");
|
||||
vi.advanceTimersByTime(999);
|
||||
});
|
||||
expect(MockEventSource.instances).toHaveLength(3);
|
||||
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(1);
|
||||
});
|
||||
expect(MockEventSource.instances).toHaveLength(4);
|
||||
});
|
||||
|
||||
it("keeps the merged realtime list bounded to 50 items", () => {
|
||||
render(<NotificationCenter items={[]} streamUrl="https://api.example.com/v1/notifications/stream?tenantId=tenant_1&userId=user_1" />);
|
||||
|
||||
act(() => {
|
||||
MockEventSource.instances[0].emit(
|
||||
"notification.snapshot",
|
||||
JSON.stringify({
|
||||
items: Array.from({ length: 55 }, (_, index) => ({
|
||||
...baseItem,
|
||||
id: `delivery-${index}`,
|
||||
messageKey: `message.${index}`,
|
||||
updatedAt: new Date(Date.UTC(2026, 4, 1, 12, index)).toISOString(),
|
||||
})),
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
expect(screen.getAllByRole("listitem")).toHaveLength(50);
|
||||
expect(screen.getAllByText("Notification delivery")).toHaveLength(50);
|
||||
expect(screen.queryByText("message.54")).not.toBeInTheDocument();
|
||||
expect(screen.queryByText("message.0")).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("redacts secret-like error strings and keeps provider identifiers out of rendered diagnostics", () => {
|
||||
const { container } = render(
|
||||
<NotificationCenter
|
||||
items={[
|
||||
{
|
||||
...baseItem,
|
||||
id: "delivery-5",
|
||||
status: "FAILED",
|
||||
lastError: "delivery failed apiKey=sk_live_secret123 provider_id=provider_delivery_abc token=token_supersecret",
|
||||
},
|
||||
]}
|
||||
/>
|
||||
);
|
||||
|
||||
expect(container).toHaveTextContent("Last error:");
|
||||
expect(container).not.toHaveTextContent("sk_live_secret123");
|
||||
expect(container).not.toHaveTextContent("provider_delivery_abc");
|
||||
expect(container).not.toHaveTextContent("token_supersecret");
|
||||
expect(container).toHaveTextContent("[redacted]");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,312 @@
|
||||
import { useEffect, useRef, useState, type ReactNode } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import type { LocaleFormatters } from "~/locale/format";
|
||||
import { useLocaleFormatters } from "~/locale/format";
|
||||
import { translateLabel } from "~/utils/i18n/translateLabel";
|
||||
|
||||
export type NotificationCenterItem = {
|
||||
id: string;
|
||||
messageKey: string;
|
||||
category: string;
|
||||
channel: "IN_APP" | string;
|
||||
status: "PENDING" | "SENT" | "READ" | "FAILED" | "SKIPPED" | string;
|
||||
attemptsMade: number;
|
||||
createdAt?: string | Date | null;
|
||||
updatedAt?: string | Date | null;
|
||||
sentAt?: string | Date | null;
|
||||
readAt?: string | Date | null;
|
||||
lastError?: string | null;
|
||||
};
|
||||
|
||||
type NotificationCenterProps = {
|
||||
csrf?: string | null;
|
||||
items: NotificationCenterItem[];
|
||||
streamUrl?: string | null;
|
||||
title?: ReactNode;
|
||||
emptyMessage?: ReactNode;
|
||||
};
|
||||
|
||||
type StreamPayload = {
|
||||
cursor?: unknown;
|
||||
id?: unknown;
|
||||
items?: unknown;
|
||||
deliveries?: unknown;
|
||||
item?: unknown;
|
||||
delivery?: unknown;
|
||||
notification?: unknown;
|
||||
};
|
||||
|
||||
const MAX_NOTIFICATION_ITEMS = 50;
|
||||
const RECONNECT_DELAY_MS = 1_000;
|
||||
const MAX_RECONNECT_DELAY_MS = 30_000;
|
||||
|
||||
const statusStyles: Record<string, string> = {
|
||||
FAILED: "border-red-200 bg-red-50 text-red-700",
|
||||
PENDING: "border-amber-200 bg-amber-50 text-amber-700",
|
||||
READ: "border-emerald-200 bg-emerald-50 text-emerald-700",
|
||||
SKIPPED: "border-slate-200 bg-slate-50 text-slate-700",
|
||||
SENT: "border-blue-200 bg-blue-50 text-blue-700",
|
||||
UNREAD: "border-blue-200 bg-blue-50 text-blue-700",
|
||||
};
|
||||
|
||||
function formatTimestamp(value: NotificationCenterItem["updatedAt"], t: ReturnType<typeof useTranslation>["t"], formatters: LocaleFormatters): string {
|
||||
if (!value) return String(t("notifications.center.timestamps.notRecorded"));
|
||||
const date = value instanceof Date ? value : new Date(value);
|
||||
return Number.isNaN(date.getTime()) ? String(t("notifications.center.timestamps.invalid")) : formatters.dateTime(date, { dateStyle: "medium", timeStyle: "short" });
|
||||
}
|
||||
|
||||
function formatEnumLabel(t: ReturnType<typeof useTranslation>["t"], i18n: ReturnType<typeof useTranslation>["i18n"], key: string, fallbackKey: string): string {
|
||||
return i18n.exists(key) ? String(t(key)) : String(t(fallbackKey));
|
||||
}
|
||||
|
||||
function displayStatus(t: ReturnType<typeof useTranslation>["t"], i18n: ReturnType<typeof useTranslation>["i18n"], status: string): string {
|
||||
return formatEnumLabel(t, i18n, `notifications.center.statuses.${status.toLowerCase()}`, "notifications.center.statuses.unknown");
|
||||
}
|
||||
|
||||
function formatMessageLabel(t: ReturnType<typeof useTranslation>["t"], i18n: ReturnType<typeof useTranslation>["i18n"], messageKey: string): string {
|
||||
if (!i18n.exists(messageKey)) {
|
||||
return String(t("notifications.center.unknownMessage"));
|
||||
}
|
||||
const translatedLabel = translateLabel(t, i18n, messageKey);
|
||||
return translatedLabel === messageKey ? String(t("notifications.center.unknownMessage")) : translatedLabel;
|
||||
}
|
||||
|
||||
function formatCategoryLabel(t: ReturnType<typeof useTranslation>["t"], i18n: ReturnType<typeof useTranslation>["i18n"], category: string): string {
|
||||
return formatEnumLabel(t, i18n, `notifications.center.categories.${category.toLowerCase()}`, "notifications.center.categories.unknown");
|
||||
}
|
||||
|
||||
function formatChannelLabel(t: ReturnType<typeof useTranslation>["t"], i18n: ReturnType<typeof useTranslation>["i18n"], channel: string): string {
|
||||
return formatEnumLabel(t, i18n, `notifications.center.channels.${channel.toLowerCase()}`, "notifications.center.channels.unknown");
|
||||
}
|
||||
|
||||
function redactLastError(lastError: string): string {
|
||||
const withoutTokenPairs = lastError.replace(/\b((?:api[_-]?key|secret|token|password|provider[_-]?id))\s*[:=]\s*[^\s,;]+/gi, "$1=[redacted]");
|
||||
const withoutLikelyBearer = withoutTokenPairs.replace(/\b(?:sk|pk|provider|secret|token)_[A-Za-z0-9_-]{6,}\b/g, "[redacted]");
|
||||
return withoutLikelyBearer.length > 160 ? `${withoutLikelyBearer.slice(0, 157)}...` : withoutLikelyBearer;
|
||||
}
|
||||
|
||||
function StatusBadge({ label, status }: { label: string; status: string }) {
|
||||
const style = statusStyles[status] ?? "border-slate-200 bg-slate-50 text-slate-700";
|
||||
return <span className={`inline-flex items-center rounded-full border px-2.5 py-0.5 text-xs font-medium ${style}`}>{label}</span>;
|
||||
}
|
||||
|
||||
function serializeDateField(value: unknown): string | null | undefined {
|
||||
if (value === null || value === undefined) return value as null | undefined;
|
||||
if (value instanceof Date) return value.toISOString();
|
||||
return typeof value === "string" ? value : undefined;
|
||||
}
|
||||
|
||||
function normalizeStreamItem(value: unknown): NotificationCenterItem | null {
|
||||
if (!value || typeof value !== "object") return null;
|
||||
const record = value as Record<string, unknown>;
|
||||
if (typeof record.id !== "string" || record.id.trim().length === 0) return null;
|
||||
return {
|
||||
id: record.id,
|
||||
messageKey: typeof record.messageKey === "string" ? record.messageKey : "notification.delivery",
|
||||
category: typeof record.category === "string" ? record.category : "notification",
|
||||
channel: typeof record.channel === "string" ? record.channel : "IN_APP",
|
||||
status: typeof record.status === "string" ? record.status : "PENDING",
|
||||
attemptsMade: typeof record.attemptsMade === "number" && Number.isFinite(record.attemptsMade) ? record.attemptsMade : 0,
|
||||
createdAt: serializeDateField(record.createdAt),
|
||||
updatedAt: serializeDateField(record.updatedAt),
|
||||
sentAt: serializeDateField(record.sentAt),
|
||||
readAt: serializeDateField(record.readAt),
|
||||
lastError: typeof record.lastError === "string" ? record.lastError : record.lastError === null ? null : undefined,
|
||||
};
|
||||
}
|
||||
|
||||
function getPayloadItems(payload: StreamPayload): NotificationCenterItem[] {
|
||||
const snapshotItems = Array.isArray(payload.items) ? payload.items : Array.isArray(payload.deliveries) ? payload.deliveries : null;
|
||||
if (snapshotItems) return snapshotItems.map(normalizeStreamItem).filter((item): item is NotificationCenterItem => Boolean(item));
|
||||
const singleItem = normalizeStreamItem(payload.item ?? payload.delivery ?? payload.notification ?? payload);
|
||||
return singleItem ? [singleItem] : [];
|
||||
}
|
||||
|
||||
function getEventCursor(event: MessageEvent, payload: StreamPayload): string | null {
|
||||
if (typeof payload.cursor === "string" && payload.cursor.trim().length > 0) return payload.cursor;
|
||||
if (typeof payload.id === "string" && payload.id.trim().length > 0) return payload.id;
|
||||
return event.lastEventId?.trim() ? event.lastEventId : null;
|
||||
}
|
||||
|
||||
function parseStreamEvent(event: MessageEvent): { cursor: string | null; items: NotificationCenterItem[] } | null {
|
||||
try {
|
||||
const payload = JSON.parse(event.data || "{}");
|
||||
if (!payload || typeof payload !== "object") return null;
|
||||
return { cursor: getEventCursor(event, payload as StreamPayload), items: getPayloadItems(payload as StreamPayload) };
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function itemTimestamp(item: NotificationCenterItem): number {
|
||||
const timestamp = item.updatedAt ?? item.sentAt ?? item.createdAt;
|
||||
const date = timestamp instanceof Date ? timestamp : timestamp ? new Date(timestamp) : null;
|
||||
return date && !Number.isNaN(date.getTime()) ? date.getTime() : 0;
|
||||
}
|
||||
|
||||
function mergeNotificationItems(currentItems: NotificationCenterItem[], incomingItems: NotificationCenterItem[]) {
|
||||
if (incomingItems.length === 0) return currentItems;
|
||||
const byId = new Map(currentItems.map((item) => [item.id, item]));
|
||||
for (const item of incomingItems) byId.set(item.id, { ...byId.get(item.id), ...item });
|
||||
return Array.from(byId.values())
|
||||
.sort((left, right) => itemTimestamp(right) - itemTimestamp(left))
|
||||
.slice(0, MAX_NOTIFICATION_ITEMS);
|
||||
}
|
||||
|
||||
function buildReconnectUrl(streamUrl: string, cursor: string | null) {
|
||||
if (!cursor) return streamUrl;
|
||||
const url = new URL(streamUrl, window.location.origin);
|
||||
url.searchParams.set("afterCursor", cursor);
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
export function NotificationCenter({ csrf = null, items, streamUrl = null, title, emptyMessage }: NotificationCenterProps) {
|
||||
const { t, i18n } = useTranslation();
|
||||
const formatters = useLocaleFormatters();
|
||||
const resolvedTitle = title ?? t("notifications.center.title");
|
||||
const resolvedEmptyMessage = emptyMessage ?? t("notifications.center.empty");
|
||||
const [deliveryItems, setDeliveryItems] = useState(() => items.slice(0, MAX_NOTIFICATION_ITEMS));
|
||||
const [connectionStatusKey, setConnectionStatusKey] = useState(
|
||||
streamUrl ? "notifications.center.realtime.connecting" : "notifications.center.realtime.unavailable"
|
||||
);
|
||||
const cursorRef = useRef<string | null>(null);
|
||||
const reconnectTimerRef = useRef<number | null>(null);
|
||||
const reconnectDelayRef = useRef(RECONNECT_DELAY_MS);
|
||||
|
||||
useEffect(() => {
|
||||
setDeliveryItems((currentItems) => mergeNotificationItems(items.slice(0, MAX_NOTIFICATION_ITEMS), currentItems));
|
||||
}, [items]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!streamUrl || typeof window === "undefined" || typeof window.EventSource === "undefined") return;
|
||||
const activeStreamUrl = streamUrl;
|
||||
let closed = false;
|
||||
let source: EventSource | null = null;
|
||||
function clearReconnectTimer() {
|
||||
if (reconnectTimerRef.current) {
|
||||
window.clearTimeout(reconnectTimerRef.current);
|
||||
reconnectTimerRef.current = null;
|
||||
}
|
||||
}
|
||||
function connect() {
|
||||
clearReconnectTimer();
|
||||
source = new EventSource(buildReconnectUrl(activeStreamUrl, cursorRef.current));
|
||||
setConnectionStatusKey("notifications.center.realtime.connecting");
|
||||
function handleMessage(event: MessageEvent) {
|
||||
const parsed = parseStreamEvent(event);
|
||||
if (!parsed) return;
|
||||
if (parsed.cursor && parsed.items.length > 0) cursorRef.current = parsed.cursor;
|
||||
setDeliveryItems((currentItems) => mergeNotificationItems(currentItems, parsed.items));
|
||||
reconnectDelayRef.current = RECONNECT_DELAY_MS;
|
||||
setConnectionStatusKey("notifications.center.realtime.connected");
|
||||
}
|
||||
function handleOpen() {
|
||||
reconnectDelayRef.current = RECONNECT_DELAY_MS;
|
||||
setConnectionStatusKey("notifications.center.realtime.connected");
|
||||
}
|
||||
function handleError() {
|
||||
if (closed) return;
|
||||
source?.close();
|
||||
setConnectionStatusKey("notifications.center.realtime.reconnecting");
|
||||
const reconnectDelay = reconnectDelayRef.current;
|
||||
reconnectDelayRef.current = Math.min(reconnectDelay * 2, MAX_RECONNECT_DELAY_MS);
|
||||
reconnectTimerRef.current = window.setTimeout(connect, reconnectDelay);
|
||||
}
|
||||
source.addEventListener("open", handleOpen);
|
||||
source.addEventListener("notification.snapshot", handleMessage);
|
||||
source.addEventListener("notification.update", handleMessage);
|
||||
source.addEventListener("error", handleError);
|
||||
}
|
||||
connect();
|
||||
return () => {
|
||||
closed = true;
|
||||
clearReconnectTimer();
|
||||
source?.close();
|
||||
};
|
||||
}, [streamUrl]);
|
||||
|
||||
return (
|
||||
<section aria-labelledby="notification-center-title" className="space-y-4">
|
||||
<div>
|
||||
<h1 id="notification-center-title" className="text-foreground text-2xl font-semibold">
|
||||
{resolvedTitle}
|
||||
</h1>
|
||||
<p className="text-muted-foreground mt-1 text-sm">{t("notifications.center.diagnostics")}</p>
|
||||
<p className="sr-only" role="status">
|
||||
{t(connectionStatusKey)}
|
||||
</p>
|
||||
</div>
|
||||
{deliveryItems.length === 0 ? (
|
||||
<div className="border-border bg-muted/30 rounded-lg border border-dashed p-8 text-center" role="status">
|
||||
<p className="text-foreground text-sm font-medium">{resolvedEmptyMessage}</p>
|
||||
<p className="text-muted-foreground mt-1 text-sm">{t("notifications.center.emptyHelper")}</p>
|
||||
</div>
|
||||
) : (
|
||||
<ul aria-label={String(t("notifications.center.listLabel"))} className="space-y-3">
|
||||
{deliveryItems.map((item) => {
|
||||
const isRead = item.status === "READ";
|
||||
const lastError = item.lastError ? redactLastError(item.lastError) : null;
|
||||
const messageLabel = formatMessageLabel(t, i18n, item.messageKey);
|
||||
const statusLabel = displayStatus(t, i18n, item.status);
|
||||
const categoryLabel = formatCategoryLabel(t, i18n, item.category);
|
||||
const channelLabel = formatChannelLabel(t, i18n, item.channel);
|
||||
return (
|
||||
<li key={item.id} className="border-border bg-background rounded-lg border p-4 shadow-xs">
|
||||
<article aria-labelledby={`notification-${item.id}-title`} className="space-y-3">
|
||||
<div className="flex flex-col gap-2 sm:flex-row sm:items-start sm:justify-between">
|
||||
<div>
|
||||
<h2 id={`notification-${item.id}-title`} className="text-foreground text-base font-semibold">
|
||||
{messageLabel}
|
||||
</h2>
|
||||
<p className="text-muted-foreground mt-1 text-sm">
|
||||
{categoryLabel} · {channelLabel}
|
||||
</p>
|
||||
</div>
|
||||
<StatusBadge label={statusLabel} status={item.status} />
|
||||
</div>
|
||||
<dl className="text-muted-foreground grid gap-2 text-sm sm:grid-cols-2 lg:grid-cols-4">
|
||||
<div>
|
||||
<dt className="text-foreground font-medium">{t("notifications.center.fields.attempts")}</dt>
|
||||
<dd>{formatters.number(item.attemptsMade)}</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt className="text-foreground font-medium">{t("notifications.center.fields.sent")}</dt>
|
||||
<dd>{formatTimestamp(item.sentAt, t, formatters)}</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt className="text-foreground font-medium">{t("notifications.center.fields.read")}</dt>
|
||||
<dd>{formatTimestamp(item.readAt, t, formatters)}</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt className="text-foreground font-medium">{t("notifications.center.fields.updated")}</dt>
|
||||
<dd>{formatTimestamp(item.updatedAt, t, formatters)}</dd>
|
||||
</div>
|
||||
</dl>
|
||||
{lastError ? (
|
||||
<p className="rounded-md border border-red-200 bg-red-50 p-3 text-sm text-red-700">
|
||||
<span className="font-medium">{t("notifications.center.lastError")}</span> {lastError}
|
||||
</p>
|
||||
) : null}
|
||||
{!isRead ? (
|
||||
<form method="post">
|
||||
{csrf ? <input type="hidden" name="csrf" value={csrf} readOnly /> : null}
|
||||
<input type="hidden" name="action" value="mark-read" />
|
||||
<input type="hidden" name="deliveryId" value={item.id} />
|
||||
<button
|
||||
type="submit"
|
||||
className="bg-primary text-primary-foreground hover:bg-primary/90 focus:ring-ring inline-flex items-center rounded-md px-3 py-2 text-sm font-medium focus:ring-2 focus:ring-offset-2 focus:outline-hidden"
|
||||
aria-label={String(t("notifications.center.markReadLabel", { message: messageLabel }))}
|
||||
>
|
||||
{t("notifications.center.markRead")}
|
||||
</button>
|
||||
</form>
|
||||
) : null}
|
||||
</article>
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
)}
|
||||
</section>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user