Fixed turnstile
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
import type { Payload } from 'payload'
|
||||
|
||||
import type { IntegrationStatus } from './status.js'
|
||||
|
||||
type Props = {
|
||||
/** Admin route, for the link. Default '/admin'. */
|
||||
adminRoute?: string
|
||||
/** Status groups to check: field name → label. */
|
||||
checks?: Record<string, string>
|
||||
/** Injected by Payload into admin server components. */
|
||||
payload?: Payload
|
||||
}
|
||||
|
||||
/**
|
||||
* 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).
|
||||
*/
|
||||
export async function IntegrationAlerts({ adminRoute = '/admin', checks = { turnstileStatus: 'Turnstile' }, payload }: Props) {
|
||||
if (!payload) {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 failing = Object.entries(checks)
|
||||
.map(([field, label]) => ({ label, status: doc?.[field] as IntegrationStatus | undefined }))
|
||||
.filter((x) => x.status?.state === 'degraded')
|
||||
if (!failing.length) {return null}
|
||||
|
||||
return (
|
||||
<div
|
||||
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>Integration problem — the site still works, but a safeguard is off.</strong>
|
||||
<ul style={{ margin: '0.5rem 0 0', paddingLeft: '1.2rem' }}>
|
||||
{failing.map(({ label, status }) => (
|
||||
<li key={label}>
|
||||
<strong>{label}:</strong> {status?.message ?? status?.code ?? 'error'}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
<a href={`${adminRoute.replace(/\/$/, '')}/globals/site-integrations`} style={{ display: 'inline-block', marginTop: '0.5rem' }}>
|
||||
Open Site Integrations →
|
||||
</a>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default IntegrationAlerts
|
||||
@@ -0,0 +1,64 @@
|
||||
'use client'
|
||||
import { useFormFields } from '@payloadcms/ui'
|
||||
|
||||
type Props = {
|
||||
/** Integration name shown in the banner, e.g. 'Turnstile'. */
|
||||
label: string
|
||||
/** Name of the hidden status group (buildIntegrationStatusField). */
|
||||
statusField: string
|
||||
}
|
||||
|
||||
const fmt = (iso: unknown) => {
|
||||
if (typeof iso !== 'string' || !iso) {return ''}
|
||||
const d = new Date(iso)
|
||||
return Number.isNaN(d.getTime()) ? '' : d.toLocaleString()
|
||||
}
|
||||
|
||||
/**
|
||||
* Status banner on an integration's tab (Site Integrations). Shows nothing until
|
||||
* the integration has been used once; a red box while it's failing; a short
|
||||
* green line once it works again.
|
||||
*/
|
||||
export function IntegrationStatusBanner({ label, statusField }: Props) {
|
||||
const state = useFormFields(([f]) => f[`${statusField}.state`]?.value) as string | undefined
|
||||
const message = useFormFields(([f]) => f[`${statusField}.message`]?.value) as string | undefined
|
||||
const code = useFormFields(([f]) => f[`${statusField}.code`]?.value) as string | undefined
|
||||
const since = useFormFields(([f]) => f[`${statusField}.since`]?.value)
|
||||
const checkedAt = useFormFields(([f]) => f[`${statusField}.checkedAt`]?.value)
|
||||
|
||||
if (!state) {return null}
|
||||
|
||||
if (state === 'ok') {
|
||||
return (
|
||||
<div style={{ fontSize: '0.85rem', marginBottom: '1.5rem', opacity: 0.75 }}>
|
||||
✓ {label} working{since ? ` since ${fmt(since)}` : ''}.
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
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: '0.9rem 1rem',
|
||||
}}
|
||||
>
|
||||
<strong>{label} is not working — forms are accepted without this check.</strong>
|
||||
{message ? <div style={{ marginTop: '0.35rem' }}>{message}</div> : null}
|
||||
<div style={{ fontSize: '0.8rem', marginTop: '0.35rem', opacity: 0.75 }}>
|
||||
{code ? `Code: ${code}. ` : ''}
|
||||
{since ? `Since ${fmt(since)}. ` : ''}
|
||||
{checkedAt ? `Last seen ${fmt(checkedAt)}.` : ''}
|
||||
</div>
|
||||
<div style={{ fontSize: '0.8rem', marginTop: '0.35rem', opacity: 0.75 }}>
|
||||
Fix the keys below and save — the status is checked again on the next form submission.
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default IntegrationStatusBanner
|
||||
@@ -0,0 +1,6 @@
|
||||
export {
|
||||
buildIntegrationStatusBanner,
|
||||
buildIntegrationStatusField,
|
||||
recordIntegrationStatus,
|
||||
} from './status.js'
|
||||
export type { IntegrationState, IntegrationStatus } from './status.js'
|
||||
@@ -0,0 +1,101 @@
|
||||
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/client#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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user