Files
ipal-kit/src/modules/integrations/status.ts
T
radoslaw.smolinskiandClaude Opus 5.5 2bd9d60822 Cookie banner layout options; keep admin UI out of /client
CookieBanner: order, settings="modal", controls="switch",
settingsActions, icon and a class slot for every element. Cookie
Settings gains eyebrow, title, settingsDescription, alwaysActiveLabel
and buttons.close.

MaskedField, TestEmailButton and IntegrationStatusBanner import
@payloadcms/ui and moved from /client to /admin; with sideEffects in
package.json the site bundle no longer ships ~235 KB gz of admin code.
A test walks the /client import graph to keep it that way.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-17 03:20:11 +02:00

102 lines
3.4 KiB
TypeScript

import type { BasePayload, Field } from 'payload'
/**
* Health of a third-party integration (Turnstile today, SMTP/Graph later),
* stored on the SiteIntegrations global so the panel can show it:
* - a banner on the integration's tab (IntegrationStatusBanner),
* - an alert on the admin dashboard (IntegrationAlerts).
*
* Written by the server when it USES the integration (e.g. verifying a form
* token) — no polling, no cron. Writes are throttled: only on a state change, or
* at most every REFRESH_MS while an error persists, so a broken integration on a
* busy form doesn't turn every submission into a DB write.
*/
export type IntegrationState = 'degraded' | 'ok'
export type IntegrationStatus = {
/** Last time the integration was used (refreshed at most every REFRESH_MS). */
checkedAt?: null | string
/** Machine code from the provider or the plugin, e.g. 'invalid-input-secret', 'missing-secret'. */
code?: null | string
/** Human explanation shown in the panel (English — panel language). */
message?: null | string
/** When the current state started. */
since?: null | string
state?: IntegrationState | null
}
const REFRESH_MS = 10 * 60_000
const SITE_INTEGRATIONS = 'site-integrations'
/** Hidden group field holding the status — add next to the integration's fields. */
export function buildIntegrationStatusField(name: string): Field {
return {
name,
type: 'group',
admin: { hidden: true },
fields: [
{ name: 'state', type: 'select', options: ['ok', 'degraded'] },
{ name: 'code', type: 'text' },
{ name: 'message', type: 'text' },
{ name: 'since', type: 'date' },
{ name: 'checkedAt', type: 'date' },
],
}
}
/** Read-only banner (UI field) — shows the status group `statusField` on the tab. */
export function buildIntegrationStatusBanner(statusField: string, label: string): Field {
return {
name: `${statusField}Banner`,
type: 'ui',
admin: {
components: {
Field: {
clientProps: { label, statusField },
path: '@intecion/ipal-kit/admin#IntegrationStatusBanner',
},
},
},
}
}
/**
* Persist a status change. Never throws — a status write must not break the
* request that triggered it.
*/
export async function recordIntegrationStatus(
payload: BasePayload,
field: string,
previous: IntegrationStatus | null | undefined,
next: { code?: string; message?: string; state: IntegrationState },
): Promise<void> {
const now = new Date()
const changed = previous?.state !== next.state || (next.state === 'degraded' && previous?.code !== next.code)
const stale =
next.state === 'degraded' &&
(!previous?.checkedAt || now.getTime() - new Date(previous.checkedAt).getTime() > REFRESH_MS)
// Healthy and already recorded as healthy (or never recorded) → nothing to do.
if (!changed && !stale) {return}
if (next.state === 'ok' && !previous?.state) {return}
const status: IntegrationStatus = {
checkedAt: now.toISOString(),
code: next.code ?? null,
message: next.message ?? null,
since: changed ? now.toISOString() : (previous?.since ?? now.toISOString()),
state: next.state,
}
try {
await payload.updateGlobal({
slug: SITE_INTEGRATIONS as never,
context: { ipalIntegrationStatus: true },
data: { [field]: status } as never,
depth: 0,
})
} catch (err) {
console.warn(`[ipal] could not record ${field}:`, err instanceof Error ? err.message : err)
}
}