// The bell in the manager's header (plan step 2.6f, APPS_PLATFORM §10): platform // notifications (license limits and expiry, support access used, print failures…) from // GET /api/platform/notifications, in the reader's language (the backend renders `message`). // - The unread count shows on the bell; it refetches on the live event // `platform_notification` {id} and after every reconnect ('ready'). // - A NEW warning/critical one also pops up as a toast. // - Opening an item marks it read; "Mark all as read" does the rest. // Not staff chat (/api/notifications, the chat module), which stays separate. import { useCallback, useEffect, useRef, useState } from 'react' import toast from 'react-hot-toast' import { Bell, CheckCheck } from 'lucide-react' import { useTranslation, useFormat } from '@xenia/i18n' import client from '../api/client' import { useLiveEvent } from '../live/useLiveEvent' const DOT = { info: 'bg-info', warning: 'bg-warning', critical: 'bg-danger' } const FALLBACK_REFRESH_MS = 5 * 60_000 export default function NotificationBell() { const { t } = useTranslation('manager') const f = useFormat() const [data, setData] = useState({ items: [], unread: 0 }) const [open, setOpen] = useState(false) const ref = useRef(null) const asked = useRef(0) const load = useCallback(async () => { // Loads overlap (the timer, a reconnect, a live event); only the newest may set the list, // else an answer sent before the event lands after it and hides the new one (seen in e2e). const mine = ++asked.current try { const r = await client.get('/api/platform/notifications', { params: { limit: 30 } }) if (mine === asked.current) setData(r.data) return r.data } catch { return null // the bell stays as it was; the next event or refresh tries again } }, []) useEffect(() => { load() const id = setInterval(load, FALLBACK_REFRESH_MS) return () => clearInterval(id) }, [load]) useLiveEvent('ready', load) useLiveEvent('platform_notification', async ev => { const fresh = await load() const n = fresh?.items.find(x => x.id === ev?.id) if (n && n.severity !== 'info') toast(n.message, { icon: '⚠️', duration: 8000 }) }) useEffect(() => { if (!open) return undefined const outside = e => { if (ref.current && !ref.current.contains(e.target)) setOpen(false) } const escape = e => { if (e.key === 'Escape') setOpen(false) } document.addEventListener('mousedown', outside) document.addEventListener('keydown', escape) return () => { document.removeEventListener('mousedown', outside) document.removeEventListener('keydown', escape) } }, [open]) async function markRead(n) { if (n.read) return setData(d => ({ unread: Math.max(0, d.unread - 1), items: d.items.map(x => (x.id === n.id ? { ...x, read: true } : x)) })) await client.post(`/api/platform/notifications/${n.id}/read`).catch(() => {}) } async function markAll() { setData(d => ({ unread: 0, items: d.items.map(x => ({ ...x, read: true })) })) await client.post('/api/platform/notifications/read-all').catch(() => {}) load() } const badge = data.unread > 9 ? '9+' : String(data.unread) return (
{open && (
{t('notifications.title')} {data.unread > 0 && ( )}
{data.items.length === 0 ?

{t('notifications.empty')}

: ( )}
)}
) }