Cookie scanner, phase 1: declarations, database lookup, review list
ipalKit({ cookieScanner }) (opt-in) adds the cookie-review collection, a
"Scan cookies" box above its list and GET/POST /api/ipal/cookie-scan. A scan
lists the cookies of every integration the project switches on (consent,
admin login, locale, stats, GA4, GTM, Meta Pixel, Clarity, LinkedIn, TikTok,
Turnstile), the project's own declarations, and extra names looked up in the
bundled Open Cookie Database, as drafts. Editors accept (→ Cookie Settings →
Cookies, every locale, only empty text filled), edit or reject. A rescan never
deletes, never changes status and never overwrites edited text.
Everything is overridable (access, collectionOverrides, integrations,
declarations, databaseCategories, labels, syncAccepted) and the building
blocks are exported. Admin labels in en + pl. Docs: cookie-scanner.md,
consent.md, AGENT.md, ipal-reference, ipal-consent-check.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
ab45cf48be
commit
a0545b6716
@@ -0,0 +1,100 @@
|
||||
'use client'
|
||||
|
||||
import { useConfig, useTranslation } from '@payloadcms/ui'
|
||||
import { useRouter } from 'next/navigation.js'
|
||||
import { useEffect, useState } from 'react'
|
||||
|
||||
import type { CookieScanLabels } from './labels.js'
|
||||
import type { CookieScanSummary } from './types.js'
|
||||
|
||||
import { COOKIE_SCAN_LABELS } from './labels.js'
|
||||
|
||||
type Status =
|
||||
| { kind: 'done'; summary: CookieScanSummary }
|
||||
| { kind: 'error' }
|
||||
| { kind: 'idle' }
|
||||
| { kind: 'scanning' }
|
||||
|
||||
/**
|
||||
* Admin UI above the Cookie review list (beforeListTable): shows which
|
||||
* integrations a scan covers, takes optional extra cookie names, runs
|
||||
* POST /api/ipal/cookie-scan and reloads the list. Labels in the admin language
|
||||
* (`labels` from the collection config, English fallback).
|
||||
*/
|
||||
export function CookieScanButton({ labels = COOKIE_SCAN_LABELS }: { labels?: Record<string, CookieScanLabels> }) {
|
||||
const { i18n } = useTranslation()
|
||||
const { config } = useConfig()
|
||||
const router = useRouter()
|
||||
const t = labels[i18n.language] ?? labels.en ?? COOKIE_SCAN_LABELS.en
|
||||
const endpoint = `${config.serverURL ?? ''}${config.routes?.api ?? '/api'}/ipal/cookie-scan`
|
||||
const [names, setNames] = useState('')
|
||||
const [integrations, setIntegrations] = useState<{ key: string; label: string }[] | null>(null)
|
||||
const [status, setStatus] = useState<Status>({ kind: 'idle' })
|
||||
|
||||
useEffect(() => {
|
||||
fetch(endpoint, { credentials: 'include' })
|
||||
.then((r) => (r.ok ? r.json() : null))
|
||||
.then((data: { integrations?: { key: string; label: string }[] } | null) => setIntegrations(data?.integrations ?? []))
|
||||
.catch(() => setIntegrations([]))
|
||||
}, [endpoint])
|
||||
|
||||
const scan = async () => {
|
||||
setStatus({ kind: 'scanning' })
|
||||
try {
|
||||
const res = await fetch(endpoint, {
|
||||
body: JSON.stringify({ names: names.split('\n') }),
|
||||
credentials: 'include',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
method: 'POST',
|
||||
})
|
||||
const data = (await res.json()) as { ok?: boolean; summary?: CookieScanSummary }
|
||||
if (!res.ok || !data.ok || !data.summary) {throw new Error('scan failed')}
|
||||
setStatus({ kind: 'done', summary: data.summary })
|
||||
router.refresh()
|
||||
} catch {
|
||||
setStatus({ kind: 'error' })
|
||||
}
|
||||
}
|
||||
|
||||
const result = (s: CookieScanSummary) =>
|
||||
t.scanResult.replace(/\{(\w+)\}/g, (_, k: string) => String(s[k as keyof CookieScanSummary] ?? ''))
|
||||
|
||||
return (
|
||||
<div className="ipal-cookie-scan" data-ipal="cookie-scan" style={{ marginBottom: '1.5rem' }}>
|
||||
<p style={{ marginTop: 0 }}>{t.scanIntro}</p>
|
||||
{integrations ? (
|
||||
<p style={{ fontSize: '.85rem', opacity: 0.8 }}>
|
||||
{integrations.length ? `${t.integrationsFound}: ${integrations.map((i) => i.label).join(', ')}` : t.integrationsNone}
|
||||
</p>
|
||||
) : null}
|
||||
<label className="field-label" htmlFor="ipal-cookie-scan-names">{t.namesLabel}</label>
|
||||
<p style={{ fontSize: '.85rem', marginTop: 0, opacity: 0.7 }}>{t.namesHint}</p>
|
||||
<textarea
|
||||
aria-label={t.namesLabel}
|
||||
id="ipal-cookie-scan-names"
|
||||
onChange={(e) => setNames(e.target.value)}
|
||||
placeholder={'_hjSessionUser_123\nintercom-id-abc'}
|
||||
rows={3}
|
||||
style={{ fontFamily: 'monospace', marginBottom: '.5rem', width: '100%' }}
|
||||
value={names}
|
||||
/>
|
||||
<div style={{ alignItems: 'center', display: 'flex', flexWrap: 'wrap', gap: '1rem' }}>
|
||||
<button
|
||||
className="btn btn--style-primary"
|
||||
disabled={status.kind === 'scanning'}
|
||||
onClick={scan}
|
||||
style={{ margin: 0 }}
|
||||
type="button"
|
||||
>
|
||||
{status.kind === 'scanning' ? t.scanning : t.scanButton}
|
||||
</button>
|
||||
{status.kind === 'done' ? <span role="status">✓ {result(status.summary)}</span> : null}
|
||||
{status.kind === 'error' ? (
|
||||
<span role="alert" style={{ color: 'var(--theme-error-500, crimson)' }}>✗ {t.scanError}</span>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default CookieScanButton
|
||||
Reference in New Issue
Block a user