- docs/consent.md: compliance defaults, Global Privacy Control, withdrawal, consent log on by default and the SQL migration; no more "UODO requirement" or "GPC not needed". docs/analytics.md: what Consent Mode advanced sends. - docs/consent-behaviour-spec.md: the behaviour IAPL and IAWL share, with the 13-item test list. - CHANGELOG 1.17.0: the SQL migration first, a before → after table for an existing site. - AGENT.md, iapl-setup / iapl-upgrade / iapl-reference / iapl-consent-check, PROGRAMISTA item 78; reference skills copy. - iapl-consent-check: Global Privacy Control, equal prominence and withdrawal runs; Google asset warnings. - Reference site uses the new defaults; e2e for GPC, a stale grant, equal prominence, withdrawal; e2e:built runs every consent test in both bundlers. YouTubeEmbed warns in development without a poster. Co-Authored-By: Claude Opus 5.5 <[email protected]>
99 lines
4.4 KiB
TypeScript
99 lines
4.4 KiB
TypeScript
'use client'
|
|
import { createContext, type ReactNode, use, useMemo } from 'react'
|
|
|
|
import type { ConsentCategory } from './categories.js'
|
|
import type { GpcMode } from './gpc.js'
|
|
import type { ConsentTexts } from './texts.js'
|
|
import type { ConsentCookieMap } from './useConsent.js'
|
|
|
|
import { useConsent } from './useConsent.js'
|
|
|
|
type ConsentContextValue = { nonce?: string; texts: ConsentTexts } & ReturnType<typeof useConsent>
|
|
|
|
const ConsentContext = createContext<ConsentContextValue | null>(null)
|
|
|
|
type ConsentProviderProps = {
|
|
children: ReactNode
|
|
/** Extra cookies per category to delete when consent is withdrawn — merged with the built-in list. */
|
|
cookieMap?: ConsentCookieMap
|
|
/**
|
|
* Optional server read: the consent cookie's raw value from this request
|
|
* (`(await cookies()).get(CONSENT_COOKIE)?.value ?? null`). The HTML then has no banner for a visitor who already
|
|
* decided — and the page is dynamic (no static / ISR copy). Leave out to keep pages static (the default): the
|
|
* banner is in the HTML for everyone and an inline script decides before the first paint.
|
|
*/
|
|
cookieValue?: null | string
|
|
/**
|
|
* Global Privacy Control: `'honour'` (default) — a browser that sends the signal gets no banner, analytics and
|
|
* marketing stay off and nothing is sent to a tracker until the visitor opts in from the cookie button; `'ask'` —
|
|
* the banner is shown, nothing is sent until a choice; `false` — the signal is ignored. A choice made on the site
|
|
* wins over the signal. See docs/consent.md → Global Privacy Control.
|
|
*/
|
|
gpc?: GpcMode
|
|
/**
|
|
* Consent log endpoint (`iapl({ consentLog })`, on by default: '/api/ipal/consent'):
|
|
* every decision is POSTed — random id, choices, policy version, no personal
|
|
* data — so the site can demonstrate consent (GDPR Art. 7(1)). Off when omitted
|
|
* (<SiteProviders> fills it in from the site shell).
|
|
*/
|
|
logEndpoint?: string
|
|
/** Days a choice is kept before the banner asks again. Default 180. */
|
|
maxAgeDays?: number
|
|
/** CSP nonce for the banner's first-paint script (a nonce policy: `(await headers()).get('x-nonce')`). */
|
|
nonce?: null | string
|
|
texts: ConsentTexts
|
|
}
|
|
|
|
/**
|
|
* The panel's cookie list (Cookie Settings → Cookies) per category, split into cookie names and browser-storage keys:
|
|
* withdrawing a category removes them too, next to the built-in list (DEFAULT_COOKIE_MAP) and `cookieMap`.
|
|
*/
|
|
function listedPerCategory(rows: ConsentTexts['cookies']): { cookies: ConsentCookieMap; storage: ConsentCookieMap } {
|
|
const cookies: ConsentCookieMap = {}
|
|
const storage: ConsentCookieMap = {}
|
|
for (const row of rows ?? []) {
|
|
if (row.category === 'necessary' || !row.name) {continue}
|
|
const target = row.storage === 'localStorage' || row.storage === 'sessionStorage' ? storage : cookies
|
|
;(target[row.category as ConsentCategory] ??= []).push(row.name)
|
|
}
|
|
return { cookies, storage }
|
|
}
|
|
|
|
export function ConsentProvider({ children, cookieMap, cookieValue, gpc, logEndpoint, maxAgeDays, nonce, texts }: ConsentProviderProps) {
|
|
// Keyed by content: the texts and the map are often new objects on every render.
|
|
const listKey = JSON.stringify(texts.cookies ?? null)
|
|
const listed = useMemo(() => listedPerCategory(JSON.parse(listKey) as ConsentTexts['cookies']), [listKey])
|
|
const cookieMapKey = JSON.stringify(cookieMap ?? null)
|
|
const mergedCookies = useMemo(() => {
|
|
const out: ConsentCookieMap = { ...listed.cookies }
|
|
const extra = (JSON.parse(cookieMapKey) ?? {}) as ConsentCookieMap
|
|
for (const [cat, names] of Object.entries(extra) as [ConsentCategory, string[]][]) {out[cat] = [...(out[cat] ?? []), ...names]}
|
|
return out
|
|
}, [listed, cookieMapKey])
|
|
const consent = useConsent({
|
|
cookieMap: mergedCookies,
|
|
cookieValue,
|
|
gpc,
|
|
maxAgeDays,
|
|
onDecision: logEndpoint
|
|
? (record) => {
|
|
void fetch(logEndpoint, {
|
|
body: JSON.stringify(record),
|
|
headers: { 'content-type': 'application/json' },
|
|
keepalive: true,
|
|
method: 'POST',
|
|
}).catch(() => {})
|
|
}
|
|
: undefined,
|
|
storageMap: listed.storage,
|
|
version: texts.version,
|
|
})
|
|
return <ConsentContext value={{ ...consent, nonce: nonce ?? undefined, texts }}>{children}</ConsentContext>
|
|
}
|
|
|
|
export function useConsentContext(): ConsentContextValue {
|
|
const ctx = use(ConsentContext)
|
|
if (!ctx) {throw new Error('useConsentContext must be used within ConsentProvider')}
|
|
return ctx
|
|
}
|