// The counter POS screen (plan steps 3.3a–e). Up to four modes, switched at the top left: // - Sale: categories on the left, the product grid with a search, the quick-sale cart on the // right (SaleCart: Charge → payment, 3.3b). // - Tables: the zones on the left and the table list (TablesView); a table opens with the // product grid again and the table's order on the right (TablePanel). // - Online (when online orders are on and the cashier handles them): new / in progress / // finished on the left, the orders (OnlineView) and the picked one (OnlinePanel). A new // order rings (and again every 30 s while one waits for an answer) and shows a notice in any // mode; the mode's badge counts the orders waiting. // - Receipts: today's paid orders (the cashier's shift or everyone's) and the picked one // (ReceiptPanel) to give money back for some or all of its items (3.3e). // A tap adds a product (or opens its choices when it has preferences to pick); the (i) button // checks its price without adding it. Every price is the backend's. import { useEffect, useMemo, useState } from 'react' import { Globe, Info, LayoutGrid, Receipt, Search, ShoppingBag, X } from 'lucide-react' import { useFormat, useLanguage, useTranslation } from '@xenia/i18n' import { useCan, useModule } from '@xenia/access' import { EmptyState, IconButton, Input, Spinner, useToast } from '@xenia/ui' import { useLiveEvent } from '../../device/context' import { playChime } from './chime' import { addLine, categoriesWithProducts, defaultSelection, needsChoices, productsIn, searchProducts, toLine, unitOf } from './logic/cart' import { findOnline, ONLINE_LISTS } from './logic/online' import { zoneCounts } from './logic/tables' import OnlinePanel from './OnlinePanel' import OnlineView from './OnlineView' import ProductDialog from './ProductDialog' import ReceiptPanel from './ReceiptPanel' import ReceiptsView from './ReceiptsView' import SaleCart from './SaleCart' import TablePanel from './TablePanel' import TablesView from './TablesView' import { useMenu, useOnline, useReceipts, useTables } from './usePos' const REMIND_MS = 30_000 export default function PosScreen() { const { t } = useTranslation('terminal') const { menu } = useMenu() if (!menu) return
return } function Counter({ menu, t }) { const { language } = useLanguage() const { formatMoney } = useFormat() const [mode, setMode] = useState('sale') // sale | tables | online | receipts const [tableId, setTableId] = useState(null) // the open table (tables mode) const [zone, setZone] = useState(null) const [category, setCategory] = useState(() => menu.settings.start_category_id ?? null) const [query, setQuery] = useState('') const [orderType, setOrderType] = useState(menu.settings.default_order_type) const [saleCart, setSaleCart] = useState([]) const [tableCart, setTableCart] = useState([]) const [dialog, setDialog] = useState(null) // product const tables = useTables() const canDiscount = useCan('ordering.prices.modify') const toast = useToast() const onlineModule = useModule('connect_orders') const handlesOnline = useCan('connect_orders.manage') const onlineOn = onlineModule && handlesOnline const online = useOnline(onlineOn) const [onlineList, setOnlineList] = useState('incoming') const [onlineId, setOnlineId] = useState(null) const [now, setNow] = useState(() => Date.now()) const [chimes, setChimes] = useState(0) const waiting = online.data?.incoming.length ?? 0 const [receiptScope, setReceiptScope] = useState('mine') const [receiptId, setReceiptId] = useState(null) const receipts = useReceipts(receiptScope, mode === 'receipts') const ring = () => { playChime(); setChimes(c => c + 1) } useLiveEvent('online_order_received', data => { if (!onlineOn) return ring() if (mode !== 'online') toast.info(t('pos.online.arrived', { count: data?.count ?? 1 })) }) // a reminder while an order waits for an answer; the waiting minutes move on too useEffect(() => { const id = setInterval(() => { setNow(Date.now()) if (waiting > 0) { playChime(); setChimes(c => c + 1) } }, REMIND_MS) return () => clearInterval(id) }, [waiting]) // a table that left the cashier's list (zone changed) shows the list again const table = tableId != null ? tables.data?.tables.find(x => x.id === tableId) ?? null : null const atTable = mode === 'tables' && table != null const picking = mode === 'tables' && !atTable const onlineOrder = findOnline(online.data, onlineId) const setCart = atTable ? setTableCart : setSaleCart const leaveTable = () => { setTableId(null); setTableCart([]) } const byId = useMemo(() => new Map(menu.products.map(p => [p.id, p])), [menu.products]) const categories = useMemo(() => categoriesWithProducts(menu.categories, menu.products), [menu]) const shown = useMemo(() => (query.trim() ? searchProducts(menu.products, query) : productsIn(menu.products, category)), [menu.products, query, category]) const counts = useMemo(() => zoneCounts(tables.data?.tables ?? []), [tables.data]) const tap = product => { if (needsChoices(product)) { setDialog(product); return } const unit = unitOf(product) setCart(c => addLine(c, toLine(product, defaultSelection(product), unit ? unit.start : 1))) } return (
{mode === 'receipts' ? ( <>
) : mode === 'online' ? ( <>
{ online.reload() if (move?.list) setOnlineList(move.list) if (move?.clear) setOnlineId(null) if (result?.change > 0) toast.success(t('pos.done.toastChange', { change: formatMoney(result.change) })) }} /> ) : picking ? (
{ setTableId(id); setTableCart([]) }} />
) : ( <>
setQuery(e.target.value)} placeholder={t('pos.search')} className="pl-10" data-testid="pos-search" aria-label={t('pos.search')} />
{query && setQuery('')}>}
{shown.length === 0 ? ( ) : (
{shown.map(p => tap(p)} onInfo={() => setDialog(p)} />)}
)}
{atTable ? : } )} {dialog && ( setDialog(null)} onAdd={line => setCart(c => addLine(c, line))} /> )}
) } function ModeButton({ active, onClick, icon: Icon, badge, alert, children, testId }) { return ( ) } function NavButton({ active, colour, onClick, children, testId, count, tally }) { return ( ) } function ProductCard({ product, onTap, onInfo }) { const { t } = useTranslation('terminal') const { formatMoney } = useFormat() const unit = unitOf(product) return (
) }