Files
ipal-kit/src/modules/stats/admin/VisitsChart.tsx
T
Radosław SmolińskiandClaude Opus 5.5 6b46bc0c14 Admin panel in English and Polish, reworded per docs/dev/admin-ux.md
- admin-i18n module: AdminText { en, pl }, component texts under the ipal
  namespace (project translations win), en + pl admin languages when the
  project sets none (project's supportedLanguages wins)
- every plugin label, description, option, tab and group as { en, pl };
  explicit labels instead of auto-labels; sentence case; glossary terms
- visible renames (slugs, names, data unchanged): Cookie banner, Form
  messages, Privacy group, Search engines, Maintenance, Spam protection,
  E-mail sending, stats nav link Overview (hidden without access)
- Polish visitor defaults by page locale: cookie banner, form messages,
  validators, lightbox, accessibility widget; {field} uses the label
- fixes: e-mail status banner used Turnstile wording; developer words and
  env var names out of editor messages; stats numbers/dates in the admin
  language; test e-mail no longer says ipal-kit
- test/adminUx.test.ts: length limits, en + pl present, no developer words

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-27 15:46:25 +01:00

119 lines
5.4 KiB
TypeScript
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 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 (
<figure className="ipal-stats-chart" style={{ margin: 0 }}>
<svg aria-hidden="true" style={{ display: 'block', height: 'auto', width: '100%' }} viewBox={`0 0 ${W} ${H}`}>
{ticks.map((t) => (
<g key={t}>
<line className="ipal-stats-grid" x1={PAD.left} x2={W - PAD.right} y1={y(t)} y2={y(t)} />
<text className="ipal-stats-axis" dominantBaseline="middle" textAnchor="end" x={PAD.left - 6} y={y(t)}>
{num(i18n, t, { maximumFractionDigits: 1 })}
</text>
</g>
))}
{days.map((d, i) => {
const x = PAD.left + i * band
const h = (d.visits / max) * plotH
const label = i % 7 === (days.length - 1) % 7
return (
<g className="ipal-stats-day" key={d.day}>
<title>{tr(i18n, 'statsChartDay', { day: dayLabel(d.day, lang, 'medium'), views: num(i18n, d.views), visits: num(i18n, d.visits) })}</title>
<rect fill="transparent" height={plotH} width={band} x={x} y={PAD.top} />
{h > 0 ? <path className="ipal-stats-bar" d={column(x + (band - barW) / 2, y(d.visits), barW, h)} /> : null}
{label ? (
<text className="ipal-stats-axis" textAnchor="middle" x={x + band / 2} y={H - 6}>
{dayLabel(d.day, lang, 'short')}
</text>
) : null}
</g>
)
})}
</svg>
<details style={{ marginTop: '0.5rem' }}>
<summary>{tr(i18n, 'statsChartTable')}</summary>
<table className="ipal-stats-table">
<thead>
<tr>
<th scope="col">{tr(i18n, 'statsColDay')}</th>
<th scope="col">{tr(i18n, 'statsVisits')}</th>
<th scope="col">{tr(i18n, 'statsPageViews')}</th>
</tr>
</thead>
<tbody>
{[...days].reverse().map((d) => (
<tr key={d.day}>
<td>{dayLabel(d.day, lang, 'medium')}</td>
<td>{num(i18n, d.visits)}</td>
<td>{num(i18n, d.views)}</td>
</tr>
))}
</tbody>
</table>
</details>
</figure>
)
}
/** 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; }
`