import type { StatsDay } from '../summarize.js' import type { StatsI18n } from './statsI18n.js' import { num, statsI18n, tr } from './statsI18n.js' const W = 1000 const H = 220 const PAD = { bottom: 24, left: 36, right: 8, top: 12 } const RADIUS = 4 /** Round up to 1 / 2 / 5 × 10^k, so the gridlines land on readable numbers. */ function niceMax(value: number): number { if (value <= 4) {return 4} const pow = 10 ** Math.floor(Math.log10(value)) return ([1, 2, 5, 10].map((s) => s * pow).find((s) => s >= value) ?? 10 * pow) } /** Column with a 4px rounded top and a square base on the baseline. */ function column(x: number, y: number, w: number, h: number): string { const r = Math.min(RADIUS, w / 2, h) return `M${x},${y + h}V${y + r}Q${x},${y} ${x + r},${y}H${x + w - r}Q${x + w},${y} ${x + w},${y + r}V${y + h}Z` } /** A `YYYY-MM-DD` day in the admin language: short (axis) or medium (table, tooltip). */ const dayLabel = (day: string, language: string, style: 'medium' | 'short') => new Intl.DateTimeFormat( language, style === 'short' ? { day: 'numeric', month: 'numeric', timeZone: 'UTC' } : { dateStyle: 'medium', timeZone: 'UTC' }, ).format(new Date(`${day}T00:00:00Z`)) /** * Visits per day — server-rendered SVG, no client JS. One series, so no * legend: the heading names it. Each day has a full-height hover target with * a native tooltip, and the numbers are in a table below for screen readers. */ export function VisitsChart({ days, i18n: i18nProp }: { days: StatsDay[]; i18n?: StatsI18n }) { const i18n = statsI18n(i18nProp) const lang = i18n.language const max = niceMax(Math.max(0, ...days.map((d) => d.visits))) const plotW = W - PAD.left - PAD.right const plotH = H - PAD.top - PAD.bottom const band = plotW / Math.max(1, days.length) const barW = Math.min(24, band - 2) const y = (v: number) => PAD.top + plotH - (v / max) * plotH const ticks = [0, max / 2, max] return (
{tr(i18n, 'statsChartTable')} {[...days].reverse().map((d) => ( ))}
{tr(i18n, 'statsColDay')} {tr(i18n, 'statsVisits')} {tr(i18n, 'statsPageViews')}
{dayLabel(d.day, lang, 'medium')} {num(i18n, d.visits)} {num(i18n, d.views)}
) } /** Colors from the reference palette (series 1, light/dark) + Payload's theme tokens for ink. */ export const STATS_CSS = ` .ipal-stats { --ipal-series-1: #2a78d6; } html[data-theme='dark'] .ipal-stats { --ipal-series-1: #3987e5; } .ipal-stats-bar { fill: var(--ipal-series-1); } .ipal-stats-day:hover .ipal-stats-bar { opacity: 0.8; } .ipal-stats-grid { stroke: var(--theme-elevation-100); stroke-width: 1; } .ipal-stats-axis { fill: var(--theme-elevation-500); font-size: 11px; } .ipal-stats-tiles { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); margin: 1rem 0 1.5rem; } .ipal-stats-tile { border: 1px solid var(--theme-elevation-150); border-radius: 4px; padding: 0.75rem 1rem; } .ipal-stats-tile b { display: block; font-size: 1.75rem; line-height: 1.2; } .ipal-stats-tile span { color: var(--theme-elevation-500); font-size: 0.85rem; } .ipal-stats-lists { display: grid; gap: 1.5rem; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); margin-top: 2rem; } .ipal-stats-table { border-collapse: collapse; width: 100%; } .ipal-stats-table th, .ipal-stats-table td { border-bottom: 1px solid var(--theme-elevation-100); padding: 0.35rem 0.5rem; text-align: left; } .ipal-stats-table td:not(:first-child), .ipal-stats-table th:not(:first-child) { font-variant-numeric: tabular-nums; text-align: right; } .ipal-stats-table code { word-break: break-all; } `