import { useQuery } from '@tanstack/react-query' import AppShell from '../components/AppShell' import client from '../api/client' import { useFormat, useTranslation } from '@xenia/i18n' import { money as fmtEuro, tw } from '../i18n' import { getVenueSettings } from '../api/venueSettings' // ── helpers ─────────────────────────────────────────────────────────────────── function fmtDuration(seconds) { if (seconds == null || seconds < 0) return '—' const h = Math.floor(seconds / 3600) const m = Math.floor((seconds % 3600) / 60) if (h === 0) return tw('common.minutesShort', { m }) if (m === 0) return tw('common.hoursShort', { h }) return tw('common.hoursMinutesShort', { h, m }) } function sinceSeconds(isoStart) { if (!isoStart) return 0 return Math.floor((Date.now() - new Date(isoStart).getTime()) / 1000) } function breakDurationSeconds(b) { if (!b.started_at) return 0 const end = b.ended_at ? new Date(b.ended_at) : new Date() return Math.floor((end - new Date(b.started_at)) / 1000) } // ── stat card ───────────────────────────────────────────────────────────────── function StatCard({ label, value, sub, accent }) { return (
{label}
{value}
{sub &&
{sub}
}
) } // ── horizontal bar chart (per-hour) ────────────────────────────────────────── function PerHourChart({ title, data, accent = 'var(--accent)' }) { const hours = Object.keys(data).map(Number).sort((a, b) => a - b) if (hours.length === 0) return null const maxValue = Math.max(...hours.map(h => data[h]), 1) return (
{title}
{hours.map(h => { const pct = (data[h] / maxValue) * 100 return (
{String(h).padStart(2, '0')}:00
0 ? 2 : 0)}%`, background: accent, transition: 'width 300ms ease', display: 'flex', alignItems: 'center', }}> {pct > 18 && ( {data[h]} )}
{pct <= 18 ? data[h] : ''}
) })}
) } // ── breaks timeline ─────────────────────────────────────────────────────────── function BreakRow({ brk, index }) { const { t } = useTranslation('waiter') const { formatTime: fmtTime } = useFormat() const dur = breakDurationSeconds(brk) const active = !brk.ended_at return (
{index + 1}
{fmtTime(brk.started_at)} – {active ? t('shiftPage.now') : fmtTime(brk.ended_at)}
{fmtDuration(dur)}
{active && ( {t('venues.active')} )}
) } // ── main page ───────────────────────────────────────────────────────────────── export default function ShiftOverviewPage() { const { t } = useTranslation('waiter') const { formatTime: fmtTime } = useFormat() const { data: shift, isLoading: shiftLoading } = useQuery({ queryKey: ['my-shift'], queryFn: () => client.get('/api/shifts/my').then(r => r.data), refetchInterval: 30_000, }) const { data: settings } = useQuery({ queryKey: ['pos-settings'], queryFn: () => getVenueSettings().then(r => r.data), staleTime: 60_000, }) const hideRevenue = settings?.['shifts.hide_revenue_from_waiters']?.value === 'true' // /api/orders/my returns only open orders belonging to this waiter — // enough for per-hour chart of current open tables const { data: openOrders = [] } = useQuery({ queryKey: ['my-orders-shift'], queryFn: () => client.get('/api/orders/my').then(r => r.data), refetchInterval: 30_000, enabled: !!shift, }) if (shiftLoading) { return (
{t('common.loading')}
) } if (!shift) { return (
⏸
{t('shiftPage.none')}
{t('shiftPage.noneSub')}
) } // ── Compute stats ──────────────────────────────────────────────────────────── const totalShiftSec = sinceSeconds(shift.started_at) const totalBreakSec = (shift.breaks || []).reduce((s, b) => s + breakDurationSeconds(b), 0) const workedSec = Math.max(0, totalShiftSec - totalBreakSec) // Per-hour buckets from currently-open orders const ordersByHour = {} const itemsByHour = {} openOrders.forEach(o => { const h = new Date(o.opened_at).getHours() ordersByHour[h] = (ordersByHour[h] || 0) + 1 itemsByHour[h] = (itemsByHour[h] || 0) + (o.items || []).filter(i => i.status !== 'cancelled').reduce((s, i) => s + i.quantity, 0) }) const totalItems = openOrders.reduce((s, o) => s + (o.items || []).filter(i => i.status !== 'cancelled').reduce((is, i) => is + i.quantity, 0), 0) const activeBreak = (shift.breaks || []).find(b => !b.ended_at) return (
{/* Active break banner */} {activeBreak && (
☕
{t('shiftPage.onBreak')}
{t('shiftPage.breakSince', { time: fmtTime(activeBreak.started_at), duration: fmtDuration(breakDurationSeconds(activeBreak)) })}
)} {/* Hero stats grid */}
{!hideRevenue && ( )} {!hideRevenue && ( )}
{/* Per-hour charts */} {openOrders.length > 0 && (
{t('shiftPage.perHour')}
)} {/* Breaks */} {(shift.breaks || []).length > 0 && (
{t('shiftPage.breaksCount', { count: shift.breaks.length })}
{shift.breaks.map((b, i) => )}
)}
) }