feat(terminal): kitchen display on paired screens, live without polling (platform-v2 3.2b)

Port of the manager's KdsPage.jsx into terminal/src/apps/kds, split into pure logic
(layout packing, age, buckets, rounds, totals, status cycle, settings; unit-tested) and
components (card, item, action bars, dialogs, item totals, rails, settings panel).
- Settings are the device's config; the settings panel saves them (managers only),
  zoom changes the size for the session.
- Orders reload on live events and every reconnect; polling only while the live
  connection is down (DeviceProvider exposes liveUp; useLiveEvents helper).
- "Tap to start" unlocks sound; venue clock via useFormat; English-first texts.
- Undo of an item recomputes the order status at once; undo/redo clear the selection.
- Tests: terminal unit +15, terminal.mjs +12 KDS checks (no polling, live order + chime,
  item/undo/prepare/decline, settings permission, change made elsewhere), backend
  regression for an item back to pending.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-14 19:27:11 +02:00
co-authored by Claude Opus 5.5
parent d5c4400fde
commit 5f6d4ab428
24 changed files with 2550 additions and 12 deletions
@@ -0,0 +1,213 @@
// An order on the kitchen board (plan step 3.2b, from KdsPage.jsx): header coloured by how
// late it is (age colours are device settings), item count, table / takeaway / delivery,
// order number and status, the items (in columns, by course or by round), the order note
// and the waiter. Tap selects it (buttons appear, items become tappable; it deselects
// itself after 5 s without a touch); a long press on an unselected card shows quick actions.
import { Fragment, useCallback, useEffect, useRef, useState } from 'react'
import { Bike, Info, ShoppingBag, User, Utensils } from 'lucide-react'
import { useTranslation } from '@xenia/i18n'
import { courseGroups, minutesSince } from '../logic/board'
import { BLOCK_GAP, BODY_PAD_B, BODY_PAD_H, BODY_PAD_T, CARD_FOOT_H_BASE, CARD_HEAD_H_BASE, CARD_SUB_H_BASE } from '../logic/layout'
import { KDS } from '../theme'
import { ActionBar, QuickActionBar } from './ActionBars'
import ItemBlock from './ItemBlock'
const HOLD_MS = 500
const IDLE_DESELECT_MS = 5000
const CHANNEL_ICONS = { here: Utensils, takeaway: ShoppingBag, delivery: Bike }
function ChannelBadge({ orderType, tableName, fs }) {
const { t } = useTranslation('terminal')
const type = CHANNEL_ICONS[orderType] ? orderType : 'here'
const Icon = CHANNEL_ICONS[type]
const iconSz = Math.round(17 * fs)
return (
<span style={{ display: 'flex', alignItems: 'center', gap: 6, fontSize: Math.round(15 * fs), fontWeight: 700, whiteSpace: 'nowrap', color: 'rgba(255,255,255,0.60)', flexShrink: 0 }}>
<Icon style={{ width: iconSz, height: iconSz, opacity: 0.65 }} />
{type === 'here' && tableName ? tableName : t(`kds.channel.${type}`)}
</span>
)
}
function headColours(age, colours) {
if (age === 'done') return [colours.done, KDS.doneSub]
if (age === 'future') return [KDS.futureHead, KDS.futureSubBg]
if (age === 'green') return [colours.fresh, KDS.greenD]
if (age === 'amber') return [colours.warning, KDS.amberD]
return [colours.late, KDS.redD]
}
function glowFor(order, age) {
if (age === 'done') return KDS.glowDone
if (age === 'future') return KDS.glowFuture
return order.kds_status === 'preparing' ? KDS.glowPreparing : KDS.glowPending
}
export default function OrderCard({ order, age, ageText, layout, focused, dimmed, settings, multiFilter, courses, now,
onFocus, onStatus, onBumpItem, onPrint, onDecline, onItemDecline, onCallWaiter }) {
const { t } = useTranslation('terminal')
const fs = settings.fontScale
const [headBg, subBg] = headColours(age.age, settings.colours)
const glow = multiFilter ? glowFor(order, age.age) : null
const itemCount = order.items.reduce((s, it) => s + it.quantity, 0)
const waiter = order.waiters?.length ? order.waiters[0] : null
const isFuture = age.age === 'future'
// long press on an unselected card → quick actions
const [quick, setQuick] = useState(false)
const hold = useRef(null)
const cancelHold = () => { if (hold.current) { clearTimeout(hold.current); hold.current = null } }
const onPointerDown = () => {
if (focused) return
hold.current = setTimeout(() => { hold.current = null; setQuick(true) }, HOLD_MS)
}
// a selected card deselects itself after a few seconds without a touch
const idle = useRef(null)
const resetIdle = useCallback(() => {
if (!focused) return
if (idle.current) clearTimeout(idle.current)
idle.current = setTimeout(() => onFocus(null), IDLE_DESELECT_MS)
}, [focused, onFocus])
useEffect(() => {
if (focused) resetIdle()
else if (idle.current) { clearTimeout(idle.current); idle.current = null }
return () => { if (idle.current) clearTimeout(idle.current) }
}, [focused, resetIdle])
const item = it => (
<ItemBlock key={it.id} item={it} focused={focused} fontScale={fs} fontWeight={settings.fontWeight}
combineModifiers={settings.combineModifiers}
onBump={x => { resetIdle(); onBumpItem(order.id, x.id) }}
onDecline={focused ? x => { resetIdle(); onItemDecline(order.id, x.id, x.product_name) } : null} />
)
const column = (key, children) => (
<div key={key} style={{ display: 'flex', flexDirection: 'column', gap: BLOCK_GAP, flex: '1 1 0', minWidth: 0 }}>{children}</div>
)
const deselect = e => { if (focused) { e.stopPropagation(); onFocus(null) } }
let body
if (settings.batchMode === 'grouped' && order._batches?.length > 1) {
body = column('rounds', order._batches.map((batch, bi) => {
const items = order.items.filter(it => (it._batchIndex ?? 0) === bi)
if (!items.length) return null
return (
<Fragment key={bi}>
<div style={{ display: 'flex', alignItems: 'center', gap: 6, fontSize: 10 * fs, fontWeight: 700, textTransform: 'uppercase',
letterSpacing: '.1em', color: '#7b8694', padding: bi === 0 ? '0 0 2px' : '4px 0 2px' }}>
<div style={{ flex: 1, height: 1, background: '#e0e5eb' }} />
<span>{t('kds.card.batch', { n: bi + 1 })}</span>
<span style={{ background: '#f0f2f5', color: '#5b6370', borderRadius: 5, padding: '1px 6px', fontFamily: 'ui-monospace, monospace' }}>
{t('kds.age.minutes', { m: Math.max(0, minutesSince(batch.opened_at, now)) })}
</span>
<div style={{ flex: 1, height: 1, background: '#e0e5eb' }} />
</div>
{items.map(item)}
</Fragment>
)
}))
} else {
body = layout.cols.map((col, ci) => {
const colItems = col.map(idx => order.items[idx]).filter(Boolean)
if (settings.groupByCourse && courses?.length > 0) {
return column(ci, courseGroups(colItems, courses).map((g, gi) => (
<div key={gi}>
<div style={{ fontSize: 10, fontWeight: 700, textTransform: 'uppercase', letterSpacing: '0.08em',
color: g.course ? g.course.color : '#6b7a8d', padding: '4px 10px 2px' }}>
{g.course ? g.course.name : t('kds.card.noCourse')}
</div>
{g.items.map(item)}
</div>
)))
}
return column(ci, colItems.map(item))
})
}
return (
<div
data-testid="kds-card" data-order={order.id} data-status={order.kds_status} data-age={age.age} data-focused={focused ? 'true' : 'false'}
onClick={e => { e.stopPropagation(); cancelHold(); if (!quick) { onFocus(order.id); resetIdle() } }}
onPointerDown={onPointerDown} onPointerUp={cancelHold} onPointerLeave={cancelHold} onPointerCancel={cancelHold}
style={{
position: 'absolute', left: layout.x, top: layout.y, width: layout.w, height: layout.h,
background: headBg, borderRadius: 10, overflow: 'hidden',
boxShadow: focused
? `0 10px 24px rgba(0,0,0,.4), 0 0 0 4px rgba(47,127,240,.25)${glow ? `, 0 0 0 2px ${glow}40` : ''}`
: `0 1px 2px rgba(0,0,0,.25), 0 6px 16px rgba(0,0,0,.22)${glow ? `, 0 0 12px 2px ${glow}55, 0 0 0 2px ${glow}44` : ''}`,
outline: focused ? `2px solid ${KDS.accent}` : '2px solid transparent', outlineOffset: 2,
display: 'flex', flexDirection: 'column',
transform: focused ? 'scale(1.012)' : 'none', transition: 'box-shadow .15s, transform .15s, outline-color .15s',
opacity: dimmed ? 0.42 : 1, filter: dimmed ? 'saturate(.7)' : 'none', zIndex: focused ? 60 : 1,
cursor: 'pointer', userSelect: 'none', WebkitUserSelect: 'none', touchAction: 'none',
}}
>
<div className={age.flash && settings.flashLate ? 'kds-flash' : undefined} style={{
display: 'flex', alignItems: 'center', justifyContent: 'space-between', padding: '0 11px', color: '#fff',
height: Math.round(CARD_HEAD_H_BASE * fs), flexShrink: 0, background: headBg, gap: 8,
}}>
<div style={{ display: 'flex', alignItems: 'center', gap: 8, minWidth: 0 }}>
<span data-testid="card-age" style={{
background: 'rgba(255,255,255,0.92)', color: '#0a0c0f', borderRadius: 7, padding: `0 ${Math.round(9 * fs)}px`,
height: Math.round(30 * fs), fontSize: Math.round(17 * fs), fontWeight: 800, fontVariantNumeric: 'tabular-nums',
letterSpacing: '-0.02em', flexShrink: 0, display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
}}>
{ageText}
</span>
<span style={{ fontSize: 12 * fs, fontWeight: 600, opacity: 0.9, whiteSpace: 'nowrap', lineHeight: 1 }}>
{t('kds.card.items', { count: itemCount })}
</span>
</div>
<div onClick={deselect}><ChannelBadge orderType={order.order_type} tableName={order.table_name} fs={fs} /></div>
</div>
<div style={{
display: 'flex', alignItems: 'center', justifyContent: 'space-between', background: subBg, color: 'rgba(255,255,255,0.88)',
padding: '0 11px', height: Math.round(CARD_SUB_H_BASE * fs), flexShrink: 0, fontSize: 12.5 * fs, fontWeight: 600,
}}>
<span style={{ fontFamily: 'ui-monospace, monospace', letterSpacing: '.02em', opacity: 0.85, lineHeight: 1 }}>#{order.id}</span>
<span onClick={deselect} style={{ fontSize: 11 * fs, fontWeight: 700, letterSpacing: '.08em', textTransform: 'uppercase', lineHeight: 1 }}>
{t(`kds.card.status.${order.kds_status === 'done' || order.kds_status === 'preparing' ? order.kds_status : 'pending'}`)}
</span>
</div>
<div style={{
background: isFuture ? '#f0f2f5' : order.kds_status === 'done' ? '#f4f7f4' : order.kds_status === 'preparing' ? '#f3f8ff' : '#ffffff',
paddingTop: BODY_PAD_T, paddingBottom: BODY_PAD_B, paddingLeft: BODY_PAD_H, paddingRight: BODY_PAD_H,
display: 'flex', gap: 12, flex: '1 1 auto', overflow: 'hidden',
}}>
{body}
</div>
{order.notes && (
<div style={{ background: '#fffbeb', borderTop: `1px solid ${KDS.line}`, flexShrink: 0 }}>
<div style={{ height: Math.round(6 * fs) }} />
<div style={{
padding: `0 ${Math.round(BODY_PAD_H * fs)}px ${Math.round(8 * fs)}px`, fontSize: Math.round(12.5 * fs), fontWeight: 600,
color: '#b45309', fontStyle: 'italic', lineHeight: 1.45, display: 'flex', alignItems: 'flex-start', gap: Math.round(5 * fs),
}}>
<Info style={{ width: Math.round(13 * fs), height: Math.round(13 * fs), flexShrink: 0, marginTop: Math.round(2 * fs), color: '#d97706' }} />
<span>{order.notes}</span>
</div>
</div>
)}
{waiter && (
<div style={{
background: KDS.cardFoot, color: KDS.cardFootInk, display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 7,
height: Math.round(CARD_FOOT_H_BASE * fs), flexShrink: 0, fontSize: 13 * fs, fontWeight: 700, borderTop: `1px solid ${KDS.line}`,
}}>
<User style={{ width: 13, height: 13, opacity: 0.7 }} />
{waiter}
</div>
)}
{quick && !focused && (
<QuickActionBar order={order} onStatus={onStatus} onDecline={onDecline} onDismiss={() => setQuick(false)} fs={fs} />
)}
{focused && (
<ActionBar order={order} onStatus={onStatus} onPrint={onPrint} onCallWaiter={onCallWaiter} fs={fs} hasWaiter={!!waiter} />
)}
</div>
)
}