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 payload?: Payload } type CrawlDoc = { error?: null | string; status?: null | string; summary?: { newUnknown?: null | number; violations?: null | number } | null } const fill = (template: string, values: Record) => 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 (
{failed ?

{fill(t('alertFailed'), { error: doc.error ?? '' })}

: null} {findings ? ( <>

{fill(t('alertTitle'), { newUnknown, violations })}

{t('alertBody')}

{t('alertLink')} ) : null} {pending ? (

{fill(t('approvalPending'), { count: pending })}{' '} {t('approvalLink')}

) : null} {failed && !findings && !pending ? ( {t('alertLink')} ) : null}
) } export default CookieCrawlAlert