Files
ipal-kit/src/modules/consent/scanner/CookieApprovalView.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

78 lines
3.0 KiB
TypeScript

import type { AdminViewServerProps, PayloadRequest } from 'payload'
import { DefaultTemplate } from '@payloadcms/next/templates'
import { Gutter } from '@payloadcms/ui'
import type { ConsentCategory } from '../categories.js'
import type { CookieScanLabels } from './labels.js'
import { DocumentTitle } from '../../admin-i18n/DocumentTitle.js'
import { adminDocumentTitle } from '../../admin-i18n/t.js'
import { DEFAULT_UNKNOWN_CATEGORY, loadCookieApproval } from './approval.js'
import { COOKIE_REVIEW_SLUG } from './collection.js'
import { CookieApprovalForm } from './CookieApprovalForm.js'
import { COOKIE_SCAN_LABELS, labelFor } from './labels.js'
type Props = {
adminRoute?: string
labels?: Record<string, CookieScanLabels>
unknownCategory?: ConsentCategory
} & AdminViewServerProps
/** Whether the user may read the review list — its own `access.read` decides (collectionOverrides narrow it). */
async function canReview(req: PayloadRequest): Promise<boolean> {
const read = req.payload.collections[COOKIE_REVIEW_SLUG as never]?.config.access?.read
if (!read || !req.user) {return false}
try {
return Boolean(await read({ req } as never))
} catch {
return false
}
}
/**
* Admin view at /admin/cookie-approval (`cookieScanner.approval`, on by
* default): everything the latest scans found, grouped by consent category,
* with one "Approve and add" button. Texts in the content locale that matches
* the admin language, else the default locale.
*/
export async function CookieApprovalView(props: Props) {
const { adminRoute = '/admin', initPageResult, labels = COOKIE_SCAN_LABELS, searchParams, unknownCategory = DEFAULT_UNKNOWN_CATEGORY } = props
const { req, visibleEntities } = initPageResult
const lang = req.i18n.language
const t = (key: keyof CookieScanLabels) => labelFor(labels, lang, key)
const l = req.payload.config.localization
const locale = l ? (l.localeCodes.includes(lang) ? lang : l.defaultLocale) : 'en'
const allowed = await canReview(req)
const list = allowed ? await loadCookieApproval({ locale, payload: req.payload, req }) : null
return (
<DefaultTemplate
i18n={req.i18n}
locale={initPageResult.locale}
params={props.params}
payload={req.payload}
permissions={initPageResult.permissions}
req={req}
searchParams={searchParams}
user={req.user ?? undefined}
visibleEntities={visibleEntities}
>
<Gutter>
<div data-ipal="cookie-approval" style={{ paddingBottom: '3rem' }}>
<DocumentTitle title={adminDocumentTitle(t('approvalTitle'), req.payload.config.admin?.meta?.titleSuffix)} />
<h1>{t('approvalTitle')}</h1>
{list ? (
<CookieApprovalForm adminRoute={adminRoute} gone={list.gone} labels={labels} pending={list.pending} unknownCategory={unknownCategory} />
) : (
<p>{t('approvalNoAccess')}</p>
)}
</div>
</Gutter>
</DefaultTemplate>
)
}
export default CookieApprovalView