feat(manager): notification bell, sign-in/venue/print settings, log download (platform-v2 2.6f)
- Bell in the header: /api/platform/notifications with unread count, live via
the new LiveProvider (one createLiveClient connection) on platform_notification
and reconnect; mark read / all; warning+critical pop a toast.
- Settings: Sign-in card (manager/waiter/device modes, require username, one
session per manager/waiter, username autofill) replaces the dead v1
security.login_method card; new "Venue & language" tab (language, time zone);
print alert threshold in Printing. Tab labels are i18n keys.
- Technical > Log files: list + download of the daily logs (owner-level).
Backend: GET /api/system/logs, /api/system/logs/{date} (core.settings.manage),
error system.log_not_found.
- Setup wizard asks staff language and time zone (setup/init stores them).
- @xenia/ui theme helpers (applyTheme, getStoredTheme); the manager stays light
until phase 6 converts the older pages (owner decision 2026-11-14).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
// 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 load = useCallback(async () => {
|
||||
try {
|
||||
const r = await client.get('/api/platform/notifications', { params: { limit: 30 } })
|
||||
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 (
|
||||
<div className="relative" ref={ref}>
|
||||
<button
|
||||
onClick={() => { setOpen(o => !o); if (!open) load() }}
|
||||
className="relative flex h-8 w-8 items-center justify-center rounded-lg border border-slate-200 bg-white text-slate-500 transition hover:bg-slate-50 hover:text-slate-700"
|
||||
aria-label={data.unread ? t('notifications.bellUnread', { count: data.unread }) : t('notifications.bell')}
|
||||
aria-haspopup="dialog"
|
||||
aria-expanded={open}
|
||||
data-testid="notif-bell"
|
||||
>
|
||||
<Bell className="h-3.5 w-3.5" />
|
||||
{data.unread > 0 && (
|
||||
<span className="absolute -top-1.5 -right-1.5 min-w-[18px] h-[18px] px-1 rounded-full bg-danger text-danger-fg text-[10px] font-bold leading-[18px] text-center"
|
||||
data-testid="notif-count">{badge}</span>
|
||||
)}
|
||||
</button>
|
||||
|
||||
{open && (
|
||||
<div className="absolute right-0 top-full mt-1.5 w-96 max-w-[calc(100vw-2rem)] rounded-card border border-line bg-surface shadow-popover z-popover"
|
||||
role="dialog" aria-label={t('notifications.title')} data-testid="notif-panel">
|
||||
<div className="flex items-center justify-between px-4 py-2.5 border-b border-line">
|
||||
<span className="text-label font-semibold text-ink">{t('notifications.title')}</span>
|
||||
{data.unread > 0 && (
|
||||
<button onClick={markAll} className="inline-flex items-center gap-1 text-caption text-accent-ink hover:underline" data-testid="notif-read-all">
|
||||
<CheckCheck className="h-3.5 w-3.5" />{t('notifications.markAll')}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
{data.items.length === 0
|
||||
? <p className="px-4 py-8 text-center text-label text-ink-3">{t('notifications.empty')}</p>
|
||||
: (
|
||||
<ul className="max-h-[60vh] overflow-y-auto divide-y divide-line">
|
||||
{data.items.map(n => (
|
||||
<li key={n.id}>
|
||||
<button onClick={() => markRead(n)} className={`w-full text-left flex gap-3 px-4 py-3 hover:bg-surface-2 ${n.read ? '' : 'bg-accent-soft/40'}`}
|
||||
data-testid={`notif-${n.id}`} data-read={n.read ? 'true' : 'false'}>
|
||||
<span className={`mt-1.5 h-2 w-2 shrink-0 rounded-full ${DOT[n.severity] || DOT.info}`} />
|
||||
<span className="flex-1 min-w-0">
|
||||
<span className={`block text-label ${n.read ? 'text-ink-2' : 'text-ink font-medium'}`}>{n.message}</span>
|
||||
<span className="block text-caption text-ink-3 mt-0.5">
|
||||
{f.formatRelative(n.last_at)}
|
||||
{n.count > 1 && ` · ${t('notifications.times', { count: n.count })}`}
|
||||
</span>
|
||||
</span>
|
||||
</button>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user