Cookie scanner: one approval for everything a scan finds
After "Scan cookies" or a site check, the new Approve cookies view (/admin/cookie-approval) lists what waits, grouped by consent category, with provider, purpose and duration and New / Changed / Needs description markers. One "Approve and add" puts every ticked cookie into Cookie banner -> Cookie list in every locale. That list feeds the cookie policy table and the detailed settings window. - Never overwrites editor text or categories, never adds a second row (a pattern and a concrete name are the same cookie), never deletes. - Unknown cookies are approved into a category that needs consent (approval.unknownCategory, default marketing) and flagged Needs description; writing a purpose clears the flag. - Cookies a scan or crawl no longer finds are flagged No longer found, for review only. - The dashboard and the crawl e-mail say how many cookies wait and link to the view; nothing is added without approval. - Optional: cookieScanner.approval false keeps per-entry review. - Migration: cookie-review gains change and needsDescription. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
0e7966dfbb
commit
86ff3f5ff7
@@ -2,41 +2,53 @@ import type { Payload } from 'payload'
|
||||
|
||||
import type { CookieScanLabels } from '../labels.js'
|
||||
|
||||
import { COOKIE_APPROVAL_VIEW_PATH, countPendingCookies } from '../approval.js'
|
||||
import { COOKIE_REVIEW_SLUG } from '../collection.js'
|
||||
import { COOKIE_SCAN_LABELS, labelFor } from '../labels.js'
|
||||
import { COOKIE_CRAWL_SLUG } from './config.js'
|
||||
|
||||
type Props = {
|
||||
adminRoute?: string
|
||||
/** Cookie approval is on: remind while cookies wait, link to the approval view. Default true. */
|
||||
approval?: boolean
|
||||
/** The browser crawl is on: report violations and failed crawls. Default true. */
|
||||
crawl?: boolean
|
||||
/** Injected by Payload into admin server components. */
|
||||
i18n?: { language?: string }
|
||||
labels?: Record<string, CookieScanLabels>
|
||||
payload?: Payload
|
||||
}
|
||||
|
||||
type CrawlDoc = { error?: null | string; status?: null | string; summary?: { newUnknown?: null | number; violations?: null | number } | null }
|
||||
|
||||
const fill = (template: string, values: Record<string, number | string>) =>
|
||||
template.replace(/\{(\w+)\}/g, (_, k: string) => String(values[k] ?? ''))
|
||||
|
||||
/**
|
||||
* Dashboard warning (beforeDashboard) after the latest cookie crawl found
|
||||
* violations or new unknown cookies, or failed. Renders nothing otherwise and
|
||||
* never throws.
|
||||
* Dashboard warning (beforeDashboard): cookies waiting for approval (with a
|
||||
* link to the approval view), and — with the crawl — violations or a failed
|
||||
* crawl. Renders nothing when nothing needs attention, and never throws.
|
||||
*/
|
||||
export async function CookieCrawlAlert({ adminRoute = '/admin', i18n, labels = COOKIE_SCAN_LABELS, payload }: Props) {
|
||||
export async function CookieCrawlAlert({ adminRoute = '/admin', approval = true, crawl = true, i18n, labels = COOKIE_SCAN_LABELS, payload }: Props) {
|
||||
if (!payload) {return null}
|
||||
let doc: { error?: null | string; status?: null | string; summary?: { newUnknown?: null | number; violations?: null | number } | null }
|
||||
try {
|
||||
doc = (await payload.findGlobal({ slug: COOKIE_CRAWL_SLUG as never, depth: 0 })) as typeof doc
|
||||
} catch {
|
||||
return null
|
||||
let doc: CrawlDoc = {}
|
||||
if (crawl) {
|
||||
try {
|
||||
doc = (await payload.findGlobal({ slug: COOKIE_CRAWL_SLUG as never, depth: 0 })) as CrawlDoc
|
||||
} catch {
|
||||
doc = {}
|
||||
}
|
||||
}
|
||||
const pending = approval ? await countPendingCookies(payload) : 0
|
||||
const t = (key: keyof CookieScanLabels) => labelFor(labels, i18n?.language, key)
|
||||
const violations = doc.summary?.violations ?? 0
|
||||
const newUnknown = doc.summary?.newUnknown ?? 0
|
||||
const failed = doc.status === 'failed'
|
||||
if (!failed && !(doc.status === 'done' && (violations || newUnknown))) {return null}
|
||||
const base = `${adminRoute.replace(/\/$/, '')}/collections/${COOKIE_REVIEW_SLUG}`
|
||||
const href = violations ? `${base}?where[violation][equals]=true` : base
|
||||
// Without approval, new unknown cookies are the reason to look; with it, the pending count says so.
|
||||
const findings = doc.status === 'done' && (violations > 0 || (!approval && newUnknown > 0))
|
||||
if (!failed && !findings && !pending) {return null}
|
||||
const admin = adminRoute.replace(/\/$/, '')
|
||||
const review = `${admin}/collections/${COOKIE_REVIEW_SLUG}`
|
||||
|
||||
return (
|
||||
<div
|
||||
@@ -50,17 +62,27 @@ export async function CookieCrawlAlert({ adminRoute = '/admin', i18n, labels = C
|
||||
padding: '1rem 1.25rem',
|
||||
}}
|
||||
>
|
||||
{failed ? (
|
||||
<strong>{fill(t('alertFailed'), { error: doc.error ?? '' })}</strong>
|
||||
) : (
|
||||
{failed ? <p style={{ margin: 0 }}><strong>{fill(t('alertFailed'), { error: doc.error ?? '' })}</strong></p> : null}
|
||||
{findings ? (
|
||||
<>
|
||||
<strong>{fill(t('alertTitle'), { newUnknown, violations })}</strong>
|
||||
<p style={{ margin: 0 }}><strong>{fill(t('alertTitle'), { newUnknown, violations })}</strong></p>
|
||||
<p style={{ margin: '0.5rem 0 0' }}>{t('alertBody')}</p>
|
||||
<a href={violations ? `${review}?where[violation][equals]=true` : review} style={{ display: 'block', marginTop: '0.5rem' }}>
|
||||
{t('alertLink')}
|
||||
</a>
|
||||
</>
|
||||
)}
|
||||
<a href={href} style={{ display: 'block', marginTop: '0.5rem' }}>
|
||||
{t('alertLink')}
|
||||
</a>
|
||||
) : null}
|
||||
{pending ? (
|
||||
<p style={{ margin: failed || findings ? '0.75rem 0 0' : 0 }}>
|
||||
<strong>{fill(t('approvalPending'), { count: pending })}</strong>{' '}
|
||||
<a href={`${admin}${COOKIE_APPROVAL_VIEW_PATH}`}>{t('approvalLink')}</a>
|
||||
</p>
|
||||
) : null}
|
||||
{failed && !findings && !pending ? (
|
||||
<a href={review} style={{ display: 'block', marginTop: '0.5rem' }}>
|
||||
{t('alertLink')}
|
||||
</a>
|
||||
) : null}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user