// 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 ( 1 ? ({ 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}> {tab ? : children} ) } /** A card with a title, an optional tip and actions. */ export function Panel({ title, tip, actions, children, className, padded = true, testId }) { return (
{(title || actions) && (

{title}{tip && {tip}}

{actions}
)}
{children}
) } 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 (
{label}{tip && {tip}} {value} {sub && {sub}}
) } export function Loading() { return
} export function Failed({ error, retry }) { const { t } = useTranslation('manager') return {t('reports.retry')} : 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 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 (
{columns.map(c => ( ))} {shown.map(r => ( onRow(r) : undefined} data-testid={rowTestId} className={cx(onRow && 'cursor-pointer hover:bg-surface-2')}> {columns.map(c => ( ))} ))}
{c.sort === false ? c.label : ( )} {c.tip && {c.tip}}
{c.render ? c.render(r) : r[c.key]}
{sorted.length > limit &&

{t('reports.showing', { shown: limit, total: sorted.length })}

}
) } /** 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 ( {formatMoney(value ?? 0)} {partial && } ) } /** 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 }