IntegrationAlerts reads the admin-only Site integrations global with access
control off and showed its error texts to everyone who opened the dashboard.
Now: an admin sees what is failing and the link, an editor a neutral "an
integration needs attention — ask an administrator", anyone else nothing.
CookieCrawlAlert is shown to editors and above (the role that may read the crawl
result). Both use userMeetsRole, so a session that still owes its second factor
sees neither. dashboardAlerts: { integrations: 'admin' | 'editor' | 'all',
cookies: 'editor' | 'all' } loosens it.
Co-Authored-By: Claude Fable 5.2 <[email protected]>
113 lines
5.0 KiB
TypeScript
113 lines
5.0 KiB
TypeScript
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<string, IntegrationLabel>
|
|
/**
|
|
* 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, unknown>) => 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<string, unknown>
|
|
}
|
|
|
|
const DEFAULT_CHECKS: Record<string, IntegrationLabel> = {
|
|
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<string, unknown>
|
|
try {
|
|
doc = (await payload.findGlobal({ slug: 'site-integrations' as never, depth: 0 })) as Record<string, unknown>
|
|
} 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 (
|
|
<div data-ipal="integration-alerts" role="status" style={{ border: '1px solid var(--theme-elevation-200, #ddd)', borderRadius: 4, marginBottom: '1.5rem', padding: '1rem 1.25rem' }}>
|
|
<strong>{serverText(i18nLike, 'alertsHeading')}</strong> {serverText(i18nLike, 'alertsAskAdmin')}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<div
|
|
data-ipal="integration-alerts"
|
|
role="alert"
|
|
style={{
|
|
background: 'var(--theme-error-50, rgba(229, 72, 77, 0.08))',
|
|
border: '1px solid var(--theme-error-400, #e5484d)',
|
|
borderRadius: 4,
|
|
marginBottom: '1.5rem',
|
|
padding: '1rem 1.25rem',
|
|
}}
|
|
>
|
|
<strong>{serverText(i18nLike, 'alertsHeading')}</strong>
|
|
<ul style={{ margin: '0.5rem 0 0', paddingLeft: '1.2rem' }}>
|
|
{failing.map(({ field, label, message }) => (
|
|
<li key={field}>
|
|
<strong>{label}:</strong> {message}
|
|
</li>
|
|
))}
|
|
</ul>
|
|
<a href={`${adminRoute.replace(/\/$/, '')}/globals/site-integrations`} style={{ display: 'inline-block', marginTop: '0.5rem' }}>
|
|
{serverText(i18nLike, 'alertsLink')}
|
|
</a>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default IntegrationAlerts
|