'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 const ConsentContext = createContext(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 * ( 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 {children} } export function useConsentContext(): ConsentContextValue { const ctx = use(ConsentContext) if (!ctx) {throw new Error('useConsentContext must be used within ConsentProvider')} return ctx }