feat(manager): the live map fits the width or the whole plan and remembers the view
FloorView: minus · Fit width · Whole map · plus; the zoom steps from the scale on screen; the plan, zoom and scroll position are kept per plan on the device (localStorage) and restored. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -3,12 +3,31 @@
|
||||
// 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 { useEffect, useLayoutEffect, useMemo, useRef, 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]
|
||||
const ZOOMS = [0.25, 0.35, 0.5, 0.75, 1, 1.25, 1.5, 2, 3]
|
||||
|
||||
/** The view the person left (zoom + scroll, per plan), kept on this device (2026-11-20). */
|
||||
const store = {
|
||||
read(key) { try { return JSON.parse(localStorage.getItem(key)) ?? null } catch { return null } },
|
||||
write(key, v) { try { localStorage.setItem(key, JSON.stringify(v)) } catch { /* private mode */ } },
|
||||
}
|
||||
|
||||
/** The size of an element, kept up to date. */
|
||||
function useSize(ref) {
|
||||
const [size, setSize] = useState({ width: 0, height: 0 })
|
||||
useLayoutEffect(() => {
|
||||
const el = ref.current
|
||||
if (!el) return undefined
|
||||
const ro = new ResizeObserver(([e]) => setSize({ width: e.contentRect.width, height: e.contentRect.height }))
|
||||
ro.observe(el)
|
||||
return () => ro.disconnect()
|
||||
})
|
||||
return size
|
||||
}
|
||||
|
||||
function useMinuteClock() {
|
||||
const [now, setNow] = useState(() => Date.now())
|
||||
@@ -26,15 +45,40 @@ const btn = active => ({
|
||||
})
|
||||
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%' }) {
|
||||
export function FloorView({ data, zone = null, onPick, mediaBase = '', height = '100%', storageKey = 'xenia.floorView' }) {
|
||||
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 [chosen, setChosen] = useState(() => store.read(storageKey)?.plan ?? null)
|
||||
const plan = plans.find(p => p.id === chosen) ?? plans[0] ?? null
|
||||
const viewKey = plan ? `${storageKey}:${plan.id}` : null
|
||||
// 'width' = fit the width (scrolls down), 'all' = the whole plan on screen, a number = a zoom
|
||||
const [zoom, setZoomState] = useState(() => (viewKey && store.read(viewKey)?.zoom) || 'width')
|
||||
const box = useRef(null)
|
||||
const size = useSize(box)
|
||||
const { placed, unplaced } = useMemo(() => splitTables(data?.tables ?? [], plan?.id ?? -1, zone), [data, plan, zone])
|
||||
|
||||
// another plan: its own saved view
|
||||
const shownKey = useRef(viewKey)
|
||||
useEffect(() => {
|
||||
if (shownKey.current === viewKey) return
|
||||
shownKey.current = viewKey
|
||||
setZoomState((viewKey && store.read(viewKey)?.zoom) || 'width')
|
||||
}, [viewKey])
|
||||
// back where the person left it (once the plan is drawn)
|
||||
const restored = useRef(null)
|
||||
useLayoutEffect(() => {
|
||||
const el = box.current
|
||||
if (!el || !viewKey || restored.current === viewKey || !size.width) return
|
||||
restored.current = viewKey
|
||||
const saved = store.read(viewKey)
|
||||
if (saved) { el.scrollLeft = saved.left ?? 0; el.scrollTop = saved.top ?? 0 }
|
||||
})
|
||||
const save = patch => { if (viewKey) store.write(viewKey, { ...(store.read(viewKey) ?? {}), ...patch }) }
|
||||
const setZoom = z => { setZoomState(z); save({ zoom: z }) }
|
||||
const choose = id => { setChosen(id); store.write(storageKey, { plan: id }) }
|
||||
const onScroll = e => save({ left: Math.round(e.currentTarget.scrollLeft), top: Math.round(e.currentTarget.scrollTop) })
|
||||
|
||||
if (!data) return null
|
||||
if (!plan) {
|
||||
return (
|
||||
@@ -44,25 +88,30 @@ export function FloorView({ data, zone = null, onPick, mediaBase = '', height =
|
||||
</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))]
|
||||
})
|
||||
// the scale on screen now: a fit is worked out from the space there is
|
||||
const fitWidth = size.width ? size.width / plan.width : 1
|
||||
const fitAll = size.width && size.height ? Math.min(size.width / plan.width, size.height / plan.height) : fitWidth
|
||||
const scale = zoom === 'width' ? fitWidth : zoom === 'all' ? fitAll : zoom
|
||||
const step = d => {
|
||||
const next = d > 0 ? ZOOMS.find(v => v > scale + 0.01) : [...ZOOMS].reverse().find(v => v < scale - 0.01)
|
||||
setZoom(next ?? (d > 0 ? ZOOMS[ZOOMS.length - 1] : ZOOMS[0]))
|
||||
}
|
||||
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>
|
||||
<button key={p.id} type="button" style={btn(p.id === plan.id)} onClick={() => choose(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={{ ...btn(zoom === 'width'), height: 36 }} aria-pressed={zoom === 'width'} onClick={() => setZoom('width')} data-testid="floor-fit-width">{t('floorMap.fit')}</button>
|
||||
<button type="button" style={{ ...btn(zoom === 'all'), height: 36 }} aria-pressed={zoom === 'all'} onClick={() => setZoom('all')} data-testid="floor-fit-all">{t('floorMap.fitAll')}</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)' }}>
|
||||
<div ref={box} onScroll={onScroll} data-testid="floor-scroll"
|
||||
style={{ flex: 1, minHeight: 0, overflow: zoom === 'all' ? 'hidden' : 'auto', scrollbarGutter: 'stable', 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' }}>
|
||||
style={{ display: 'block', margin: zoom === 'all' ? '0 auto' : undefined, width: `${Math.max(1, plan.width * scale)}px`, height: `${Math.max(1, plan.height * scale)}px`, 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 => (
|
||||
|
||||
Reference in New Issue
Block a user