// The tables now, in the manager (plan step 6.9; the map since 6.8): each table's state, total, // time open, waiters, flags and a booking kept for it soon (RESERVED). A table with an order // opens its order page; a free one opens a new order there. Quick actions set the table's flags // and can tell its waiters (or everyone) a message. Tickets that didn't print are listed below // with what can be done about them (6.10). // Everything stays current through the live events. // 2026-11-20: the toolbar is the shared one (view, state chips, zones as a multi-select), a click on // the card opens the table (no separate button), and the tables' QR codes are a view here (?view=qr; // was the page /online/qr) for whoever manages the QR menu. import { useState } from 'react' import { useNavigate, useSearchParams } from 'react-router-dom' import { Clock, LayoutGrid, Lock, Map as MapIcon, Printer, QrCode, Zap } from 'lucide-react' import { useCan, useModule } from '@xenia/access' import { useFormat, useTranslation } from '@xenia/i18n' import { Dropdown, EmptyState, FilterChip, IconButton, Page, Segmented, Spinner, Toolbar, ToolbarSpacer, cx } from '@xenia/ui' import client from '../../api/client' import { useLiveQuery } from '../../live/useLiveQuery' import LiveFloor from '../floor/LiveFloor' import { useDuration } from '../overview/hooks' import { TABLE_STATES, durationParts, orderTotal, tableCards, tableName } from '../overview/overviewLogic' import { Money, Pill } from '../overview/parts' import PrintProblemsCard from './PrintProblemsCard' import TableQrCodes from '../online/TableQrCodes' import { BORDER, SOFT, SOLID } from '../overview/tones' import QuickActions from './QuickActions' import { NO_ZONE, cardTarget, filterCards, flagsByTable, personName } from './orderLogic' const ORDER_EVENTS = ['order_updated', 'order_paid', 'order_closed', 'order_refunded'] const STATE_TONE = { open: 'success', partially_paid: 'info', paid: 'accent', free: 'neutral' } export default function TablesPage() { const { t } = useTranslation('manager') const tc = useTranslation('common').t const navigate = useNavigate() const qrModule = useModule('connect_menu') const qrAllowed = useCan('connect_menu.manage') const qrOn = qrModule && qrAllowed const [params, setParams] = useSearchParams() const views = ['list', 'map', ...(qrOn ? ['qr'] : [])] const view = views.includes(params.get('view')) ? params.get('view') : 'list' const setView = v => setParams(p => { const n = new URLSearchParams(p); n.set('view', v); return n }, { replace: true }) const [states, setStates] = useState([]) const [zones, setZones] = useState([]) const [quick, setQuick] = useState(null) // a card const { data: tables = [], isPending: loadingTables } = useLiveQuery({ queryKey: ['tables'], queryFn: () => client.get('/api/tables/').then(r => r.data), events: ['table_list_changed', 'reservation_updated'], refetchInterval: 60_000, }) const { data: orders = [], isPending: loadingOrders } = useLiveQuery({ queryKey: ['orders-active'], queryFn: () => client.get('/api/orders/').then(r => r.data), events: ORDER_EVENTS, refetchInterval: 60_000, }) const { data: groups = [] } = useLiveQuery({ queryKey: ['table-groups'], queryFn: () => client.get('/api/tables/groups').then(r => r.data), events: ['table_list_changed'] }) const { data: flagDefs = [] } = useLiveQuery({ queryKey: ['flag-defs'], queryFn: () => client.get('/api/flags/defs').then(r => r.data), staleTime: 60_000 }) const { data: flagRows = [] } = useLiveQuery({ queryKey: ['flag-assignments'], queryFn: () => client.get('/api/flags/assignments').then(r => r.data), events: ['table_flags_changed'] }) const { data: staff = [] } = useLiveQuery({ queryKey: ['waiters'], queryFn: () => client.get('/api/waiters/').then(r => r.data), staleTime: 60_000 }) const cards = tableCards(tables, orders) const shown = filterCards(cards, { states, zones }) const flags = flagsByTable(flagDefs, flagRows) const name = id => personName(staff.find(s => s.id === id)) || `#${id}` const toggleState = s => setStates(list => (list.includes(s) ? list.filter(x => x !== s) : [...list, s])) if (loadingTables || loadingOrders) return