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 user?: unknown } /** * Dashboard card (admin.components.beforeDashboard): visits per day for the * last 30 days with a link to the full view. For users the stats-visits * collection lets read (admins by default); renders nothing on * any error (DB not ready, collection missing). */ 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 }) } catch { return null } return (

{tr(i18n, 'statsVisitsPerDay')}

{tr(i18n, 'statsWidgetSummary', { views: num(i18n, stats.totals.views), visits: num(i18n, stats.totals.visits) })} ยท{' '} {tr(i18n, 'statsWidgetLink')}

) } export default StatsWidget