'use client' import { useConfig, useTranslation } from '@payloadcms/ui' import { useCallback, useEffect, useState } from 'react' import type { CrawlViolation } from './crawl/violations.js' import type { CookieScanLabels } from './labels.js' import { CookieScanButton } from './CookieScanButton.js' import { COOKIE_SCAN_LABELS } from './labels.js' /** * "Site check" tab of the Cookies global (`ui` field, combined admin layout, * only with `cookieScanner.crawl`): start a check, its status, the violations * of the latest check, and a link to the warning e-mail settings. */ export function CookieSiteCheckTab({ labels = COOKIE_SCAN_LABELS }: { labels?: Record }) { const { i18n } = useTranslation() const { config } = useConfig() const t = labels[i18n.language] ?? labels.en ?? COOKIE_SCAN_LABELS.en const admin = (config.routes?.admin ?? '/admin').replace(/\/$/, '') const endpoint = `${config.serverURL ?? ''}${config.routes?.api ?? '/api'}/globals/cookie-crawl?depth=0` const [violations, setViolations] = useState(null) const load = useCallback(() => { fetch(endpoint, { credentials: 'include' }) .then((r) => (r.ok ? r.json() : null)) .then((data: { violations?: CrawlViolation[] | null } | null) => setViolations(Array.isArray(data?.violations) ? data.violations : [])) .catch(() => setViolations([])) }, [endpoint]) useEffect(load, [load]) const kind = (v: CrawlViolation) => (v.kind === 'request' ? t.request : v.kind === 'cookie' ? t.cookie : v.kind === 'localStorage' ? t.localStorage : t.sessionStorage) return (

{t.violationsTitle}

{violations === null ?

{t.loadingList}

: null} {violations?.length === 0 ?

{t.noViolations}

: null} {violations?.length ? ( {violations.map((v) => ( ))}
{t.name} {t.kind} {t.category} {t.pages}
{v.name} {kind(v)} {v.category && v.category in t ? t[v.category as keyof CookieScanLabels] : (v.category ?? '')} {v.pages.join(', ')}
) : null}

{t.warningEmailsLink}

) } export default CookieSiteCheckTab