- setup wizard (src/pages/setup): welcome (pick the language: the wizard switches at once), the owner's account, the venue (kind, name, staff language, time zone), an optional lock-screen PIN, finish -> sign in; English and Greek - shell (src/shell): AppLayout (license banner in both languages, header, lock button, idle lock / sign-out), Sidebar on a new 'nav' token set in @xenia/ui (light + dark), ProfileDialog (name, username, password, 4-6 digit PIN), licenseContext; App.jsx loads the session and the setup state with react-query (no setState in effects); the header's dead search box gone - Management -> Tables -> List (src/pages/tables, was TablesConfigTab): zones as tabs (drag to reorder), zone name / prefix / colour, add many numbered on, a table's name (6 chars) / zone / seats, twice-used names flagged, off / on / delete, pick several to move, rename in a series or delete (says how many failed) - the old src/ui kit, layouts/, components/Sidebar + EditProfileModal, the 'primary' palette and the .btn/.card/.input classes deleted; NotificationBell / UserMenuButton on tokens; @xenia/ui: nav tokens, 'scrim' in the Tailwind preset - v2.json paths = src (the guards cover all of it); npm run lint = eslint src; argsIgnorePattern for components in JSX - tests: shell.test.mjs (3), e2e setup.mjs (new: a fresh install through the wizard in Greek to the overview, in run-all), manager.mjs 'tables list' (zone, 3 tables, rename series, profile PIN, rail tokens) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
133 lines
6.0 KiB
React
133 lines
6.0 KiB
React
// 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 (
|
|
<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-line bg-surface text-ink-3 transition hover:bg-surface-2 hover:text-ink"
|
|
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>
|
|
)
|
|
}
|