import type { Payload } from 'payload' import type { IntegrationStatus } from './status.js' import { userMeetsRole } from '../access/predicates.js' import { turnstileMissingKey } from '../turnstile/config.js' import { type IntegrationLabel, resolveLabel, serverText, STATUS_CODE_TEXTS } from './texts.js' type Props = { /** Admin route, for the link. Default '/admin'. */ adminRoute?: string /** Status groups to check: field name → label (one string, or one per language). */ checks?: Record /** * Who sees WHAT is failing (the error text and the link to Site integrations): `'admin'` * (default — the role that may read that global), `'editor'`, or `'all'` (everyone who opens * the dashboard, as before). Below that, an editor sees a neutral line; anyone else nothing. */ details?: 'admin' | 'all' | 'editor' /** Injected by Payload into admin server components. */ i18n?: { language?: string; t?: (key: never, vars?: Record) => string } /** Injected by Payload into admin server components. */ payload?: Payload /** Injected by Payload into admin server components — the account looking at the dashboard. */ user?: null | Record } const DEFAULT_CHECKS: Record = { emailStatus: { en: 'E-mail sending', pl: 'Wysyłka e-mail' }, turnstileStatus: { en: 'Spam protection', pl: 'Ochrona przed spamem' }, } /** * Dashboard alert (admin.components.beforeDashboard): lists integrations that * are currently failing, with a link to Site integrations. Renders nothing when * everything is fine — and never throws (DB not ready → nothing). * * The texts come from the Site integrations global, which only admins may read: the widget * reads it with access control off, so it decides itself who gets them (`details`). A * session that still owes its second factor gets nothing (`userMeetsRole` includes it). */ export async function IntegrationAlerts({ adminRoute = '/admin', checks = DEFAULT_CHECKS, details = 'admin', i18n, payload, user }: Props) { if (!payload) {return null} const sees = (role: 'admin' | 'editor') => Boolean(user) && userMeetsRole({ payload, user }, role) const full = details === 'all' || sees(details) if (!full && !sees('editor')) {return null} let doc: Record try { doc = (await payload.findGlobal({ slug: 'site-integrations' as never, depth: 0 })) as Record } catch { return null } const i18nLike = i18n const language = i18n?.language const failing = Object.entries(checks) .map(([field, label]) => { const status = doc?.[field] as IntegrationStatus | undefined if (status?.state !== 'degraded') {return { field, label: resolveLabel(label, language), message: null }} const key = status.code ? STATUS_CODE_TEXTS[status.code] : undefined const message = key ? serverText(i18nLike, key) : (status.message ?? status.code ?? serverText(i18nLike, 'alertsUnknownError')) return { field, label: resolveLabel(label, language), message } }) .filter((x): x is { field: string; label: string; message: string } => x.message !== null) // Configuration problems no request will ever record (nothing is verified // while Turnstile is off) — checked live from the saved keys. const missing = turnstileMissingKey(doc ?? {}) if (missing && 'turnstileStatus' in checks && !failing.some((f) => f.field === 'turnstileStatus')) { failing.push({ field: 'turnstileStatus', label: resolveLabel(checks.turnstileStatus, language), message: serverText(i18nLike, missing === 'secret' ? 'statusTurnstileNoSecretKey' : 'statusTurnstileNoSiteKey'), }) } if (!failing.length) {return null} // Not allowed the detail: that something needs attention, and whom to ask — nothing of what or why. if (!full) { return (
{serverText(i18nLike, 'alertsHeading')} {serverText(i18nLike, 'alertsAskAdmin')}
) } return (
{serverText(i18nLike, 'alertsHeading')}
    {failing.map(({ field, label, message }) => (
  • {label}: {message}
  • ))}
{serverText(i18nLike, 'alertsLink')}
) } export default IntegrationAlerts