// AK-P3 follow-up — macOS-style in-app notification toasts (top-right, below // the shell header). Unread IN_APP deliveries pop up as dismissible toasts: // X hides the toast (persisted per browser via localStorage, the delivery // stays unread in the center), clicking the body marks it read and follows // the deeplink. Data comes from the notifications route loader plus the // shared SSE hook, so live arrivals toast too when the stream is available. import { useEffect, useMemo, useState } from "react"; import { useFetcher, useNavigate } from "react-router"; import { useTranslation } from "react-i18next"; import { FeatherX } from "@subframe/core"; import type { NotificationCenterVM } from "@hivegrid/view-models"; import { Toast } from "~/ui/components/Toast"; import { BadgeFeedback } from "~/ui/components/BadgeFeedback"; import { useNotificationsStream, type NotificationsStreamState } from "~/components/notifications/useNotificationsStream"; import { useRootData } from "~/utils/data/useRootData"; import { useLocaleFormatters } from "~/locale/format"; import { appPath } from "~/utils/app/CanonicalUrlUtils"; const MAX_VISIBLE_TOASTS = 3; const MAX_STORED_DISMISSED_IDS = 200; function storageKeyFor(tenantSlug: string): string { return `hg.notification-toasts.dismissed.${tenantSlug}`; } function readDismissedIds(tenantSlug: string): string[] { if (typeof window === "undefined") return []; try { const raw = window.localStorage.getItem(storageKeyFor(tenantSlug)); const parsed = raw ? JSON.parse(raw) : []; return Array.isArray(parsed) ? parsed.filter((id): id is string => typeof id === "string") : []; } catch { return []; } } function persistDismissedIds(tenantSlug: string, ids: string[]): void { if (typeof window === "undefined") return; try { window.localStorage.setItem(storageKeyFor(tenantSlug), JSON.stringify(ids.slice(-MAX_STORED_DISMISSED_IDS))); } catch { // Storage may be unavailable (private mode) — toasts then reappear on reload. } } /** * Shared shell notification state: loads the notification-center data once per * tenant and keeps it live via the SSE hook. Drives both the bell badge * (unreadCount) and the toast stack (items). */ export function useShellNotifications(tenantSlug: string | null): { items: NotificationsStreamState["items"]; unreadCount: number } { const loadFetcher = useFetcher<{ vm: NotificationCenterVM; streamUrl: string }>({ key: "notification-toasts" }); useEffect(() => { if (tenantSlug) loadFetcher.load(appPath({ tenantSlug, path: "notifications", mode: "host-aware" })); // eslint-disable-next-line react-hooks/exhaustive-deps }, [tenantSlug]); const initialItems = useMemo( () => (loadFetcher.data?.vm.status === "ready" && loadFetcher.data.vm.data ? loadFetcher.data.vm.data.items : []), [loadFetcher.data] ); const stream = useNotificationsStream(tenantSlug && loadFetcher.data ? loadFetcher.data.streamUrl : null, initialItems); return { items: stream.items, unreadCount: stream.unreadCount }; } export function NotificationToasts({ tenantSlug, items }: { tenantSlug: string; items: NotificationsStreamState["items"] }) { const { t } = useTranslation(); const navigate = useNavigate(); const rootData = useRootData(); const formatters = useLocaleFormatters(); const markFetcher = useFetcher({ key: "notification-toasts-mark" }); const [dismissedIds, setDismissedIds] = useState(() => readDismissedIds(tenantSlug)); const notificationsPath = appPath({ tenantSlug, path: "notifications", mode: "host-aware" }); useEffect(() => { setDismissedIds(readDismissedIds(tenantSlug)); }, [tenantSlug]); const visibleToasts = items .filter((item) => !item.isRead && !dismissedIds.includes(item.id)) .slice(0, MAX_VISIBLE_TOASTS); function dismiss(id: string) { const next = [...dismissedIds, id]; setDismissedIds(next); persistDismissedIds(tenantSlug, next); } function open(item: (typeof visibleToasts)[number]) { dismiss(item.id); const form = new FormData(); form.set("action", "mark-read"); form.set("deliveryId", item.id); if (rootData?.csrf) form.set("csrf", rootData.csrf); markFetcher.submit(form, { method: "post", action: notificationsPath }); navigate(item.deeplinkHref ?? notificationsPath); } if (visibleToasts.length === 0) return null; return (
{visibleToasts.map((item) => (
))}
); } export default NotificationToasts;