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:
@@ -0,0 +1,294 @@
|
||||
// One floor plan's editor (plan step 6.8, D-31). Drag tables and things on the floor; the panel
|
||||
// on the right edits what's selected (shape, seats, size, rotation, text), adds the tables that
|
||||
// aren't on the plan and walls / the bar / labels / areas, and holds the plan's settings: name,
|
||||
// zone, size, the grid (snap on / off and its size), the floor's shape (drag its corners; add or
|
||||
// remove corners) and a background picture from the media library. Arrow keys move the selected
|
||||
// thing (a grid cell, or 5; Shift: 1), Delete takes it off, Escape lets go. Nothing is saved
|
||||
// until "Save"; then every app's map changes at once.
|
||||
import { useMemo, useRef, useState } from 'react'
|
||||
import toast from 'react-hot-toast'
|
||||
import { RotateCcw, RotateCw, Save, Trash2, Undo2 } from 'lucide-react'
|
||||
import { errorMessage } from '@xenia/api'
|
||||
import {
|
||||
Floor, GRID_SIZES, ITEM_KINDS, Item, SHAPES, TableShape, addCorner, defaultSize, moveCorner, moveTo, outlineOf, removeCorner, toPlan,
|
||||
} from '@xenia/floorplan'
|
||||
import { useTranslation } from '@xenia/i18n'
|
||||
import { Button, Dialog, Field, IconButton, Input, Select, Switch, cx } from '@xenia/ui'
|
||||
import MediaField from '../media/MediaField'
|
||||
import * as api from './floorApi'
|
||||
import { addItem, addable, find, fitOutline, layoutBody, layoutOf, nudge, placeTable, planBody, remove, resize, rotate, sameLayout, update } from './editorLogic'
|
||||
|
||||
const PAD = 30 // room around the floor for the corner handles
|
||||
|
||||
export default function FloorEditor({ plan: saved, data, groups, onSaved, onDeleted }) {
|
||||
const { t } = useTranslation('manager')
|
||||
const initial = useMemo(() => layoutOf(data, saved.id), [data, saved.id])
|
||||
const [layout, setLayout] = useState(initial)
|
||||
const [form, setForm] = useState(() => ({ ...saved }))
|
||||
const [sel, setSel] = useState(null) // {type: 'table'|'item', id} | {type: 'corner', index}
|
||||
const [shapeMode, setShapeMode] = useState(false)
|
||||
const [newShape, setNewShape] = useState('square')
|
||||
const [busy, setBusy] = useState(false)
|
||||
const [deleting, setDeleting] = useState(false)
|
||||
const svg = useRef(null)
|
||||
const drag = useRef(null)
|
||||
|
||||
const plan = form // the preview follows the unsaved settings
|
||||
const dirtySettings = JSON.stringify(planBody(form)) !== JSON.stringify(planBody(saved))
|
||||
const dirty = dirtySettings || !sameLayout(layout, initial)
|
||||
const selected = sel && sel.type !== 'corner' ? find(layout, sel) : null
|
||||
const corners = outlineOf(plan)
|
||||
const setF = (k, v) => setForm(f => ({ ...f, [k]: v }))
|
||||
const { own, other } = addable(data, plan, layout)
|
||||
|
||||
const point = e => toPlan(svg.current, e.clientX, e.clientY)
|
||||
const start = (e, target) => {
|
||||
e.stopPropagation()
|
||||
svg.current?.focus()
|
||||
const [px, py] = point(e)
|
||||
if (target.type === 'corner') {
|
||||
setSel(target)
|
||||
drag.current = { target }
|
||||
} else {
|
||||
const thing = find(layout, target)
|
||||
setSel(target)
|
||||
drag.current = { target, dx: px - thing.x, dy: py - thing.y }
|
||||
}
|
||||
svg.current.setPointerCapture?.(e.pointerId)
|
||||
}
|
||||
const move = e => {
|
||||
const d = drag.current
|
||||
if (!d) return
|
||||
const [px, py] = point(e)
|
||||
if (d.target.type === 'corner') {
|
||||
setForm(f => ({ ...f, outline: moveCorner(outlineOf(f), d.target.index, px, py, f) }))
|
||||
return
|
||||
}
|
||||
setLayout(l => {
|
||||
const thing = find(l, d.target)
|
||||
if (!thing) return l
|
||||
const m = moveTo(thing, px - d.dx, py - d.dy, plan)
|
||||
return update(l, d.target, { x: m.x, y: m.y })
|
||||
})
|
||||
}
|
||||
const end = () => { drag.current = null }
|
||||
const keys = e => {
|
||||
if (!sel || sel.type === 'corner') return
|
||||
const dirs = { ArrowLeft: [-1, 0], ArrowRight: [1, 0], ArrowUp: [0, -1], ArrowDown: [0, 1] }
|
||||
if (dirs[e.key]) { e.preventDefault(); setLayout(l => nudge(l, sel, plan, ...dirs[e.key], e.shiftKey)) }
|
||||
else if (e.key === 'Delete' || e.key === 'Backspace') { e.preventDefault(); setLayout(l => remove(l, sel)); setSel(null) }
|
||||
else if (e.key === 'Escape') setSel(null)
|
||||
}
|
||||
|
||||
const save = async () => {
|
||||
setBusy(true)
|
||||
try {
|
||||
if (dirtySettings) await api.savePlan(saved.id, planBody(form))
|
||||
await api.saveLayout(saved.id, layoutBody(layout))
|
||||
toast.success(t('floor.saved'))
|
||||
onSaved()
|
||||
} catch (err) { toast.error(errorMessage(err, t)) } finally { setBusy(false) }
|
||||
}
|
||||
const zoneName = id => groups.find(g => g.id === id)?.name ?? t('floor.noZone')
|
||||
|
||||
return (
|
||||
<div className="grid gap-4 lg:grid-cols-[1fr_18rem] items-start" data-testid="floor-editor" data-plan={saved.id}>
|
||||
<div className="grid gap-3 content-start min-w-0">
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<h2 className="text-body-lg font-semibold text-ink mr-auto">{form.name || saved.name}</h2>
|
||||
{dirty && <span className="px-2 py-0.5 rounded-full bg-warning-soft text-warning-ink text-caption font-medium" data-testid="floor-dirty">{t('floor.unsaved')}</span>}
|
||||
<Button variant="secondary" size="sm" icon={<Undo2 size={16} />} disabled={!dirty || busy}
|
||||
onClick={() => { setLayout(initial); setForm({ ...saved }); setSel(null) }}>{t('floor.undoAll')}</Button>
|
||||
<Button size="sm" icon={<Save size={16} />} loading={busy} disabled={!dirty || !form.name.trim()} onClick={save} data-testid="floor-save">{t('floor.save')}</Button>
|
||||
</div>
|
||||
<div className="flex flex-wrap items-center gap-3 text-label text-ink-2">
|
||||
<Switch checked={!!form.grid_on} onChange={v => setF('grid_on', v)} label={t('floor.grid')} data-testid="floor-grid" />
|
||||
{form.grid_on && (
|
||||
<Select size="sm" value={String(form.grid_size)} onChange={e => setF('grid_size', Number(e.target.value))} aria-label={t('floor.gridSize')}
|
||||
options={[...new Set([...GRID_SIZES, form.grid_size])].sort((a, b) => a - b).map(v => ({ value: String(v), label: t('floor.gridCell', { size: v }) }))} />
|
||||
)}
|
||||
<Switch checked={shapeMode} onChange={v => { setShapeMode(v); setSel(null) }} label={t('floor.editShape')} data-testid="floor-shape-mode" />
|
||||
</div>
|
||||
{shapeMode && <p className="text-caption text-ink-2">{t('floor.shapeHint')}</p>}
|
||||
<div className="rounded-card border border-line bg-canvas overflow-auto max-h-[70vh]">
|
||||
<svg ref={svg} viewBox={`${-PAD} ${-PAD} ${plan.width + 2 * PAD} ${plan.height + 2 * PAD}`} tabIndex={0} onKeyDown={keys} role="application" aria-label={t('floor.canvas')}
|
||||
className="block w-full h-auto touch-none select-none outline-none focus-visible:ring-2 focus-visible:ring-[var(--x-accent)]"
|
||||
onPointerMove={move} onPointerUp={end} onPointerCancel={end} onPointerDown={() => setSel(null)} data-testid="floor-canvas">
|
||||
<Floor plan={plan} showGrid={!!plan.grid_on} />
|
||||
{layout.items.map(i => (
|
||||
<Item key={i.key} item={i} label={t('floor.kinds.bar')} selected={sel?.type === 'item' && sel.id === i.key}
|
||||
style={{ cursor: shapeMode ? 'default' : 'move' }} pointerEvents={shapeMode ? 'none' : undefined}
|
||||
onPointerDown={e => start(e, { type: 'item', id: i.key })} data-testid="floor-item" data-kind={i.kind} />
|
||||
))}
|
||||
{layout.tables.map(tb => (
|
||||
<TableShape key={tb.id} table={tb} live={false} selected={sel?.type === 'table' && sel.id === tb.id}
|
||||
style={{ cursor: shapeMode ? 'default' : 'move' }} pointerEvents={shapeMode ? 'none' : undefined}
|
||||
onPointerDown={e => start(e, { type: 'table', id: tb.id })} data-testid="floor-table" />
|
||||
))}
|
||||
{shapeMode && corners.map(([x, y], i) => (
|
||||
<circle key={i} cx={x} cy={y} r={Math.max(10, plan.width / 90)} data-testid="floor-corner"
|
||||
fill={sel?.type === 'corner' && sel.index === i ? 'var(--x-accent)' : 'var(--x-surface)'} stroke="var(--x-accent)" strokeWidth="3"
|
||||
style={{ cursor: 'grab' }} onPointerDown={e => start(e, { type: 'corner', index: i })} />
|
||||
))}
|
||||
</svg>
|
||||
</div>
|
||||
<p className="text-caption text-ink-3">{t('floor.keysHint')}</p>
|
||||
</div>
|
||||
|
||||
<aside className="grid gap-4 content-start lg:max-h-[calc(100vh-10rem)] lg:overflow-y-auto">
|
||||
{shapeMode ? (
|
||||
<Panel title={t('floor.shapeTitle')}>
|
||||
<p className="text-caption text-ink-2">{t('floor.cornerCount', { count: corners.length })}</p>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
<Button size="sm" variant="secondary" disabled={sel?.type !== 'corner'} data-testid="floor-corner-add"
|
||||
onClick={() => setForm(f => ({ ...f, outline: addCorner(outlineOf(f), sel.index) }))}>{t('floor.addCorner')}</Button>
|
||||
<Button size="sm" variant="secondary" disabled={sel?.type !== 'corner' || corners.length <= 3}
|
||||
onClick={() => { setForm(f => ({ ...f, outline: removeCorner(outlineOf(f), sel.index) })); setSel(null) }}>{t('floor.removeCorner')}</Button>
|
||||
<Button size="sm" variant="ghost" disabled={!form.outline} onClick={() => { setF('outline', null); setSel(null) }}>{t('floor.rectangle')}</Button>
|
||||
</div>
|
||||
</Panel>
|
||||
) : selected ? (
|
||||
<Selected thing={selected} sel={sel} plan={plan} t={t}
|
||||
onChange={patch => setLayout(l => update(l, sel, patch))}
|
||||
onRemove={() => { setLayout(l => remove(l, sel)); setSel(null) }} />
|
||||
) : (
|
||||
<Panel title={t('floor.add')}>
|
||||
<Field label={t('floor.newTableShape')}>
|
||||
<Select size="sm" value={newShape} onChange={e => setNewShape(e.target.value)} options={SHAPES.map(s => ({ value: s, label: t(`floor.shapes.${s}`) }))} />
|
||||
</Field>
|
||||
{own.length + other.length === 0 ? <p className="text-caption text-ink-3">{t('floor.allPlaced')}</p> : (
|
||||
<div className="grid gap-2">
|
||||
<TableButtons tables={own} title={plan.group_id != null ? t('floor.zoneTables', { zone: zoneName(plan.group_id) }) : t('floor.offPlan')}
|
||||
onAdd={tb => { setLayout(l => placeTable(l, plan, tb, newShape)); setSel({ type: 'table', id: tb.id }) }} />
|
||||
{other.length > 0 && <TableButtons tables={other} title={t('floor.otherTables')} quiet
|
||||
onAdd={tb => { setLayout(l => placeTable(l, plan, tb, newShape)); setSel({ type: 'table', id: tb.id }) }} />}
|
||||
</div>
|
||||
)}
|
||||
<div className="grid gap-1.5">
|
||||
<span className="text-label font-semibold text-ink">{t('floor.things')}</span>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{ITEM_KINDS.map(k => (
|
||||
<Button key={k} size="sm" variant="secondary" data-testid={`floor-add-${k}`}
|
||||
onClick={() => { const r = addItem(layout, plan, k, k === 'label' ? t('floor.labelText') : null); setLayout(r.layout); setSel({ type: 'item', id: r.key }) }}>
|
||||
{t(`floor.kinds.${k}`)}
|
||||
</Button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</Panel>
|
||||
)}
|
||||
|
||||
<Panel title={t('floor.settings')}>
|
||||
<Field label={t('floor.name')}><Input size="sm" value={form.name} maxLength={60} onChange={e => setF('name', e.target.value)} data-testid="floor-name" /></Field>
|
||||
<Field label={t('floor.zone')} hint={t('floor.zoneHint')}>
|
||||
<Select size="sm" value={form.group_id ?? ''} onChange={e => setF('group_id', e.target.value === '' ? null : Number(e.target.value))}
|
||||
options={[{ value: '', label: t('floor.noZone') }, ...groups.map(g => ({ value: g.id, label: g.name }))]} />
|
||||
</Field>
|
||||
<div className="grid grid-cols-2 gap-2">
|
||||
<Field label={t('floor.width')}>
|
||||
<Input size="sm" type="number" min={200} max={6000} step={50} value={form.width}
|
||||
onChange={e => { const w = Number(e.target.value); setForm(f => ({ ...f, width: w, outline: fitOutline(f.outline, w, f.height) })) }} />
|
||||
</Field>
|
||||
<Field label={t('floor.height')}>
|
||||
<Input size="sm" type="number" min={200} max={6000} step={50} value={form.height}
|
||||
onChange={e => { const h = Number(e.target.value); setForm(f => ({ ...f, height: h, outline: fitOutline(f.outline, f.width, h) })) }} />
|
||||
</Field>
|
||||
</div>
|
||||
<MediaField value={form.background} onChange={img => setF('background', img)} label={t('floor.background')} testid="floor-background" />
|
||||
{form.background && (
|
||||
<label className="grid gap-1 text-label text-ink-2">{t('floor.backgroundOpacity', { value: Math.round((form.background_opacity ?? 0.6) * 100) })}
|
||||
<input type="range" min="0.1" max="1" step="0.05" value={form.background_opacity ?? 0.6} onChange={e => setF('background_opacity', Number(e.target.value))} />
|
||||
</label>
|
||||
)}
|
||||
<Button size="sm" variant="ghost" icon={<Trash2 size={16} />} onClick={() => setDeleting(true)} className="justify-self-start text-danger-ink">{t('floor.delete')}</Button>
|
||||
</Panel>
|
||||
</aside>
|
||||
|
||||
{deleting && (
|
||||
<Dialog open size="sm" title={t('floor.deleteTitle')} onClose={() => setDeleting(false)}
|
||||
footer={<>
|
||||
<Button variant="secondary" onClick={() => setDeleting(false)}>{t('floor.cancel')}</Button>
|
||||
<Button variant="danger" loading={busy} data-testid="floor-confirm-delete" onClick={async () => {
|
||||
setBusy(true)
|
||||
try { await api.deletePlan(saved.id); toast.success(t('floor.deleted')); onDeleted() } catch (err) { toast.error(errorMessage(err, t)) } finally { setBusy(false) }
|
||||
}}>{t('floor.delete')}</Button>
|
||||
</>}>
|
||||
<p>{t('floor.deleteBody', { name: saved.name })}</p>
|
||||
</Dialog>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function Panel({ title, children }) {
|
||||
return (
|
||||
<section className="grid gap-3 p-4 rounded-card border border-line bg-surface shadow-raised">
|
||||
<h3 className="text-label font-semibold text-ink uppercase tracking-wide">{title}</h3>
|
||||
{children}
|
||||
</section>
|
||||
)
|
||||
}
|
||||
|
||||
function TableButtons({ tables, title, onAdd, quiet = false }) {
|
||||
if (!tables.length) return null
|
||||
return (
|
||||
<div className="grid gap-1.5">
|
||||
<span className={cx('text-caption', quiet ? 'text-ink-3' : 'text-ink-2')}>{title}</span>
|
||||
<div className="flex flex-wrap gap-1.5" data-testid={quiet ? 'floor-other-tables' : 'floor-own-tables'}>
|
||||
{tables.map(tb => (
|
||||
<button key={tb.id} type="button" onClick={() => onAdd(tb)} data-table={tb.id} data-name={tb.name}
|
||||
className="min-w-[2.75rem] px-2.5 py-1.5 rounded-control border border-line bg-surface-2 text-label font-semibold text-ink hover:border-accent hover:bg-accent-soft">
|
||||
{tb.name}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function Selected({ thing, sel, plan, t, onChange, onRemove }) {
|
||||
const isTable = sel.type === 'table'
|
||||
const setSize = (w, h) => onChange(resize(thing, plan, w, h))
|
||||
return (
|
||||
<Panel title={isTable ? t('floor.table', { name: thing.name }) : t(`floor.kinds.${thing.kind}`)}>
|
||||
{isTable && (
|
||||
<>
|
||||
<Field label={t('floor.shape')}>
|
||||
<div className="flex gap-1.5" role="radiogroup">
|
||||
{SHAPES.map(s => (
|
||||
<button key={s} type="button" role="radio" aria-checked={thing.shape === s} data-testid={`floor-shape-${s}`}
|
||||
onClick={() => onChange({ shape: s, ...defaultSize(s, thing.seats, plan.grid_on ? plan.grid_size : 0) })}
|
||||
className={cx('flex-1 px-2 py-1.5 rounded-control border text-label font-medium',
|
||||
thing.shape === s ? 'border-accent bg-accent-soft text-accent-ink' : 'border-line bg-surface text-ink-2 hover:bg-surface-2')}>
|
||||
{t(`floor.shapes.${s}`)}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</Field>
|
||||
<Field label={t('floor.seats')} hint={t('floor.seatsHint')}>
|
||||
<Input size="sm" type="number" min={0} max={99} value={thing.seats ?? ''} data-testid="floor-seats"
|
||||
onChange={e => onChange({ seats: e.target.value === '' ? null : Math.max(0, Math.min(99, Math.round(Number(e.target.value)))) })} />
|
||||
</Field>
|
||||
</>
|
||||
)}
|
||||
{!isTable && thing.kind !== 'wall' && (
|
||||
<Field label={t('floor.text')}>
|
||||
<Input size="sm" value={thing.text ?? ''} maxLength={60} onChange={e => onChange({ text: e.target.value })} data-testid="floor-item-text" />
|
||||
</Field>
|
||||
)}
|
||||
<div className="grid grid-cols-2 gap-2">
|
||||
<Field label={t('floor.itemWidth')}><Input size="sm" type="number" min={4} value={thing.width} onChange={e => setSize(Number(e.target.value), undefined)} /></Field>
|
||||
<Field label={t('floor.itemHeight')}><Input size="sm" type="number" min={4} value={thing.height} onChange={e => setSize(undefined, Number(e.target.value))} /></Field>
|
||||
</div>
|
||||
<div className="flex items-center gap-1.5">
|
||||
<span className="text-label text-ink-2 mr-auto">{t('floor.rotation', { deg: Math.round(thing.rotation || 0) })}</span>
|
||||
<IconButton size="sm" variant="secondary" label={t('floor.rotateLeft')} onClick={() => onChange({ rotation: rotate(thing.rotation || 0, -15) })}><RotateCcw size={16} /></IconButton>
|
||||
<IconButton size="sm" variant="secondary" label={t('floor.rotateRight')} onClick={() => onChange({ rotation: rotate(thing.rotation || 0, 15) })} data-testid="floor-rotate"><RotateCw size={16} /></IconButton>
|
||||
<Button size="sm" variant="secondary" onClick={() => onChange({ rotation: rotate(thing.rotation || 0, 90) })}>90°</Button>
|
||||
</div>
|
||||
<Button size="sm" variant="ghost" icon={<Trash2 size={16} />} onClick={onRemove} className="justify-self-start text-danger-ink" data-testid="floor-remove">
|
||||
{isTable ? t('floor.takeOff') : t('floor.removeThing')}
|
||||
</Button>
|
||||
</Panel>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user