Every language filled (fillEmptyLocales, default on): text saved in one language goes to every language where the same field is still empty, never over a language's own text. Applies to the cookie list (purpose, duration) and the cookie review (purpose, duration). Scanner and approval writes give a language without its own text the default language's text. Building blocks: buildFillEmptyLocalesGlobalHook / CollectionHook, fillEmptyLocales, withFillLocales. One Cookies entry (cookieScanner.adminLayout, default combined): the Cookie banner global is now labelled Cookies and gets Review and approve (scan box and approval list inline) and Site check tabs, built from ui fields. Cookie review and Cookie check leave the navigation (admin.group false) but keep their pages, data and API. This is presentation only: no schema change and no migration, only generate:importmap. adminLayout separate keeps the old layout. Co-Authored-By: Claude Opus 5.5 <[email protected]>
71 lines
3.0 KiB
TypeScript
71 lines
3.0 KiB
TypeScript
'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<string, CookieScanLabels> }) {
|
|
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<CrawlViolation[] | null>(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 (
|
|
<div data-ipal="cookies-site-check-tab" style={{ marginTop: '1rem' }}>
|
|
<CookieScanButton crawl labels={labels} onChange={load} scan={false} />
|
|
<h3 style={{ marginTop: '1.5rem' }}>{t.violationsTitle}</h3>
|
|
{violations === null ? <p>{t.loadingList}</p> : null}
|
|
{violations?.length === 0 ? <p>{t.noViolations}</p> : null}
|
|
{violations?.length ? (
|
|
<table style={{ borderCollapse: 'collapse', width: '100%' }}>
|
|
<thead>
|
|
<tr>
|
|
<th scope="col" style={{ textAlign: 'left' }}>{t.name}</th>
|
|
<th scope="col" style={{ textAlign: 'left' }}>{t.kind}</th>
|
|
<th scope="col" style={{ textAlign: 'left' }}>{t.category}</th>
|
|
<th scope="col" style={{ textAlign: 'left' }}>{t.pages}</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{violations.map((v) => (
|
|
<tr key={`${v.kind}:${v.name}`}>
|
|
<td><code>{v.name}</code></td>
|
|
<td>{kind(v)}</td>
|
|
<td>{v.category && v.category in t ? t[v.category as keyof CookieScanLabels] : (v.category ?? '')}</td>
|
|
<td>{v.pages.join(', ')}</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
) : null}
|
|
<p style={{ marginTop: '1.5rem' }}>
|
|
<a href={`${admin}/globals/cookie-crawl`}>{t.warningEmailsLink}</a>
|
|
</p>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default CookieSiteCheckTab
|