Files
xenia-pos-local/waiter_pwa/src/pages/ShiftOverviewPage.jsx
T
bonaminandClaude Opus 5.5 beb53c65bb feat(waiter): the look on the shared tokens; the venue's colour (platform-v2 step 4.6)
- The waiter's --bg/--text/--accent… are made of @xenia/ui tokens: light / dark (user
  menu) and the venue's accent reach every screen; the 751 colour literals in
  waiter_pwa/src became tokens (by hue family and lightness), hex-alpha suffixes became
  fade() (color-mix), fixing 'var(--accent)18' tints that never showed.
- @xenia/ui: new `special` (violet: offers, discounts, deals; contrast-tested) and
  `on-colour` tokens.
- Setting venue.theme (teal | amber | blue | violet | rose, default amber), bootstrap
  venue.theme, manager Settings → Venue → Colour; the waiter sets data-venue-theme and
  the theme before the first render.
- Guard tests/no-colours.test.mjs; tests/e2e/waiter_look.mjs (every page light + dark,
  side-by-side compare page); waiter e2e +3; backend test_venue_theme.py.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 09:08:39 +02:00

272 lines
12 KiB
React
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 (
<div style={{
background: 'var(--bg2)', border: '1px solid var(--border)',
borderRadius: 16, padding: '16px 18px',
display: 'flex', flexDirection: 'column', gap: 4,
}}>
<div style={{ fontSize: 11, fontWeight: 700, color: 'var(--muted)', textTransform: 'uppercase', letterSpacing: 0.7 }}>{label}</div>
<div style={{
fontSize: 28, fontWeight: 700, lineHeight: 1.1,
color: accent || 'var(--text)',
fontFamily: "'ui-monospace','SFMono-Regular',monospace",
}}>{value}</div>
{sub && <div style={{ fontSize: 12, color: 'var(--muted)' }}>{sub}</div>}
</div>
)
}
// ── 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 (
<div style={{ marginBottom: 20 }}>
<div style={{ fontSize: 12, fontWeight: 700, color: 'var(--muted)', textTransform: 'uppercase', letterSpacing: 0.5, marginBottom: 10 }}>{title}</div>
<div style={{ display: 'flex', flexDirection: 'column', gap: 6 }}>
{hours.map(h => {
const pct = (data[h] / maxValue) * 100
return (
<div key={h} style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
<div style={{ width: 32, fontSize: 11, fontWeight: 700, color: 'var(--muted)', textAlign: 'right', flexShrink: 0 }}>
{String(h).padStart(2, '0')}:00
</div>
<div style={{ flex: 1, height: 20, background: 'var(--bg3)', borderRadius: 4, overflow: 'hidden' }}>
<div style={{
height: '100%', borderRadius: 4,
width: `${Math.max(pct, data[h] > 0 ? 2 : 0)}%`,
background: accent,
transition: 'width 300ms ease',
display: 'flex', alignItems: 'center',
}}>
{pct > 18 && (
<span style={{ fontSize: 10, fontWeight: 800, color: 'var(--accent-fg)', paddingLeft: 6 }}>
{data[h]}
</span>
)}
</div>
</div>
<div style={{ width: 20, fontSize: 11, fontWeight: 700, color: 'var(--text)', flexShrink: 0 }}>
{pct <= 18 ? data[h] : ''}
</div>
</div>
)
})}
</div>
</div>
)
}
// ── breaks timeline ───────────────────────────────────────────────────────────
function BreakRow({ brk, index }) {
const { t } = useTranslation('waiter')
const { formatTime: fmtTime } = useFormat()
const dur = breakDurationSeconds(brk)
const active = !brk.ended_at
return (
<div style={{
display: 'flex', alignItems: 'center', gap: 12,
padding: '10px 14px', borderRadius: 10,
background: active ? 'rgb(var(--x-warning-rgb) / 0.1)' : 'var(--bg2)',
border: `1px solid ${active ? 'var(--x-warning)' : 'var(--border)'}`,
}}>
<div style={{
width: 28, height: 28, borderRadius: 8, flexShrink: 0,
background: active ? 'var(--x-warning)' : 'var(--bg3)',
display: 'flex', alignItems: 'center', justifyContent: 'center',
fontSize: 12, fontWeight: 700, color: active ? 'var(--x-warning-fg)' : 'var(--muted)',
}}>{index + 1}</div>
<div style={{ flex: 1 }}>
<div style={{ fontSize: 13, fontWeight: 600, color: 'var(--text)' }}>
{fmtTime(brk.started_at)} – {active ? t('shiftPage.now') : fmtTime(brk.ended_at)}
</div>
<div style={{ fontSize: 11, color: 'var(--muted)', marginTop: 1 }}>
{fmtDuration(dur)}
</div>
</div>
{active && (
<span style={{
fontSize: 10, fontWeight: 700, padding: '3px 8px', borderRadius: 999,
background: 'var(--x-warning)', color: 'var(--x-warning-fg)',
}}>{t('venues.active')}</span>
)}
</div>
)
}
// ── 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 (
<AppShell title="Shift Overview">
<div style={{ flex: 1, display: 'flex', alignItems: 'center', justifyContent: 'center', color: 'var(--muted)', fontSize: 14 }}>
{t('common.loading')}
</div>
</AppShell>
)
}
if (!shift) {
return (
<AppShell title="Shift Overview">
<div style={{ flex: 1, display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', gap: 12, padding: 24, textAlign: 'center' }}>
<div style={{ fontSize: 40 }}>⏸</div>
<div style={{ fontSize: 16, fontWeight: 700, color: 'var(--text)' }}>{t('shiftPage.none')}</div>
<div style={{ fontSize: 13, color: 'var(--muted)' }}>{t('shiftPage.noneSub')}</div>
</div>
</AppShell>
)
}
// ── 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 (
<AppShell title="Shift Overview">
<div style={{ flex: 1, overflowY: 'auto', padding: '16px 16px 24px' }}>
{/* Active break banner */}
{activeBreak && (
<div style={{
margin: '0 0 16px',
padding: '12px 16px',
borderRadius: 12,
background: 'rgb(var(--x-warning-rgb) / 0.12)',
border: '1px solid var(--x-warning)',
display: 'flex', alignItems: 'center', gap: 10,
}}>
<span style={{ fontSize: 20 }}>☕</span>
<div>
<div style={{ fontSize: 14, fontWeight: 700, color: 'var(--x-warning)' }}>{t('shiftPage.onBreak')}</div>
<div style={{ fontSize: 12, color: 'var(--muted)', marginTop: 1 }}>
{t('shiftPage.breakSince', { time: fmtTime(activeBreak.started_at), duration: fmtDuration(breakDurationSeconds(activeBreak)) })}
</div>
</div>
</div>
)}
{/* Hero stats grid */}
<div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 10, marginBottom: 20 }}>
<StatCard label={t('shiftPage.start')} value={fmtTime(shift.started_at)} />
<StatCard label={t('shiftPage.worked')} value={fmtDuration(workedSec)} sub={t('shiftPage.breaksSub', { duration: fmtDuration(totalBreakSec) })} />
<StatCard label={t('shiftPage.startCash')} value={fmtEuro(shift.starting_cash)} />
{!hideRevenue && (
<StatCard label={t('shiftPage.collected')} value={fmtEuro(shift.total_collected)} accent="var(--success, var(--x-success))" />
)}
{!hideRevenue && (
<StatCard label={t('shiftPage.toHandIn')} value={fmtEuro(shift.net_to_deliver)} sub={t('shiftPage.toHandInSub')} accent="var(--accent)" />
)}
<StatCard label={t('shiftPage.openTables')} value={String(openOrders.length)} sub={t('shiftPage.openTablesSub')} />
<StatCard label={t('shiftPage.items')} value={String(totalItems)} />
<StatCard label={t('shiftPage.breaks')} value={String((shift.breaks || []).length)} sub={t('shiftPage.totalSub', { duration: fmtDuration(totalBreakSec) })} />
</div>
{/* Per-hour charts */}
{openOrders.length > 0 && (
<div style={{
background: 'var(--bg2)', border: '1px solid var(--border)',
borderRadius: 16, padding: '16px 18px', marginBottom: 20,
}}>
<div style={{ fontSize: 14, fontWeight: 700, color: 'var(--text)', marginBottom: 16 }}>{t('shiftPage.perHour')}</div>
<PerHourChart title={t('nav.orders')} data={ordersByHour} />
<PerHourChart title={t('shiftPage.itemsShort')} data={itemsByHour} />
</div>
)}
{/* Breaks */}
{(shift.breaks || []).length > 0 && (
<div>
<div style={{ fontSize: 12, fontWeight: 700, color: 'var(--muted)', textTransform: 'uppercase', letterSpacing: 0.5, marginBottom: 10 }}>
{t('shiftPage.breaksCount', { count: shift.breaks.length })}
</div>
<div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
{shift.breaks.map((b, i) => <BreakRow key={b.id} brk={b} index={i} />)}
</div>
</div>
)}
</div>
</AppShell>
)
}