- The period: one toolbar (range / day as a Segmented, dates, quick choices, the report's own filters and export / print on the right), every control 44 px; the business day a searchable Dropdown. - Report filters (person, category, status, waiter, table, printer) are the shared Dropdown; export / print buttons as tall as the rest; trends' step a Segmented. - A report's tabs are the shared tab strip above the page; a day's and an order's sections too. - e2e: the floor editor's edge '+' and the layout check cover products, tables set-up and the channel prices. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
144 lines
7.1 KiB
React
144 lines
7.1 KiB
React
// The report kit's building blocks (plan step 6.12a): a report's page with tabs, a panel, a big
|
|
// figure, a sortable table, loading / error states, the export button. On the tokens, texts in
|
|
// `manager:reports.*`, money and dates through useFormat().
|
|
import { useState } from 'react'
|
|
import { useSearchParams } from 'react-router-dom'
|
|
import toast from 'react-hot-toast'
|
|
import { ArrowDown, ArrowUp, Download, Info } from 'lucide-react'
|
|
import { errorMessage } from '@xenia/api'
|
|
import { useFormat, useTranslation } from '@xenia/i18n'
|
|
import { Button, EmptyState, InfoTip, Page, PageHeader, Spinner, Tabs, cx } from '@xenia/ui'
|
|
import client from '../../../api/client'
|
|
import { sortRows } from './reportLogic'
|
|
|
|
/** A report page: its tabs (kept in the address as ?tab=) over the chosen tab's content. */
|
|
export function ReportPage({ title, tabs = [], children, testId }) {
|
|
const [params, setParams] = useSearchParams()
|
|
const active = tabs.find(t => t.id === params.get('tab'))?.id ?? tabs[0]?.id
|
|
const tab = tabs.find(t => t.id === active)
|
|
return (
|
|
<Page testId={testId}
|
|
top={tabs.length > 1 ? <Tabs size="lg" value={active} tabs={tabs.map(t => ({ id: t.id, label: t.label, testId: `report-tab-${t.id}` }))}
|
|
onChange={id => setParams(p => { const n = new URLSearchParams(p); n.set('tab', id); return n }, { replace: true })} /> : undefined}>
|
|
<PageHeader title={title} />
|
|
{tab ? <tab.Component key={tab.id} /> : children}
|
|
</Page>
|
|
)
|
|
}
|
|
|
|
/** A card with a title, an optional tip and actions. */
|
|
export function Panel({ title, tip, actions, children, className, padded = true, testId }) {
|
|
return (
|
|
<section className={cx('rounded-card border border-line bg-surface shadow-raised overflow-hidden', className)} data-testid={testId}>
|
|
{(title || actions) && (
|
|
<header className="flex flex-wrap items-center gap-2 px-card py-card-y border-b border-line">
|
|
<h2 className="flex-1 min-w-0 text-body font-semibold text-ink flex items-center gap-1">{title}{tip && <InfoTip>{tip}</InfoTip>}</h2>
|
|
{actions}
|
|
</header>
|
|
)}
|
|
<div className={padded ? 'p-card' : ''}>{children}</div>
|
|
</section>
|
|
)
|
|
}
|
|
|
|
const TONE = { success: 'text-success-ink', danger: 'text-danger-ink', warning: 'text-warning-ink', info: 'text-info-ink', accent: 'text-accent-ink' }
|
|
|
|
/** A figure: a label over a value, an optional line under it, a tone for the value. */
|
|
export function Stat({ label, value, sub, tip, tone, big = false, testId }) {
|
|
return (
|
|
<div className="rounded-card border border-line bg-surface shadow-raised p-card grid gap-1 content-start" data-testid={testId}>
|
|
<span className="text-overline font-semibold uppercase tracking-wide text-ink-3 flex items-center gap-1">{label}{tip && <InfoTip>{tip}</InfoTip>}</span>
|
|
<span className={cx('font-semibold tabular-nums', big ? 'text-display' : 'text-title', TONE[tone] ?? 'text-ink')}>{value}</span>
|
|
{sub && <span className="text-caption text-ink-2">{sub}</span>}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export function Loading() {
|
|
return <div className="py-16 grid place-items-center"><Spinner /></div>
|
|
}
|
|
|
|
export function Failed({ error, retry }) {
|
|
const { t } = useTranslation('manager')
|
|
return <EmptyState title={t('reports.failed')} description={errorMessage(error, t)}
|
|
action={retry ? <Button variant="secondary" onClick={retry}>{t('reports.retry')}</Button> : undefined} />
|
|
}
|
|
|
|
/**
|
|
* A sortable table. columns: [{ key, label, align?, render?(row), sort? (key or fn; false = not sortable), tip? }]
|
|
* Shows at most `limit` rows ("Showing 100 of 240").
|
|
*/
|
|
export function Table({ columns, rows, initialSort, onRow, rowTestId, limit = 200, empty, rowKey = r => r.id }) {
|
|
const { t, i18n } = useTranslation('manager')
|
|
const [sort, setSort] = useState(initialSort ?? null) // { key, dir }
|
|
if (!rows.length) return <EmptyState compact title={empty ?? t('reports.nothing')} />
|
|
const col = sort && columns.find(c => c.key === sort.key)
|
|
const sorted = col ? sortRows(rows, typeof col.sort === 'function' ? col.sort : (col.sort ?? col.key), sort.dir, i18n.language) : rows
|
|
const shown = sorted.slice(0, limit)
|
|
const flip = c => setSort(s => (s?.key === c.key ? { key: c.key, dir: s.dir === 'desc' ? 'asc' : 'desc' } : { key: c.key, dir: c.align === 'right' ? 'desc' : 'asc' }))
|
|
return (
|
|
<div className="overflow-x-auto">
|
|
<table className="w-full text-label border-collapse">
|
|
<thead>
|
|
<tr className="bg-surface-2 text-ink-2">
|
|
{columns.map(c => (
|
|
<th key={c.key} className={cx('px-4 py-2.5 align-middle font-semibold whitespace-nowrap', c.align === 'right' ? 'text-right' : 'text-left')}>
|
|
{c.sort === false ? c.label : (
|
|
<button type="button" className="inline-flex items-center gap-1 hover:text-ink" onClick={() => flip(c)}>
|
|
{c.label}{sort?.key === c.key && (sort.dir === 'desc' ? <ArrowDown size={12} /> : <ArrowUp size={12} />)}
|
|
</button>
|
|
)}
|
|
{c.tip && <InfoTip>{c.tip}</InfoTip>}
|
|
</th>
|
|
))}
|
|
</tr>
|
|
</thead>
|
|
<tbody className="divide-y divide-line">
|
|
{shown.map(r => (
|
|
<tr key={rowKey(r)} onClick={onRow ? () => onRow(r) : undefined} data-testid={rowTestId}
|
|
className={cx(onRow && 'cursor-pointer hover:bg-surface-2')}>
|
|
{columns.map(c => (
|
|
<td key={c.key} className={cx('px-4 py-2.5 align-middle', c.align === 'right' && 'text-right tabular-nums')}>{c.render ? c.render(r) : r[c.key]}</td>
|
|
))}
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
{sorted.length > limit && <p className="px-3 py-2 text-caption text-ink-3 border-t border-line">{t('reports.showing', { shown: limit, total: sorted.length })}</p>}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
/** Money with an optional "includes estimates" mark (a has_gap total is a lower bound). */
|
|
export function MoneyCell({ value, partial }) {
|
|
const { t } = useTranslation('manager')
|
|
const { formatMoney } = useFormat()
|
|
return (
|
|
<span className="inline-flex items-center gap-1">
|
|
{formatMoney(value ?? 0)}
|
|
{partial && <span title={t('reports.partialTip')} className="text-warning-ink"><Info size={12} aria-label={t('reports.partial')} /></span>}
|
|
</span>
|
|
)
|
|
}
|
|
|
|
/** Downloads a CSV from a report's export endpoint (venue time, the venue's Excel separators). */
|
|
export function ExportButton({ endpoint, params, filename }) {
|
|
const { t } = useTranslation('manager')
|
|
const [busy, setBusy] = useState(false)
|
|
async function run() {
|
|
setBusy(true)
|
|
try {
|
|
const r = await client.get(endpoint, { params, responseType: 'blob' })
|
|
const url = URL.createObjectURL(r.data)
|
|
const a = document.createElement('a')
|
|
a.href = url
|
|
a.download = filename
|
|
document.body.appendChild(a)
|
|
a.click()
|
|
a.remove()
|
|
setTimeout(() => URL.revokeObjectURL(url), 1000)
|
|
} catch (err) { toast.error(errorMessage(err, t)) } finally { setBusy(false) }
|
|
}
|
|
return <Button variant="secondary" icon={<Download size={16} />} loading={busy} onClick={run} data-testid="report-export">{t('reports.export')}</Button>
|
|
}
|