feat: the table map (platform-v2 step 6.8, D-31)

- floor_plans / floor_items (0035), tables' plan_id + width/height/rotation/shape;
  /api/floor-plans (set-up, catalog.tables.manage, floor_plans.changed) and
  /api/floor-plans/live (each table's state, guests, waiters, time; zone rule)
- @xenia/floorplan: the drawing (Floor, Item, TableShape, FloorView) and its geometry
  (grid snap in whole cells, outlines, chairs), tested
- manager: Management -> Tables -> Floor plans (editor: drag, grid, irregular floor,
  walls/bar/labels/areas, background picture), Tables page List / Map
- counter POS: Tables List / Map; waiter: Map mode, the list stays the default
- tests: test_floor_plans.py, floor + geometry unit tests, manager/waiter/terminal e2e

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-18 17:00:45 +02:00
co-authored by Claude Opus 5.5
parent fd3254aa31
commit f4aae6abbc
47 changed files with 2102 additions and 8 deletions
+106
View File
@@ -0,0 +1,106 @@
// The live table map (plan step 6.8, D-31), the same in the waiter app, the counter POS and the
// manager's Tables page. `data` is GET /api/floor-plans/live ({plans, tables}); the app reloads it
// on its live events. A tab per floor plan (of the zone, when one is chosen), zoom buttons, and
// the tables not on any plan as buttons under it. A tap on a table calls onPick(table).
// Styles are inline with @xenia/ui tokens, so it fits the apps with and without Tailwind.
import { useEffect, useMemo, useState } from 'react'
import { useTranslation } from '@xenia/i18n'
import { plansFor, splitTables, toneOf, TONES } from './geometry.js'
import { Floor, Item, TableShape } from './Shapes.jsx'
const ZOOMS = [0.5, 0.75, 1, 1.25, 1.5, 2, 3]
function useMinuteClock() {
const [now, setNow] = useState(() => Date.now())
useEffect(() => {
const id = setInterval(() => setNow(Date.now()), 30_000)
return () => clearInterval(id)
}, [])
return now
}
const btn = active => ({
padding: '6px 12px', borderRadius: 999, border: `1px solid ${active ? 'var(--x-accent)' : 'var(--x-line)'}`,
background: active ? 'var(--x-accent-soft)' : 'var(--x-surface)', color: active ? 'var(--x-accent-ink)' : 'var(--x-ink-2)',
fontWeight: 600, fontSize: 13, cursor: 'pointer', whiteSpace: 'nowrap',
})
const round = { width: 36, height: 36, borderRadius: 999, border: '1px solid var(--x-line)', background: 'var(--x-surface)', color: 'var(--x-ink)', fontSize: 18, fontWeight: 700, cursor: 'pointer' }
export function FloorView({ data, zone = null, onPick, mediaBase = '', height = '100%' }) {
const { t } = useTranslation('common')
const now = useMinuteClock()
const plans = useMemo(() => plansFor(data?.plans ?? [], zone), [data, zone])
const [chosen, setChosen] = useState(null)
const [zoom, setZoom] = useState(null) // null = fit the width
const plan = plans.find(p => p.id === chosen) ?? plans[0] ?? null
const { placed, unplaced } = useMemo(() => splitTables(data?.tables ?? [], plan?.id ?? -1, zone), [data, plan, zone])
if (!data) return null
if (!plan) {
return (
<div style={{ padding: 24, textAlign: 'center', color: 'var(--x-ink-2)' }} data-testid="floor-none">
<p style={{ fontWeight: 700, color: 'var(--x-ink)', marginBottom: 6 }}>{t('floorMap.none')}</p>
<p style={{ fontSize: 13 }}>{t('floorMap.noneHint')}</p>
</div>
)
}
const step = d => setZoom(z => {
const cur = z ?? 1
const i = ZOOMS.findIndex(v => v >= cur - 0.001)
return ZOOMS[Math.min(ZOOMS.length - 1, Math.max(0, (i < 0 ? ZOOMS.length - 1 : i) + d))]
})
return (
<div style={{ display: 'flex', flexDirection: 'column', gap: 8, height, minHeight: 0 }} data-testid="floor-view" data-plan={plan.id}>
<div style={{ display: 'flex', alignItems: 'center', gap: 6, flexWrap: 'wrap' }}>
{plans.length > 1 && plans.map(p => (
<button key={p.id} type="button" style={btn(p.id === plan.id)} onClick={() => setChosen(p.id)} aria-pressed={p.id === plan.id} data-testid="floor-plan-tab">{p.name}</button>
))}
<span style={{ flex: 1 }} />
<button type="button" style={round} onClick={() => step(-1)} aria-label={t('floorMap.zoomOut')}>−</button>
<button type="button" style={{ ...round, width: 'auto', padding: '0 12px', fontSize: 13 }} onClick={() => setZoom(null)}>{t('floorMap.fit')}</button>
<button type="button" style={round} onClick={() => step(1)} aria-label={t('floorMap.zoomIn')}>+</button>
</div>
<div style={{ flex: 1, minHeight: 0, overflow: 'auto', borderRadius: 12, background: 'var(--x-canvas)', border: '1px solid var(--x-line)' }}>
<svg viewBox={`0 0 ${plan.width} ${plan.height}`} role="img" aria-label={plan.name}
style={{ display: 'block', width: zoom ? `${plan.width * zoom}px` : '100%', height: 'auto', touchAction: 'pan-x pan-y pinch-zoom' }}>
<Floor plan={plan} mediaBase={mediaBase} />
{plan.items.map(i => <Item key={i.id} item={i} label={t('floorMap.bar')} />)}
{placed.map(table => (
<TableShape key={table.id} table={table} now={now} role="button" tabIndex={0} style={{ cursor: 'pointer' }}
aria-label={`${table.name}: ${t(`floorMap.state.${toneOf(table)}`)}`} data-testid="floor-table"
onClick={() => onPick?.(table)} onKeyDown={e => { if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); onPick?.(table) } }} />
))}
</svg>
</div>
<Legend t={t} />
{unplaced.length > 0 && (
<div style={{ display: 'flex', alignItems: 'center', gap: 6, flexWrap: 'wrap', fontSize: 12, color: 'var(--x-ink-2)' }} data-testid="floor-unplaced">
<span>{t('floorMap.notOnMap')}</span>
{unplaced.map(table => {
const tone = TONES[toneOf(table)]
return (
<button key={table.id} type="button" onClick={() => onPick?.(table)} data-table={table.id}
style={{ padding: '4px 10px', borderRadius: 8, border: `1.5px solid ${tone.stroke}`, background: tone.fill, color: tone.ink, fontWeight: 600, cursor: 'pointer' }}>
{table.name}
</button>
)
})}
</div>
)}
</div>
)
}
function Legend({ t }) {
return (
<div style={{ display: 'flex', gap: 12, flexWrap: 'wrap', fontSize: 12, color: 'var(--x-ink-2)' }}>
{['free', 'mine', 'open', 'paid', 'reserved'].map(k => (
<span key={k} style={{ display: 'inline-flex', alignItems: 'center', gap: 5 }}>
<span style={{ width: 12, height: 12, borderRadius: 4, background: TONES[k].fill, border: `2px solid ${TONES[k].stroke}` }} />
{t(`floorMap.state.${k}`)}
</span>
))}
<span>{t('floorMap.legendGuests')}</span>
</div>
)
}
+111
View File
@@ -0,0 +1,111 @@
// The pieces of a floor plan as SVG (plan step 6.8): the floor (outline, grid, background
// picture), the walls / bar / labels / areas, and a table with its chairs and what it shows.
// Colours are @xenia/ui tokens (CSS variables), so the same drawing works in every app.
import { chairs, durationText, outlineOf, peopleText, pointsAttr, TONES, toneOf } from './geometry.js'
/** The floor: background picture, the floor's shape, and the grid (when asked). */
export function Floor({ plan, showGrid = false, mediaBase = '' }) {
const pts = pointsAttr(outlineOf(plan))
const g = plan.grid_size
const clip = `floor-clip-${plan.id ?? 'new'}`
return (
<g>
<defs>
<clipPath id={clip}><polygon points={pts} /></clipPath>
{showGrid && g > 0 && (
<pattern id={`${clip}-grid`} width={g} height={g} patternUnits="userSpaceOnUse">
<path d={`M ${g} 0 L 0 0 0 ${g}`} fill="none" stroke="var(--x-line)" strokeWidth="1" />
</pattern>
)}
</defs>
<polygon points={pts} fill="var(--x-surface)" stroke="var(--x-line-strong)" strokeWidth="3" />
{plan.background?.full && (
<image href={`${mediaBase}${plan.background.full}`} x="0" y="0" width={plan.width} height={plan.height}
preserveAspectRatio="xMidYMid slice" opacity={plan.background_opacity ?? 0.6} clipPath={`url(#${clip})`} />
)}
{showGrid && g > 0 && <rect x="0" y="0" width={plan.width} height={plan.height} fill={`url(#${clip}-grid)`} clipPath={`url(#${clip})`} pointerEvents="none" />}
</g>
)
}
const ITEM_LOOK = {
wall: { fill: 'var(--x-ink-2)', stroke: 'none' },
bar: { fill: 'var(--x-surface-3)', stroke: 'var(--x-ink-3)' },
label: { fill: 'none', stroke: 'none' },
area: { fill: 'var(--x-surface-2)', stroke: 'var(--x-line-strong)', dash: '8 6' },
}
/** A wall, the bar, a label or an area. */
export function Item({ item, selected = false, label, ...rest }) {
const look = ITEM_LOOK[item.kind] ?? ITEM_LOOK.area
const w = item.width
const h = item.height
const text = item.text || (item.kind === 'bar' ? label : '')
return (
<g transform={`translate(${item.x} ${item.y}) rotate(${item.rotation || 0})`} {...rest}>
<rect x={-w / 2} y={-h / 2} width={w} height={h} rx={item.kind === 'wall' ? 2 : 8}
fill={look.fill} stroke={selected ? 'var(--x-accent)' : look.stroke} strokeWidth={selected ? 4 : 2} strokeDasharray={selected ? undefined : look.dash} />
{item.kind === 'label' && selected && <rect x={-w / 2} y={-h / 2} width={w} height={h} fill="none" stroke="var(--x-accent)" strokeWidth="2" strokeDasharray="6 4" />}
{text && (
<text x="0" y="0" textAnchor="middle" dominantBaseline="central" fill="var(--x-ink-2)" pointerEvents="none"
fontSize={Math.max(12, Math.min(28, h * 0.5, (w / Math.max(text.length, 1)) * 1.6))} fontWeight="600"
transform={`rotate(${-(item.rotation || 0)})`}>{text}</text>
)}
</g>
)
}
/** A small person (for seats / guests): drawn, so it looks the same everywhere. */
function Person({ x, y, size, color }) {
const r = size / 2
return (
<g transform={`translate(${x} ${y})`} fill={color}>
<circle cx={0} cy={-r * 0.35} r={r * 0.32} />
<path d={`M ${-r * 0.6} ${r * 0.55} Q 0 ${-r * 0.25} ${r * 0.6} ${r * 0.55} Z`} />
</g>
)
}
/**
* A table: its shape, chairs for its seats, and (live) its name, guests / seats, time open,
* waiters and a "ready" count. `now` (ms) moves the clock.
*/
export function TableShape({ table, live = true, selected = false, now = Date.now(), ...rest }) {
const tone = TONES[live ? toneOf(table) : 'free']
const w = table.width || 80
const h = table.height || 80
const rot = table.rotation || 0
const o = live ? table.order : null
const fs = Math.max(11, Math.min(22, Math.min(w, h) * 0.24))
const lines = [table.name]
const second = o ? `${o.guests ?? '–'}/${table.seats ?? '–'}` : table.seats ? `${table.seats}` : ''
const third = o ? [durationText(o.opened_at ? Math.max(0, Math.floor((now - new Date(o.opened_at).getTime()) / 60000)) : null), peopleText(o.people)].filter(Boolean).join(' · ') : ''
return (
<g transform={`translate(${table.x} ${table.y})`} data-table={table.id} data-state={live ? toneOf(table) : undefined} {...rest}>
<g transform={`rotate(${rot})`}>
{chairs(table.shape, w, h, table.seats).map(([cx, cy], i) => (
<circle key={i} cx={cx} cy={cy} r={Math.max(6, Math.min(10, fs * 0.45))} fill="var(--x-surface-2)" stroke={tone.stroke} strokeWidth="1.5" />
))}
{table.shape === 'round'
? <ellipse cx="0" cy="0" rx={w / 2} ry={h / 2} fill={tone.fill} stroke={selected ? 'var(--x-accent)' : tone.stroke} strokeWidth={selected ? 5 : 3} />
: <rect x={-w / 2} y={-h / 2} width={w} height={h} rx="10" fill={tone.fill} stroke={selected ? 'var(--x-accent)' : tone.stroke} strokeWidth={selected ? 5 : 3} />}
</g>
<g fill={tone.ink} pointerEvents="none">
<text x="0" y={second || third ? -fs * (third ? 0.9 : 0.45) : 0} textAnchor="middle" dominantBaseline="central" fontSize={fs} fontWeight="700">{lines[0]}</text>
{second && (
<g>
<Person x={-fs * 0.95} y={third ? fs * 0.15 : fs * 0.6} size={fs * 0.85} color={tone.ink} />
<text x={fs * 0.1} y={third ? fs * 0.15 : fs * 0.6} textAnchor="middle" dominantBaseline="central" fontSize={fs * 0.75} fontWeight="600">{second}</text>
</g>
)}
{third && <text x="0" y={fs * 1.1} textAnchor="middle" dominantBaseline="central" fontSize={fs * 0.62}>{third}</text>}
</g>
{o?.ready > 0 && (
<g transform={`translate(${w / 2 - 4} ${-h / 2 + 4})`} pointerEvents="none">
<circle r={fs * 0.55} fill="var(--x-success)" />
<text textAnchor="middle" dominantBaseline="central" fontSize={fs * 0.6} fontWeight="700" fill="var(--x-success-fg)">{o.ready}</text>
</g>
)}
</g>
)
}
+139
View File
@@ -0,0 +1,139 @@
// The table map's geometry (plan step 6.8, D-31), pure and tested (tests/geometry.test.mjs).
// Plan units: 1 unit = 1 px at 100 %. A table's or an item's (x, y) is its centre; rotation in
// degrees, clockwise. The grid is a "snap to fixed positions" grid: positions (centres) and sizes
// become multiples of the grid size (half a cell for odd sizes, so a 1-cell table sits in a cell).
export const SHAPES = ['square', 'round', 'long']
export const ITEM_KINDS = ['wall', 'bar', 'label', 'area']
export const GRID_SIZES = [20, 25, 40, 50, 80, 100]
/** A value on the grid (off: as it is, to 1 decimal). */
export function snap(v, grid) {
if (!grid) return Math.round(v * 10) / 10
return Math.round(v / grid) * grid
}
/** A size on the grid: whole cells, at least one. */
export const snapSize = (v, grid) => (grid ? Math.max(grid, Math.round(v / grid) * grid) : Math.max(10, Math.round(v * 10) / 10))
/** A centre on the grid: an odd number of cells puts the centre in the middle of a cell. */
export function snapCentre(c, size, grid) {
if (!grid) return Math.round(c * 10) / 10
const cells = Math.round(size / grid)
const off = cells % 2 ? grid / 2 : 0
return Math.round((c - off) / grid) * grid + off
}
export const clamp = (v, lo, hi) => Math.min(Math.max(v, lo), hi)
/** Where a thing goes after a drag, kept on the plan and (grid on) on the grid. */
export function moveTo(thing, x, y, plan) {
const grid = plan.grid_on ? plan.grid_size : 0
return { ...thing, x: clamp(snapCentre(x, thing.width, grid), 0, plan.width), y: clamp(snapCentre(y, thing.height, grid), 0, plan.height) }
}
/** A new table's size from its shape and seats (2 seats: small; long tables grow with seats). */
export function defaultSize(shape, seats, grid = 0) {
const n = Math.max(1, seats || 4)
let w = 80
let h = 80
if (shape === 'round') { w = h = n <= 2 ? 70 : n <= 4 ? 90 : 120 }
else if (shape === 'square') { w = h = n <= 2 ? 70 : 90 }
else { h = 80; w = Math.max(140, Math.ceil(n / 2) * 60) }
return { width: snapSize(w, grid), height: snapSize(h, grid) }
}
export const ITEM_SIZES = { wall: [300, 12], bar: [320, 60], label: [140, 36], area: [300, 200] }
/** The floor's corners: its outline, or the whole rectangle. */
export const outlineOf = plan => (plan.outline?.length >= 3 ? plan.outline : [[0, 0], [plan.width, 0], [plan.width, plan.height], [0, plan.height]])
export const pointsAttr = points => points.map(([x, y]) => `${x},${y}`).join(' ')
/** A corner more: the middle of the edge after corner `i`. */
export function addCorner(points, i) {
const a = points[i]
const b = points[(i + 1) % points.length]
const out = [...points]
out.splice(i + 1, 0, [Math.round((a[0] + b[0]) / 2), Math.round((a[1] + b[1]) / 2)])
return out
}
/** A corner less (a floor keeps at least 3). */
export const removeCorner = (points, i) => (points.length <= 3 ? points : points.filter((_, j) => j !== i))
/** Move corner `i` (kept on the plan, on the grid). */
export function moveCorner(points, i, x, y, plan) {
const grid = plan.grid_on ? plan.grid_size : 0
return points.map((p, j) => (j === i ? [clamp(snap(x, grid), 0, plan.width), clamp(snap(y, grid), 0, plan.height)] : p))
}
/** Where the chairs go around a table (centres, relative to the table's centre). */
export function chairs(shape, width, height, seats) {
const n = Math.max(0, Math.min(seats || 0, 24))
if (!n) return []
const gap = 10
if (shape === 'round') {
const rx = width / 2 + gap
const ry = height / 2 + gap
return Array.from({ length: n }, (_, i) => {
const a = (2 * Math.PI * i) / n - Math.PI / 2
return [Math.round(rx * Math.cos(a) * 10) / 10, Math.round(ry * Math.sin(a) * 10) / 10]
})
}
// rectangles: along the two long sides (one at each end when there are few)
const along = width >= height
const len = along ? width : height
const top = Math.ceil(n / 2)
const bottom = n - top
const row = (count, side) => Array.from({ length: count }, (_, i) => {
const t = -len / 2 + (len * (i + 0.5)) / count
const off = side * ((along ? height : width) / 2 + gap)
return along ? [Math.round(t * 10) / 10, off] : [off, Math.round(t * 10) / 10]
})
if (n === 1) return [along ? [0, -(height / 2 + gap)] : [-(width / 2 + gap), 0]]
return [...row(top, -1), ...row(bottom, 1)]
}
/** How a table looks from its state: free | open | mine | paid | reserved. */
export function toneOf(table) {
if (table.state === 'open' && table.mine) return 'mine'
return table.state ?? 'free'
}
export const TONES = {
free: { fill: 'var(--x-surface)', stroke: 'var(--x-line-strong)', ink: 'var(--x-ink)' },
open: { fill: 'var(--x-info-soft)', stroke: 'var(--x-info)', ink: 'var(--x-info-ink)' },
mine: { fill: 'var(--x-accent-soft)', stroke: 'var(--x-accent)', ink: 'var(--x-accent-ink)' },
paid: { fill: 'var(--x-success-soft)', stroke: 'var(--x-success)', ink: 'var(--x-success-ink)' },
reserved: { fill: 'var(--x-special-soft)', stroke: 'var(--x-special)', ink: 'var(--x-special-ink)' }, // not the accent: some venues' accent is amber
}
/** Minutes since an order opened. */
export const minutesSince = (iso, now = Date.now()) => (iso ? Math.max(0, Math.floor((now - new Date(iso).getTime()) / 60000)) : null)
/** "42′" under an hour, "1:05" after. */
export function durationText(min) {
if (min == null) return ''
if (min < 60) return `${min}′`
return `${Math.floor(min / 60)}:${String(min % 60).padStart(2, '0')}`
}
/** "Maria", "Maria +1". */
export const peopleText = people => (!people?.length ? '' : people.length === 1 ? people[0] : `${people[0]} +${people.length - 1}`)
/** The tables of one plan, and the ones not on any plan (of a zone, when given). */
export function splitTables(tables, planId, zone = null) {
const inZone = zone == null ? tables : tables.filter(t => t.group_id === zone)
return { placed: tables.filter(t => t.plan_id === planId && t.x != null), unplaced: inZone.filter(t => t.plan_id == null || t.x == null) }
}
/** The plans to offer for a zone (null = all): the zone's plans and the plans without a zone. */
export const plansFor = (plans, zone = null) => (zone == null ? plans : plans.filter(p => p.group_id === zone || p.group_id == null))
/** Turn a point on screen (client coordinates) into plan units, for an <svg> drawn with a viewBox. */
export function toPlan(svg, clientX, clientY) {
const r = svg.getBoundingClientRect()
const vb = svg.viewBox.baseVal
return [vb.x + ((clientX - r.left) / r.width) * vb.width, vb.y + ((clientY - r.top) / r.height) * vb.height]
}
+5
View File
@@ -0,0 +1,5 @@
// @xenia/floorplan (plan step 6.8, D-31): the table map, drawn the same in the waiter app, the
// counter POS and the manager (the live map and the editor), with its pure geometry.
export * from './geometry.js'
export { Floor, Item, TableShape } from './Shapes.jsx'
export { FloorView } from './FloorView.jsx'