feat(terminal): the customer display, with an idle screen each venue designs

Plan step 3.4. Owner (2026-11-16): the idle screen is configurable per venue.
- routers/customer_display.py (module customer_display, /api/display): the counter POS
  pushes its state (POST /api/display/state: idle | cart | paid with lines, total, paid,
  change); only the displays linked to that POS get `display.state`; the last state is kept
  in memory for a display that reconnects (GET /api/display/state). pos_counter's device
  scope includes customer_display.state.
- The idle screen: setting customer_display.idle (name, logo, logo + name, blank, one
  image, slideshow, QR code to the online ordering / QR menu / any link; a line of text;
  dark or light); images uploaded to /app/data/display_media (JPG/PNG/WebP/GIF ≤ 5 MB,
  random names, served publicly on the LAN); a change sends `display.settings` to every
  display. New key customer_display.manage (owners, store managers; migration 0022).
- Terminal: CustomerDisplayApp (cart with a large total, "Thank you" with the change for
  12 s, the venue's idle screen, its theme), logic.js (unit-tested); the POS pushes from the
  sale cart and the payment dialog (tables, online orders too) via useDisplay().
  qrcode.react added to the terminal (already in the workspace).
- Manager: Settings → Customer display (style cards, venue name, text, look, slide seconds,
  QR target/link/caption, image upload/pick/order/delete).
- Tests: tests/test_customer_display.py (3), terminal display.test.mjs (3); e2e terminal
  (linked vs unlinked display, < 1 s, paid, QR and slideshow idle screens), manager (the
  settings tab).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-16 08:34:05 +02:00
co-authored by Claude Opus 5.5
parent 18d580d87c
commit 88a9703902
27 changed files with 1073 additions and 25 deletions
+1
View File
@@ -18,6 +18,7 @@
"@xenia/ui": "^0.0.0",
"axios": "^1.21.1",
"lucide-react": "^1.73.0",
"qrcode.react": "^4.2.0",
"react": "^19.3.1",
"react-dom": "^19.3.1",
"react-router-dom": "^7.18.5"
@@ -1,6 +1,132 @@
// Placeholder until step 3.4 builds this app (plan phase 3). Lazily loaded (apps/registry.js).
import AppPlaceholder from '../AppPlaceholder'
// The customer display (plan step 3.4): a screen facing the customer, paired with sign-in
// "none" and linked to a counter POS in the manager's pair dialog. While the cashier sells it
// shows the lines and the total; after payment "Thank you" with what was paid and the change;
// in between, the idle screen the venue designed in Settings → Customer display (the name, the
// logo, both, nothing, an image, a slideshow, or a QR code, with a line of text, light or dark).
// The POS's state arrives live (`display.state`, addressed to this screen only); a settings
// change re-reads it (`display.settings`). GET /api/display/state on start and reconnect.
import { useCallback, useEffect, useRef, useState } from 'react'
import { QRCodeSVG } from 'qrcode.react'
import { useFormat, useTranslation } from '@xenia/i18n'
import { applyTheme, getStoredTheme } from '@xenia/ui'
import client from '../../api/client'
import { useLiveEvent, useLiveEvents } from '../../device/context'
import { idleParts, PAID_MS, slideAt, viewOf } from './logic'
const quiet = { xenia: { noOverride: true } }
export default function CustomerDisplayApp() {
return <AppPlaceholder appKey="customer_display" />
const [data, setData] = useState(null)
const [state, setState] = useState(null)
const [receivedAt, setReceivedAt] = useState(0)
const [now, setNow] = useState(() => Date.now())
const load = useCallback(async () => {
try {
const { data: d } = await client.get('/api/display/state', quiet)
setData(d)
setState(s => s ?? d.state)
setReceivedAt(r => r || Date.now())
} catch { /* the shell shows offline */ }
}, [])
useEffect(() => { load() }, [load])
useLiveEvent('ready', load)
useLiveEvents(['display.settings', 'device.updated'], load)
useLiveEvent('display.state', next => { setState(next); setReceivedAt(Date.now()) })
// the clock moves the paid screen to idle and the slides along
useEffect(() => {
const id = setInterval(() => setNow(Date.now()), 1000)
return () => clearInterval(id)
}, [])
const theme = data?.idle?.theme ?? 'dark'
useEffect(() => {
applyTheme(theme)
return () => { applyTheme(getStoredTheme()) }
}, [theme])
if (!data) return <main className="absolute inset-0 bg-canvas" data-testid="app-customer_display" />
const view = viewOf(state, receivedAt, now)
return (
<main className="absolute inset-0 bg-canvas text-ink overflow-hidden select-none" data-testid="app-customer_display" data-view={view}>
{view === 'cart' && <CartView state={state} venue={data.venue_name} />}
{view === 'paid' && <PaidView state={state} />}
{view === 'idle' && <IdleView idle={data.idle} venue={data.venue_name} now={now} linked={data.linked} />}
</main>
)
}
function CartView({ state, venue }) {
const { t } = useTranslation('terminal')
const { formatMoney, formatNumber } = useFormat()
const end = useRef(null)
useEffect(() => { end.current?.scrollIntoView({ block: 'end' }) }, [state.lines.length])
return (
<div className="h-full flex flex-col" data-testid="display-cart">
{venue && <header className="px-8 py-4 text-title text-ink-2 border-b border-line">{venue}</header>}
<ul className="flex-1 overflow-hidden px-8 py-4 grid content-start gap-3">
{state.lines.map((l, i) => (
<li key={i} className="flex items-baseline gap-4 text-3xl" data-testid="display-line">
<span className="w-20 shrink-0 text-ink-2 tabular-nums">{formatNumber(l.quantity)}×</span>
<span className="flex-1 min-w-0">
<span className="font-medium">{l.name}</span>
{l.options?.length > 0 && <span className="block text-xl text-ink-2">{l.options.join(' · ')}</span>}
</span>
<span className="tabular-nums">{formatMoney(l.total)}</span>
</li>
))}
<li ref={end} />
</ul>
<footer className="px-8 py-6 bg-surface border-t border-line flex items-baseline justify-between">
<span className="text-3xl text-ink-2">{t('display.total')}</span>
<span className="text-7xl font-semibold tabular-nums" data-testid="display-total">{formatMoney(state.total)}</span>
</footer>
</div>
)
}
function PaidView({ state }) {
const { t } = useTranslation('terminal')
const { formatMoney } = useFormat()
return (
<div className="h-full flex flex-col items-center justify-center gap-6 text-center p-8" data-testid="display-paid">
<p className="text-7xl font-semibold text-success-ink">{t('display.thanks')}</p>
<p className="text-3xl text-ink-2">{t('display.paid', { amount: formatMoney(state.paid ?? state.total) })}</p>
{state.change > 0 && <p className="text-5xl font-semibold tabular-nums" data-testid="display-change">{t('display.change', { amount: formatMoney(state.change) })}</p>}
<span className="sr-only">{PAID_MS}</span>
</div>
)
}
function IdleView({ idle, venue, now, linked }) {
const { t } = useTranslation('terminal')
const p = idleParts(idle, venue)
const slide = slideAt(p.slides.length, idle?.slide_seconds, now)
const full = p.image || p.slides.length > 0
return (
<div className="h-full relative flex flex-col items-center justify-center gap-6 p-8 text-center" data-testid="display-idle" data-style={idle?.style}>
{p.image && <img src={p.image} alt="" className="absolute inset-0 w-full h-full object-cover" data-testid="display-image" />}
{p.slides.map((src, i) => (
<img key={src} src={src} alt="" data-testid="display-slide" data-current={i === slide ? 'true' : 'false'}
className={`absolute inset-0 w-full h-full object-cover transition-opacity duration-1000 ${i === slide ? 'opacity-100' : 'opacity-0'}`} />
))}
{!p.blank && !full && (
<>
{p.logo && <img src={p.logo} alt={venue || ''} className="max-h-[45vh] max-w-[70vw] object-contain" data-testid="display-logo" />}
{p.name && <h1 className="text-7xl font-semibold" data-testid="display-name">{p.name}</h1>}
{p.qr && (
<div className="grid justify-items-center gap-4" data-testid="display-qr" data-link={p.qr}>
<div className="p-5 rounded-card bg-surface shadow-raised"><QRCodeSVG value={p.qr} size={320} includeMargin={false} /></div>
{p.caption && <p className="text-4xl font-semibold">{p.caption}</p>}
</div>
)}
</>
)}
{p.message && (
<p className={`text-3xl ${full ? 'absolute bottom-8 inset-x-8 py-3 rounded-card bg-surface/80 text-ink' : 'text-ink-2'}`} data-testid="display-message">{p.message}</p>
)}
{!linked && <p className="absolute bottom-3 right-4 text-label text-ink-3" data-testid="display-unlinked">{t('display.unlinked')}</p>}
</div>
)
}
@@ -0,0 +1,33 @@
// The customer display's logic (plan step 3.4). Pure: unit-tested in Node (tests/display.test.mjs).
export const PAID_MS = 12_000 // "Thank you" stays this long, then the idle screen
export const STYLES = ['name', 'logo', 'name_logo', 'blank', 'image', 'slideshow', 'qr']
/** What to show: 'idle' | 'cart' | 'paid'. A paid screen goes idle after PAID_MS; an empty cart is idle. */
export function viewOf(state, receivedAt, now = Date.now()) {
if (!state || state.mode === 'idle') return 'idle'
if (state.mode === 'paid') return now - receivedAt < PAID_MS ? 'paid' : 'idle'
return state.lines?.length ? 'cart' : 'idle'
}
/** The parts of the idle screen to draw, from the venue's settings (GET /api/display/state → idle). */
export function idleParts(idle, venueName) {
const s = STYLES.includes(idle?.style) ? idle.style : 'name'
const logo = idle?.logo_url || null
return {
name: (s === 'name' || s === 'name_logo' || (s === 'logo' && !logo)) ? (venueName || null) : null,
logo: (s === 'logo' || s === 'name_logo') ? logo : null,
image: s === 'image' ? idle?.image_url || null : null,
slides: s === 'slideshow' ? idle?.slide_urls ?? [] : [],
qr: s === 'qr' ? idle?.qr_link || null : null,
caption: s === 'qr' ? idle?.qr_caption || null : null,
message: s === 'blank' ? null : idle?.message || null,
blank: s === 'blank',
}
}
/** The slide to show at `now` (every `seconds`). */
export function slideAt(count, seconds, now = Date.now()) {
if (!count) return -1
return Math.floor(now / (Math.max(3, seconds || 8) * 1000)) % count
}
+13 -2
View File
@@ -3,12 +3,13 @@
// by row. The caller's `submit({ method, rowMethods, cashGiven })` makes the one call: a quick
// sale (POST /api/pos/sales, rows = cart lines) or a table's items (POST /api/pos/orders/{id}/pay,
// rows = order items). Cash with the exact amount is pre-selected: Charge → Take = two taps.
import { useState } from 'react'
import { useEffect, useRef, useState } from 'react'
import { Banknote, Check, CreditCard, Split } from 'lucide-react'
import { errorMessage } from '@xenia/api'
import { parseNumber, useFormat, useTranslation } from '@xenia/i18n'
import { Button, Dialog, Input } from '@xenia/ui'
import { changeFor, quickAmounts, splitPayments } from './logic/cart'
import { useDisplay } from './usePos'
const tab = on => `flex-1 min-h-[var(--x-control-lg)] rounded-control flex items-center justify-center gap-2 text-body-lg font-medium transition-colors duration-fast
${on ? 'bg-accent text-accent-fg' : 'bg-surface-2 text-ink-2 hover:bg-surface-3'}`
@@ -29,6 +30,13 @@ export default function PaymentDialog({ total, rows, submit, onPaid, onClose })
const [busy, setBusy] = useState(false)
const [error, setError] = useState('')
const short = cashDue > 0 && change == null
// the customer display (3.4): the bill while paying, then "Thank you" with the change
const display = useDisplay()
const paidRef = useRef(false)
useEffect(() => {
display({ mode: 'cart', lines: rows.map(r => ({ name: r.label, total: r.total })), total })
return () => { if (!paidRef.current) display({ mode: 'idle' }) }
}, []) // eslint-disable-line react-hooks/exhaustive-deps -- once, when the dialog opens
const pay = async () => {
if (busy || short) return
@@ -36,7 +44,10 @@ export default function PaymentDialog({ total, rows, submit, onPaid, onClose })
setError('')
const payments = method === 'split' ? parts.payments : [{ method, lines: rows.map((_, i) => i) }]
try {
onPaid(await submit({ method, rowMethods: split, payments, cashGiven: cashDue > 0 ? (given ?? cashDue) : null }))
const result = await submit({ method, rowMethods: split, payments, cashGiven: cashDue > 0 ? (given ?? cashDue) : null })
paidRef.current = true
display({ mode: 'paid', total, paid: result?.cash_given ?? total, change: result?.change ?? 0 })
onPaid(result)
} catch (err) {
setError(errorMessage(err, t))
} finally {
+19 -2
View File
@@ -1,7 +1,7 @@
// The quick-sale cart (plan steps 3.3a–b): order type, the lines priced by the backend,
// Charge → PaymentDialog (POST /api/pos/sales); after a sale it shows what happened and
// "Same again".
import { useState } from 'react'
import { useEffect, useRef, useState } from 'react'
import { Banknote, Repeat, ShoppingBag, Trash2, Utensils } from 'lucide-react'
import { useFormat, useTranslation } from '@xenia/i18n'
import { Button, EmptyState, useToast } from '@xenia/ui'
@@ -9,7 +9,7 @@ import client from '../../api/client'
import CartLine from './CartLine'
import { setLineQty } from './logic/cart'
import PaymentDialog from './PaymentDialog'
import { useQuote } from './usePos'
import { useDisplay, useQuote } from './usePos'
export default function SaleCart({ cart, setCart, orderType, setOrderType, byId, canDiscount }) {
const { t } = useTranslation('terminal')
@@ -20,6 +20,22 @@ export default function SaleCart({ cart, setCart, orderType, setOrderType, byId,
const quote = useQuote(cart, orderType)
const ready = cart.length > 0 && !quote.pending && !quote.error && quote.quote?.lines?.length === cart.length
// the customer display (3.4) follows the cart; after a sale it shows "Thank you" (sent by
// the payment) until it times out, so an empty cart then says nothing
const display = useDisplay()
const thanking = useRef(false) // the display shows "Thank you" for the last sale
useEffect(() => {
if (paying) return
if (!cart.length) { if (!thanking.current) display({ mode: 'idle' }); return }
if (!ready) return
thanking.current = false
display({
mode: 'cart', total: quote.quote.total,
lines: cart.map((l, i) => ({ name: byId.get(l.product_id)?.name ?? '', quantity: l.quantity, total: quote.quote.lines[i].total })),
})
}, [cart, ready, quote.quote, paying, byId, display])
useEffect(() => () => display({ mode: 'idle' }), [display])
const submit = async ({ method, payments, cashGiven }) => (await client.post('/api/pos/sales', {
order_type: orderType, lines: cart, payments: method === 'split' ? payments : [{ method }], cash_given: cashGiven,
})).data
@@ -65,6 +81,7 @@ export default function SaleCart({ cart, setCart, orderType, setOrderType, byId,
onClose={() => setPaying(false)}
onPaid={result => {
setPaying(false)
thanking.current = true
setLastSale({ lines: cart, orderType, result })
setCart([])
toast.success(result.change > 0 ? t('pos.done.toastChange', { change: formatMoney(result.change) }) : t('pos.done.toast'))
+16
View File
@@ -2,6 +2,7 @@
// the menu, the cart priced by the backend, the tables, a table's order, online orders and receipts. Kept fresh by the
// live connection, so a waiter's phone and the counter see each other's changes at once.
import { useCallback, useEffect, useRef, useState } from 'react'
import { useModule } from '@xenia/access'
import client from '../../api/client'
import { useLiveEvent, useLiveEvents } from '../../device/context'
@@ -130,6 +131,21 @@ export function useOnline(enabled) {
return { data, reload: load }
}
// The customer display (plan step 3.4): what this counter's customer should see. One timer for
// the whole screen, so the last thing said wins; "paid" goes at once, the rest after a moment
// (a burst of cart changes is one message). Nothing is sent while the module is off.
let displayTimer = null
/** push({ mode: 'idle' | 'cart' | 'paid', lines?, total?, paid?, change? }) → POST /api/display/state. */
export function useDisplay() {
const on = useModule('customer_display')
return useCallback(state => {
if (!on) return
clearTimeout(displayTimer)
displayTimer = setTimeout(() => { client.post('/api/display/state', state, quiet).catch(() => {}) }, state.mode === 'paid' ? 0 : 200)
}, [on])
}
const BOOKING_EVENTS = ['reservation_requested', 'reservation_updated']
/** GET /api/connect/bookings: { requested, upcoming } (null while loading); live (S.6). */
+37
View File
@@ -0,0 +1,37 @@
// The customer display's logic (plan step 3.4).
import assert from 'node:assert/strict'
import { test } from 'node:test'
import { idleParts, PAID_MS, slideAt, viewOf } from '../src/apps/customer-display/logic.js'
test('what the display shows: the cart, thank you (for a while), idle', () => {
const t0 = 1_000_000
assert.equal(viewOf(null, 0, t0), 'idle')
assert.equal(viewOf({ mode: 'cart', lines: [{}] }, t0, t0), 'cart')
assert.equal(viewOf({ mode: 'cart', lines: [] }, t0, t0), 'idle')
assert.equal(viewOf({ mode: 'paid' }, t0, t0 + 1000), 'paid')
assert.equal(viewOf({ mode: 'paid' }, t0, t0 + PAID_MS + 1), 'idle')
})
test('the idle screen the venue chose', () => {
const idle = { logo_url: '/l.png', image_url: '/i.png', slide_urls: ['/a.png', '/b.png'], qr_link: 'https://x/order/s', qr_caption: 'Order online', message: 'Hi' }
assert.deepEqual(idleParts({ ...idle, style: 'name' }, 'Taverna'), { name: 'Taverna', logo: null, image: null, slides: [], qr: null, caption: null, message: 'Hi', blank: false })
assert.equal(idleParts({ ...idle, style: 'logo' }, 'Taverna').name, null)
assert.equal(idleParts({ ...idle, style: 'logo', logo_url: null }, 'Taverna').name, 'Taverna', 'no logo yet: the name instead')
const both = idleParts({ ...idle, style: 'name_logo' }, 'Taverna')
assert.equal(both.name, 'Taverna'); assert.equal(both.logo, '/l.png')
assert.equal(idleParts({ ...idle, style: 'image' }, 'T').image, '/i.png')
assert.deepEqual(idleParts({ ...idle, style: 'slideshow' }, 'T').slides, ['/a.png', '/b.png'])
const qr = idleParts({ ...idle, style: 'qr' }, 'T')
assert.equal(qr.qr, 'https://x/order/s'); assert.equal(qr.caption, 'Order online')
const blank = idleParts({ ...idle, style: 'blank' }, 'T')
assert.equal(blank.blank, true); assert.equal(blank.message, null); assert.equal(blank.name, null)
assert.equal(idleParts({ style: 'nonsense' }, 'T').name, 'T')
})
test('the slideshow moves on every N seconds', () => {
assert.equal(slideAt(0, 8, 0), -1)
assert.equal(slideAt(3, 8, 0), 0)
assert.equal(slideAt(3, 8, 8000), 1)
assert.equal(slideAt(3, 8, 24_000), 0)
assert.equal(slideAt(2, 1, 3000), 1, 'at least 3 seconds a slide')
})