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]>
This commit is contained in:
Radosław Smoliński
2026-10-27 15:46:25 +01:00
co-authored by Claude Opus 5.5
parent eeac33ed2f
commit 6b46bc0c14
108 changed files with 2806 additions and 981 deletions
+23 -3
View File
@@ -1,8 +1,28 @@
/** Nav link to /admin/stats (admin.components.afterNavLinks). */
export function StatsNavLink({ adminRoute = '/admin' }: { adminRoute?: string }) {
import type { Payload } from 'payload'
import type { StatsI18n } from './statsI18n.js'
import { canReadStats } from './canReadStats.js'
import { statsI18n, tr } from './statsI18n.js'
type Props = {
adminRoute?: string
/** Injected by Payload into admin server components. */
i18n?: StatsI18n
payload?: Payload
user?: unknown
}
/**
* Nav link to /admin/stats (admin.components.afterNavLinks), shown right after
* the Statistics group, so it reads "Overview". Hidden for users the
* stats-visits collection does not let read.
*/
export async function StatsNavLink({ adminRoute = '/admin', i18n, payload, user }: Props) {
if (!payload || !(await canReadStats(payload, undefined, user))) {return null}
return (
<a className="nav__link" href={`${adminRoute.replace(/\/$/, '')}/stats`} id="nav-ipal-stats">
<span className="nav__link-label">Statistics</span>
<span className="nav__link-label">{tr(statsI18n(i18n), 'statsNavLink')}</span>
</a>
)
}
+52 -40
View File
@@ -5,18 +5,19 @@ import { Gutter } from '@payloadcms/ui'
import type { StatsVisit } from '../collection.js'
import { formatDateTime } from '../../admin-i18n/t.js'
import { VISITOR_ID_PATTERN } from '../parse.js'
import { canReadStats } from './canReadStats.js'
import { loadRecentVisitors, loadStats, loadVisitor } from './loadStats.js'
import { deviceLabel, num, plural, type StatsI18n, tr } from './statsI18n.js'
import { STATS_CSS, VisitsChart } from './VisitsChart.js'
type Props = { adminRoute?: string; timeZone?: string } & AdminViewServerProps
const fmt = (n: number) => n.toLocaleString('en-US')
const when = (iso: string, timeZone: string) =>
new Intl.DateTimeFormat('en-GB', { dateStyle: 'medium', timeStyle: 'short', timeZone }).format(new Date(iso))
const timeOnly = (iso: string, language: string, timeZone: string) =>
new Intl.DateTimeFormat(language, { timeStyle: 'short', timeZone }).format(new Date(iso))
function TopList({ rows, title, unit }: { rows: [string, number][]; title: string; unit: string }) {
function TopList({ i18n, label, rows, title, unit }: { i18n: StatsI18n; label?: (key: string) => React.ReactNode; rows: [string, number][]; title: string; unit: string }) {
return (
<section>
<h3>{title}</h3>
@@ -31,39 +32,39 @@ function TopList({ rows, title, unit }: { rows: [string, number][]; title: strin
<tbody>
{rows.map(([key, count]) => (
<tr key={key}>
<td><code>{key}</code></td>
<td>{fmt(count)}</td>
<td>{label ? label(key) : <code>{key}</code>}</td>
<td>{num(i18n, count)}</td>
</tr>
))}
</tbody>
</table>
) : (
<p>No data yet.</p>
<p>{tr(i18n, 'statsNoData')}</p>
)}
</section>
)
}
function VisitorHistory({ base, timeZone, visitorId, visits }: { base: string; timeZone: string; visitorId: string; visits: StatsVisit[] }) {
function VisitorHistory({ base, i18n, timeZone, visitorId, visits }: { base: string; i18n: StatsI18n; timeZone: string; visitorId: string; visits: StatsVisit[] }) {
const lang = i18n.language
return (
<>
<p><a href={base}>← All statistics</a></p>
<h1>Visitor <code>{visitorId}</code></h1>
<p><a href={base}>{tr(i18n, 'statsBack')}</a></p>
<h1>{tr(i18n, 'statsVisitorHeading')} <code>{visitorId}</code></h1>
<p>
{visits.length} visit{visits.length === 1 ? '' : 's'} recorded with analytics consent. To erase this
visitor’s data, filter <em>Statistics → Visits</em> by this visitor id and delete the results.
{plural(i18n, 'statsVisitorVisits', visits.length)} {tr(i18n, 'statsVisitorErase')}
</p>
{visits.map((v) => (
<section className="ipal-stats-tile" key={v.id} style={{ marginBottom: '1rem' }}>
<strong>{when(v.startedAt, timeZone)}</strong>
<strong>{formatDateTime(v.startedAt, lang, timeZone)}</strong>
<span>
{' '}· {v.pageCount} page{v.pageCount === 1 ? '' : 's'} · {v.device ?? 'unknown device'}
{v.referrer ? ` · from ${v.referrer}` : ''}
{' '}· {plural(i18n, 'statsPages', v.pageCount)} · {deviceLabel(i18n, v.device)}
{v.referrer ? ` · ${tr(i18n, 'statsFrom', { referrer: v.referrer })}` : ''}
</span>
<ol style={{ margin: '0.5rem 0 0', paddingLeft: '1.25rem' }}>
{(v.pages ?? []).map((p, i) => (
<li key={`${p.t}-${i}`}>
<code>{p.p}</code> <span>{when(p.t, timeZone).split(', ').pop()}</span>
<code>{p.p}</code> <span>{timeOnly(p.t, lang, timeZone)}</span>
</li>
))}
</ol>
@@ -87,60 +88,71 @@ export async function StatsView(props: Props) {
const visitorParam = typeof searchParams?.visitor === 'string' ? searchParams.visitor : undefined
const visitorId = visitorParam && VISITOR_ID_PATTERN.test(visitorParam) ? visitorParam : undefined
const i18n = req.i18n as unknown as StatsI18n
const lang = i18n.language
let content: React.ReactNode
if (!(await canReadStats(req.payload, req))) {
content = <p>You do not have access to the statistics.</p>
content = <p>{tr(i18n, 'statsNoAccess')}</p>
} else if (visitorId) {
content = <VisitorHistory base={base} timeZone={timeZone} visitorId={visitorId} visits={await loadVisitor(req.payload, visitorId)} />
content = <VisitorHistory base={base} i18n={i18n} timeZone={timeZone} visitorId={visitorId} visits={await loadVisitor(req.payload, visitorId)} />
} else {
const [stats, recent] = await Promise.all([loadStats(req.payload, { timeZone }), loadRecentVisitors(req.payload, 50)])
const { totals } = stats
const visitors = [...new Map(recent.map((v) => [v.visitorId!, v])).values()].slice(0, 15)
const visits = tr(i18n, 'statsVisits')
content = (
<>
<h1>Statistics</h1>
<p>
Last 30 days. Counted without cookies, so no consent is needed; no IP addresses are stored. Visitors who accept
analytics cookies also get a history across days.
</p>
<h1>{tr(i18n, 'statsTitle')}</h1>
<p>{tr(i18n, 'statsIntro')}</p>
<div className="ipal-stats-tiles">
<div className="ipal-stats-tile"><b>{fmt(totals.visits)}</b><span>Visits</span></div>
<div className="ipal-stats-tile"><b>{fmt(totals.views)}</b><span>Page views</span></div>
<div className="ipal-stats-tile"><b>{num(i18n, totals.visits)}</b><span>{visits}</span></div>
<div className="ipal-stats-tile"><b>{num(i18n, totals.views)}</b><span>{tr(i18n, 'statsPageViews')}</span></div>
<div className="ipal-stats-tile">
<b>{totals.visits ? (totals.views / totals.visits).toFixed(1) : '0'}</b><span>Pages per visit</span>
<b>{num(i18n, totals.visits ? totals.views / totals.visits : 0, { maximumFractionDigits: 1, minimumFractionDigits: 1 })}</b>
<span>{tr(i18n, 'statsPagesPerVisit')}</span>
</div>
<div className="ipal-stats-tile"><b>{Math.round(totals.bounceRate * 100)}%</b><span>Single-page visits</span></div>
<div className="ipal-stats-tile"><b>{fmt(totals.consented)}</b><span>Visits with analytics consent</span></div>
<div className="ipal-stats-tile">
<b>{num(i18n, totals.bounceRate, { maximumFractionDigits: 0, style: 'percent' })}</b>
<span>{tr(i18n, 'statsSinglePageVisits')}</span>
</div>
<div className="ipal-stats-tile"><b>{num(i18n, totals.consented)}</b><span>{tr(i18n, 'statsConsentedVisits')}</span></div>
</div>
<h2>Visits per day</h2>
<VisitsChart days={stats.days} />
<h2>{tr(i18n, 'statsVisitsPerDay')}</h2>
<VisitsChart days={stats.days} i18n={i18n} />
<div className="ipal-stats-lists">
<TopList rows={stats.topPages} title="Page" unit="Views" />
<TopList rows={stats.entries} title="Entry page" unit="Visits" />
<TopList rows={stats.referrers} title="Referrer" unit="Visits" />
<TopList rows={stats.devices} title="Device" unit="Visits" />
<TopList i18n={i18n} rows={stats.topPages} title={tr(i18n, 'statsColPage')} unit={tr(i18n, 'statsColViews')} />
<TopList i18n={i18n} rows={stats.entries} title={tr(i18n, 'statsColEntryPage')} unit={visits} />
<TopList
i18n={i18n}
label={(key) => (key === '(direct)' ? tr(i18n, 'statsDirect') : <code>{key}</code>)}
rows={stats.referrers}
title={tr(i18n, 'statsColReferrer')}
unit={visits}
/>
<TopList i18n={i18n} label={(key) => deviceLabel(i18n, key)} rows={stats.devices} title={tr(i18n, 'statsColDevice')} unit={visits} />
</div>
<section style={{ marginTop: '2rem' }}>
<h3>Visitor history (analytics consent only)</h3>
<h3>{tr(i18n, 'statsVisitorHistory')}</h3>
{visitors.length ? (
<table className="ipal-stats-table">
<thead>
<tr>
<th scope="col">Visitor</th>
<th scope="col">Last seen</th>
<th scope="col">{tr(i18n, 'statsColVisitor')}</th>
<th scope="col">{tr(i18n, 'statsColLastVisit')}</th>
</tr>
</thead>
<tbody>
{visitors.map((v) => (
<tr key={v.visitorId}>
<td><a aria-label={`Visitor ${v.visitorId}`} href={`${base}?visitor=${v.visitorId}`}><code>{v.visitorId}</code></a></td>
<td>{when(v.lastAt, timeZone)}</td>
<td><a aria-label={tr(i18n, 'statsVisitorLink', { id: v.visitorId })} href={`${base}?visitor=${v.visitorId}`}><code>{v.visitorId}</code></a></td>
<td>{formatDateTime(v.lastAt, lang, timeZone)}</td>
</tr>
))}
</tbody>
</table>
) : (
<p>No visitor has accepted analytics cookies yet.</p>
<p>{tr(i18n, 'statsNoVisitors')}</p>
)}
</section>
</>
+10 -5
View File
@@ -1,12 +1,16 @@
import type { Payload, PayloadRequest } from 'payload'
import type { StatsI18n } from './statsI18n.js'
import { canReadStats } from './canReadStats.js'
import { loadStats } from './loadStats.js'
import { num, statsI18n, tr } from './statsI18n.js'
import { STATS_CSS, VisitsChart } from './VisitsChart.js'
type Props = {
adminRoute?: string
/** Injected by Payload into admin server components. */
i18n?: StatsI18n
payload?: Payload
req?: PayloadRequest
timeZone?: string
@@ -19,8 +23,9 @@ type Props = {
* collection lets read (admins by default); renders nothing on
* any error (DB not ready, collection missing).
*/
export async function StatsWidget({ adminRoute = '/admin', payload, req, timeZone = 'Europe/Warsaw', user }: Props) {
export async function StatsWidget({ adminRoute = '/admin', i18n: i18nProp, payload, req, timeZone = 'Europe/Warsaw', user }: Props) {
if (!payload || !(await canReadStats(payload, req, user))) {return null}
const i18n = statsI18n(i18nProp ?? (req?.i18n as StatsI18n | undefined))
let stats
try {
stats = await loadStats(payload, { timeZone })
@@ -30,12 +35,12 @@ export async function StatsWidget({ adminRoute = '/admin', payload, req, timeZon
return (
<section className="ipal-stats" style={{ marginBottom: '2rem' }}>
<style>{STATS_CSS}</style>
<h2 style={{ marginBottom: '0.25rem' }}>Visits per day</h2>
<h2 style={{ marginBottom: '0.25rem' }}>{tr(i18n, 'statsVisitsPerDay')}</h2>
<p style={{ color: 'var(--theme-elevation-500)', margin: '0 0 0.75rem' }}>
Last 30 days: {stats.totals.visits.toLocaleString('en-US')} visits, {stats.totals.views.toLocaleString('en-US')} page views ·{' '}
<a href={`${adminRoute.replace(/\/$/, '')}/stats`}>All statistics →</a>
{tr(i18n, 'statsWidgetSummary', { views: num(i18n, stats.totals.views), visits: num(i18n, stats.totals.visits) })} ·{' '}
<a href={`${adminRoute.replace(/\/$/, '')}/stats`}>{tr(i18n, 'statsWidgetLink')}</a>
</p>
<VisitsChart days={stats.days} />
<VisitsChart days={stats.days} i18n={i18n} />
</section>
)
}
+22 -12
View File
@@ -1,4 +1,7 @@
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
@@ -18,14 +21,21 @@ function column(x: number, y: number, w: number, h: number): string {
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`
}
const shortDate = (day: string) => `${day.slice(8, 10)}.${day.slice(5, 7)}`
/** 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 }: { days: StatsDay[] }) {
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
@@ -41,7 +51,7 @@ export function VisitsChart({ days }: { days: StatsDay[] }) {
<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)}>
{Number.isInteger(t) ? t : t.toFixed(1)}
{num(i18n, t, { maximumFractionDigits: 1 })}
</text>
</g>
))}
@@ -51,12 +61,12 @@ export function VisitsChart({ days }: { days: StatsDay[] }) {
const label = i % 7 === (days.length - 1) % 7
return (
<g className="ipal-stats-day" key={d.day}>
<title>{`${d.day}: ${d.visits} visits, ${d.views} page views`}</title>
<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}>
{shortDate(d.day)}
{dayLabel(d.day, lang, 'short')}
</text>
) : null}
</g>
@@ -64,21 +74,21 @@ export function VisitsChart({ days }: { days: StatsDay[] }) {
})}
</svg>
<details style={{ marginTop: '0.5rem' }}>
<summary>Table</summary>
<summary>{tr(i18n, 'statsChartTable')}</summary>
<table className="ipal-stats-table">
<thead>
<tr>
<th scope="col">Day</th>
<th scope="col">Visits</th>
<th scope="col">Page views</th>
<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>{d.day}</td>
<td>{d.visits}</td>
<td>{d.views}</td>
<td>{dayLabel(d.day, lang, 'medium')}</td>
<td>{num(i18n, d.visits)}</td>
<td>{num(i18n, d.views)}</td>
</tr>
))}
</tbody>
+40
View File
@@ -0,0 +1,40 @@
import type { AdminTextKey } from '../../admin-i18n/texts/index.js'
import { formatNumber, ipalT } from '../../admin-i18n/t.js'
import { adminTexts } from '../../admin-i18n/texts/index.js'
/** The part of Payload's server `i18n` the stats components use. */
export type StatsI18n = { language: string; t: (key: never, vars?: Record<string, unknown>) => string }
/** Payload passes `i18n` to admin server components; without it, English. */
const englishOnly: StatsI18n = {
language: 'en',
t: ((key: string, vars?: Record<string, unknown>) => {
const text = (adminTexts.en as Record<string, string>)[key.replace(/^ipal:/, '')] ?? key
return text.replace(/\{\{(\w+)\}\}/g, (_, name: string) => String((vars?.[name] as number | string | undefined) ?? ''))
}) as StatsI18n['t'],
}
export const statsI18n = (i18n?: null | StatsI18n): StatsI18n => i18n ?? englishOnly
export const tr = (i18n: StatsI18n, key: AdminTextKey, vars?: Record<string, unknown>) => ipalT(i18n.t, key, vars)
/** Number in the admin language, e.g. 1 234 (pl) / 1,234 (en). */
export const num = (i18n: StatsI18n, n: number, options?: Intl.NumberFormatOptions) =>
options ? new Intl.NumberFormat(i18n.language, options).format(n) : formatNumber(n, i18n.language)
const PLURAL_SUFFIX = { few: 'Few', many: 'Many', one: 'One', other: 'Other', two: 'Other', zero: 'Other' } as const
/** Plural text: `base` + One / Few / Many / Other, picked by Intl.PluralRules for the admin language. */
export function plural(i18n: StatsI18n, base: 'statsPages' | 'statsVisitorVisits', count: number): string {
const category = new Intl.PluralRules(i18n.language).select(count)
return tr(i18n, `${base}${PLURAL_SUFFIX[category]}` as AdminTextKey, { count: num(i18n, count) })
}
/** Panel name of a stored device value. */
export function deviceLabel(i18n: StatsI18n, device: null | string | undefined): string {
if (device === 'desktop') {return tr(i18n, 'statsDeviceDesktop')}
if (device === 'mobile') {return tr(i18n, 'statsDeviceMobile')}
if (device === 'tablet') {return tr(i18n, 'statsDeviceTablet')}
return device || tr(i18n, 'statsUnknownDevice')
}
+42 -16
View File
@@ -63,37 +63,63 @@ export function buildStatsCollection(): CollectionConfig {
},
admin: {
defaultColumns: ['day', 'entry', 'pageCount', 'referrer', 'device', 'visitorId'],
description:
'Visits from the built-in statistics. Anonymous by default; a visitor id appears only for visitors who accepted analytics cookies. No IP addresses are stored.',
group: 'Statistics',
description: {
en: 'Visits counted by the built-in statistics, without IP addresses.',
pl: 'Wizyty liczone przez wbudowane statystyki, bez adresów IP.',
},
group: { en: 'Statistics', pl: 'Statystyki' },
useAsTitle: 'entry',
},
fields: [
{ name: 'day', type: 'text', index: true, required: true },
{ name: 'startedAt', type: 'date', admin: { date: { pickerAppearance: 'dayAndTime' } }, required: true },
{ name: 'lastAt', type: 'date', admin: { date: { pickerAppearance: 'dayAndTime' } }, index: true, required: true },
{ name: 'entry', type: 'text', required: true },
{ name: 'exit', type: 'text', required: true },
{ name: 'pageCount', type: 'number', required: true },
{ name: 'pages', type: 'json' },
{ name: 'referrer', type: 'text' },
{ name: 'day', type: 'text', index: true, label: { en: 'Day', pl: 'Dzień' }, required: true },
{
name: 'startedAt',
type: 'date',
admin: { date: { pickerAppearance: 'dayAndTime' } },
label: { en: 'Start', pl: 'Początek' },
required: true,
},
{
name: 'lastAt',
type: 'date',
admin: { date: { pickerAppearance: 'dayAndTime' } },
index: true,
label: { en: 'Last activity', pl: 'Ostatnia aktywność' },
required: true,
},
{ name: 'entry', type: 'text', label: { en: 'Entry page', pl: 'Strona wejścia' }, required: true },
{ name: 'exit', type: 'text', label: { en: 'Exit page', pl: 'Strona wyjścia' }, required: true },
{ name: 'pageCount', type: 'number', label: { en: 'Pages viewed', pl: 'Liczba stron' }, required: true },
{ name: 'pages', type: 'json', label: { en: 'Pages', pl: 'Strony' } },
{ name: 'referrer', type: 'text', label: { en: 'Referrer', pl: 'Strona odsyłająca' } },
{
name: 'device',
type: 'select',
options: ['desktop', 'mobile', 'tablet'],
label: { en: 'Device', pl: 'Urządzenie' },
options: [
{ label: { en: 'Desktop', pl: 'Komputer' }, value: 'desktop' },
{ label: { en: 'Mobile', pl: 'Telefon' }, value: 'mobile' },
{ label: { en: 'Tablet', pl: 'Tablet' }, value: 'tablet' },
],
},
{ name: 'locale', type: 'text' },
{ name: 'country', type: 'text' },
{ name: 'locale', type: 'text', label: { en: 'Language', pl: 'Język' } },
{ name: 'country', type: 'text', label: { en: 'Country', pl: 'Kraj' } },
{
name: 'visitorId',
type: 'text',
admin: { description: 'Only with analytics consent — the id from the visitor’s ipal_vid cookie.' },
admin: {
description: {
en: 'Set only for visitors who accepted analytics cookies.',
pl: 'Tylko u odwiedzających, którzy zaakceptowali cookies analityczne.',
},
},
index: true,
label: { en: 'Visitor ID', pl: 'Identyfikator odwiedzającego' },
},
// Daily-salted, unlinkable after midnight; only used to join hits into a visit.
{ name: 'visitKey', type: 'text', admin: { hidden: true }, index: true },
],
labels: { plural: 'Visits', singular: 'Visit' },
labels: { plural: { en: 'Visits', pl: 'Wizyty' }, singular: { en: 'Visit', pl: 'Wizyta' } },
timestamps: false,
}
}