import { useEffect, useState } from 'react'
import { useNavigate, useLocation } from 'react-router-dom'
import { useCan, useModule } from '@xenia/access'
import { useFormat, useTranslation } from '@xenia/i18n'
import UserMenu from './UserMenu'
import { useNotifications } from '../context/NotificationContext'
import useNotificationSettingsStore from '../store/notificationSettingsStore'
import useTableViewStore from '../store/tableViewStore'
import useChatStore from '../store/chatStore'
import useKdsReadyStore from '../store/kdsReadyStore'
import useInstallPromptStore, { apkAvailable } from '../store/installPromptStore'
import useOfflineQueueCount from '../hooks/useOfflineQueueCount'
import usePrintProblems from '../hooks/usePrintProblems'
import PrintProblemsSheet from './PrintProblemsSheet'
import { fade } from '../logic/colour'
// ─── Nav item registry ────────────────────────────────────────────────────────
const NAV_REGISTRY = {
tables: {
key: 'tables',
label: 'nav.tables',
shortLabel: 'nav.tables',
href: '/tables?tab=tables',
match: (loc) => loc.pathname === '/tables' && loc.search !== '?tab=orders',
icon: (
),
},
orders: {
key: 'orders',
label: 'nav.activeOrders',
shortLabel: 'nav.orders',
href: '/tables?tab=orders',
match: (loc) => loc.pathname === '/tables' && loc.search === '?tab=orders',
icon: (
),
},
messages: {
key: 'messages',
label: 'nav.messages',
shortLabel: 'nav.messages',
href: '/messages',
match: (loc) => loc.pathname === '/messages',
icon: (
),
},
standalone: {
key: 'standalone',
label: 'nav.takeaway',
shortLabel: 'nav.takeaway',
href: '/tables',
match: () => false,
icon: (
),
},
'shift-overview': {
key: 'shift-overview',
label: 'nav.shift',
shortLabel: 'nav.shift',
href: '/shift-overview',
match: (loc) => loc.pathname === '/shift-overview',
icon: (
),
},
'order-log': {
key: 'order-log',
label: 'nav.log',
shortLabel: 'nav.log',
href: '/order-log',
match: (loc) => loc.pathname === '/order-log',
icon: (
),
},
}
/** Staff chat shows only when the venue has it on and the person holds chat.use (plan 4.3). */
function useChatOn() {
const on = useModule('chat')
const can = useCan('chat.use')
return on && can
}
const shown = chatOn => item => item.key !== 'messages' || chatOn
const DRAWER_ITEMS = [
{ ...NAV_REGISTRY.tables },
{ ...NAV_REGISTRY.orders },
{ ...NAV_REGISTRY.messages },
]
// ─── Cache badge (cloud upload) ───────────────────────────────────────────────
function CacheButton({ count }) {
if (!count) return null
return (
{/* Cloud upload icon */}
{count > 9 ? '9+' : count}
)
}
// ─── Failed print badge ───────────────────────────────────────────────────────
function FailedPrintButton({ count, onClick }) {
if (!count) return null
return (
)
}
// ─── Bell button ──────────────────────────────────────────────────────────────
function BellButton({ onClick, count }) {
return (
)
}
// ─── Notification drawer ──────────────────────────────────────────────────────
function NotificationDrawer({ messages, onClose }) {
const { t } = useTranslation('waiter')
const navigate = useNavigate()
const { formatTime } = useFormat()
return (
e.stopPropagation()} style={{ maxHeight: '80svh' }}>
{t('shell.notifications')}
{messages.length === 0 && (
{t('shell.noNotifications')}
)}
{messages.map(msg => {
const tableIds = (() => { try { return JSON.parse(msg.table_ids || '[]') } catch { return [] } })()
return (
{ onClose(); navigate(`/tables/${tableIds[0]}`) } : undefined}
style={{
padding: '12px 4px', borderBottom: '1px solid var(--border)',
display: 'flex', gap: 12, alignItems: 'flex-start',
cursor: msg.message_type === 'qr_order' && tableIds.length ? 'pointer' : undefined,
}}>
📢
{msg.sender_name && (
{msg.sender_name}
)}
{msg.body}
{tableIds.length > 0 && (
{t('shell.notificationTables', { tables: tableIds.join(', ') })}
)}
{formatTime(msg.created_at)}
)
})}
)
}
// ─── Side drawer ──────────────────────────────────────────────────────────────
const DRAWER_NAV_ITEMS = [
{ ...NAV_REGISTRY.tables },
{
key: 'standalone',
label: 'nav.takeawayDelivery',
href: '/tables',
match: () => false, // active state handled separately in SideDrawer
icon: (
),
},
{ ...NAV_REGISTRY.orders },
{ ...NAV_REGISTRY.messages },
{
// table bookings (4.8): for whoever handles online orders or manages reservations
key: 'bookings',
label: 'nav.bookings',
href: '/bookings',
match: (loc) => loc.pathname === '/bookings',
icon: (
),
},
{
key: 'shift-overview',
label: 'nav.shiftSummary',
href: '/shift-overview',
match: (loc) => loc.pathname === '/shift-overview',
icon: (
),
},
{
key: 'order-log',
label: 'nav.orderLog',
href: '/order-log',
match: (loc) => loc.pathname === '/order-log',
icon: (
),
},
]
const DRAWER_SETTINGS_ITEM = {
key: 'settings',
label: 'nav.settings',
href: '/settings',
match: (loc) => loc.pathname.startsWith('/settings'),
icon: (
),
}
const DRAWER_INSTALL_ITEM = {
key: 'install-app',
label: 'nav.installApp',
match: () => false,
icon: (
),
}
// Browser on Android only, and only if this venue's server serves the APK
// (the native app never shows it) — opens the install dialog.
function InstallAppDrawerButton({ location, onClose }) {
const [available, setAvailable] = useState(false)
const show = useInstallPromptStore(s => s.show)
useEffect(() => {
apkAvailable().then(setAvailable)
}, [])
if (!available) return null
return (
{ onClose(); show() }}
/>
)
}
function DrawerButton({ item, location, onNavigate, active: activeProp, onClick }) {
const { t } = useTranslation('waiter')
const active = activeProp ?? item.match(location)
return (
)
}
function SideDrawer({ open, onClose }) {
const { t } = useTranslation('waiter')
const navigate = useNavigate()
const chatOn = useChatOn()
const bookingsModule = useModule('connect_orders')
const bookingsOnline = useCan('connect_orders.manage')
const bookingsManage = useCan('reservations.manage')
const bookingsOn = bookingsModule && (bookingsOnline || bookingsManage)
const location = useLocation()
const { activeZoneTab, setActiveZoneTab } = useTableViewStore()
function go(href) {
if (href.startsWith('/tables')) setActiveZoneTab('all')
onClose()
navigate(href, { replace: true })
}
function goStandalone() {
setActiveZoneTab('standalone')
onClose()
navigate('/tables', { replace: true })
}
return (
<>
{/* Overlay */}
{/* Drawer panel */}
{/* Drawer header */}
Xenia
{t('shell.posSystem')}
{/* Main nav items */}
{t('shell.navigation')}
{DRAWER_NAV_ITEMS.filter(shown(chatOn)).filter(item => item.key !== 'bookings' || bookingsOn).map(item => {
const isStandaloneActive = location.pathname === '/tables' && activeZoneTab === 'standalone'
const active = item.key === 'standalone'
? isStandaloneActive
: item.key === 'tables'
? item.match(location) && !isStandaloneActive
: undefined
return (
)
})}
{/* Install app + settings at bottom */}
>
)
}
// ─── Triple pill footer ───────────────────────────────────────────────────────
function PillBadge({ count, color = 'var(--x-danger)' }) {
if (!count) return null
return (
{count > 9 ? '9+' : count}
)
}
function TripleFooter({ location, navigate, chatUnread, kdsReady, tripleItems }) {
const { t } = useTranslation('waiter')
const { activeZoneTab, setActiveZoneTab } = useTableViewStore()
const chatOn = useChatOn()
const items = (tripleItems || ['tables', 'orders', 'messages']).map(k => NAV_REGISTRY[k]).filter(Boolean).filter(shown(chatOn))
function handleNavClick(item) {
if (item.key === 'standalone') { setActiveZoneTab('standalone'); navigate('/tables', { replace: true }); return }
if (item.key === 'tables' || item.key === 'orders') setActiveZoneTab('all')
navigate(item.href, { replace: true })
}
return (
{items.map(item => {
const isStandaloneActive = location.pathname === '/tables' && activeZoneTab === 'standalone'
const active = item.key === 'standalone'
? isStandaloneActive
: item.key === 'tables'
? item.match(location) && !isStandaloneActive
: item.match(location)
const badge = item.key === 'messages' && chatUnread > 0
? { count: chatUnread, color: 'var(--x-danger)' }
: item.key === 'orders' && kdsReady > 0
? { count: kdsReady, color: 'var(--x-success)' }
: null
return (
)
})}
)
}
// ─── Icon navbar footer ───────────────────────────────────────────────────────
function IconNavbar({ navItems, location, navigate, chatUnread, kdsReady }) {
const { t } = useTranslation('waiter')
const { activeZoneTab, setActiveZoneTab } = useTableViewStore()
const chatOn = useChatOn()
const items = navItems
.map(key => NAV_REGISTRY[key])
.filter(Boolean)
.filter(shown(chatOn))
function handleNavClick(item) {
if (item.key === 'standalone') { setActiveZoneTab('standalone'); navigate('/tables', { replace: true }); return }
if (item.key === 'tables' || item.key === 'orders') setActiveZoneTab('all')
navigate(item.href, { replace: true })
}
return (
{items.map(item => {
const isStandaloneActive = location.pathname === '/tables' && activeZoneTab === 'standalone'
const active = item.key === 'standalone'
? isStandaloneActive
: item.key === 'tables'
? item.match(location) && !isStandaloneActive
: item.match(location)
const badge = item.key === 'messages' && chatUnread > 0
? { count: chatUnread, color: 'var(--x-danger)' }
: item.key === 'orders' && kdsReady > 0
? { count: kdsReady, color: 'var(--x-success)' }
: null
return (
)
})}
)
}
// ─── AppShell ─────────────────────────────────────────────────────────────────
export default function AppShell({ children, title, rightActions }) {
const navigate = useNavigate()
const location = useLocation()
const [drawerOpen, setDrawerOpen] = useState(false)
const [showNotifs, setShowNotifs] = useState(false)
const { unreadCount, recentMessages, fetchRecent, ackAll } = useNotifications() || {}
const { popupMode } = useNotificationSettingsStore()
const { navStyle, navItems, tripleItems } = useTableViewStore()
const { totalUnread: chatUnread } = useChatStore()
const { readyCount } = useKdsReadyStore()
const offlineCacheCount = useOfflineQueueCount()
const prints = usePrintProblems() // 6.10: tickets that didn't print, live
const [showPrints, setShowPrints] = useState(false)
function openNotifs() {
setShowNotifs(true)
fetchRecent?.()
if (popupMode === 'never') ackAll?.()
}
return (
{/* ── Header ────────────────────────────────────────────── */}
{/* Hamburger menu */}
{/* Title / right-actions area */}
{title && (
{title}
)}
{!title && }
{/* Custom right actions (per-page) */}
{rightActions}
{/* Offline cache badge — visible only when ops are pending */}
{/* Tickets that didn't print (6.10): opens them with what can be done */}
setShowPrints(true)} />
{/* Bell */}
{/* Username chip */}
{showPrints &&
setShowPrints(false)} />}
{/* ── Side drawer ───────────────────────────────────────── */}
setDrawerOpen(false)} />
{/* ── Page content ──────────────────────────────────────── */}
{children}
{/* ── Footer nav ────────────────────────────────────────── */}
{navStyle === 'icon_navbar' ? (
) : (
)}
{/* ── Notification drawer ───────────────────────────────── */}
{showNotifs && (
setShowNotifs(false)}
/>
)}
)
}