Files
ipal-kit/src/modules/consent/scanner/crawl/CookieCrawlAlert.tsx
T
Radosław SmolińskiandClaude Opus 5.5 86ff3f5ff7 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]>
2026-10-28 22:17:46 +01:00

91 lines
3.6 KiB
TypeScript

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): 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', approval = true, crawl = true, i18n, labels = COOKIE_SCAN_LABELS, payload }: Props) {
if (!payload) {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'
// 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
data-ipal="cookie-crawl-alert"
role="alert"
style={{
background: 'var(--theme-warning-50, rgba(255, 178, 36, 0.1))',
border: '1px solid var(--theme-warning-400, #ffb224)',
borderRadius: 4,
marginBottom: '1.5rem',
padding: '1rem 1.25rem',
}}
>
{failed ? <p style={{ margin: 0 }}><strong>{fill(t('alertFailed'), { error: doc.error ?? '' })}</strong></p> : null}
{findings ? (
<>
<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>
</>
) : 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>
)
}
export default CookieCrawlAlert