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:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user