import { useQuery } from '@tanstack/react-query' import AppShell from '../components/AppShell' import client from '../api/client' import { getVenueSettings } from '../api/venueSettings' // ── helpers ─────────────────────────────────────────────────────────────────── function fmtTime(iso) { if (!iso) return '—' return new Date(iso).toLocaleTimeString('el-GR', { hour: '2-digit', minute: '2-digit' }) } 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 `${m}λ` if (m === 0) return `${h}ω` return `${h}ω ${m}λ` } function fmtEuro(n) { return '€' + parseFloat(n || 0).toFixed(2) } 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 dur = breakDurationSeconds(brk) const active = !brk.ended_at return (
{index + 1}
{fmtTime(brk.started_at)} – {active ? 'Τώρα' : fmtTime(brk.ended_at)}
{fmtDuration(dur)}
{active && ( ΕΝΕΡΓΟ )}
) } // ── main page ───────────────────────────────────────────────────────────────── export default function ShiftOverviewPage() { 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 (
Φόρτωση…
) } if (!shift) { return (
⏸
Δεν υπάρχει ενεργή βάρδια
Ξεκινήστε βάρδια για να δείτε στατιστικά.
) } // ── 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 && (
☕
Σε διάλειμμα
από {fmtTime(activeBreak.started_at)} · {fmtDuration(breakDurationSeconds(activeBreak))} μέχρι τώρα
)} {/* Hero stats grid */}
{!hideRevenue && ( )} {!hideRevenue && ( )}
{/* Per-hour charts */} {openOrders.length > 0 && (
Ανά Ώρα
)} {/* Breaks */} {(shift.breaks || []).length > 0 && (
Διαλείμματα ({shift.breaks.length})
{shift.breaks.map((b, i) => )}
)}
) }