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]>
85 lines
3.4 KiB
TypeScript
85 lines
3.4 KiB
TypeScript
import type { ConsentCategory } from '../categories.js'
|
|
import type { CookieFinding, LocalizedText } from './types.js'
|
|
|
|
/** The editable values of a review entry; `purpose` / `retention` per locale. */
|
|
export type CookieReviewValues = {
|
|
category?: ConsentCategory | null
|
|
domain?: null | string
|
|
purpose?: Record<string, null | string>
|
|
retention?: Record<string, null | string>
|
|
setBeforeConsent?: boolean | null
|
|
vendor?: null | string
|
|
}
|
|
|
|
const SCALARS = ['category', 'domain', 'setBeforeConsent', 'vendor'] as const
|
|
const TEXTS = ['purpose', 'retention'] as const
|
|
|
|
/** Text for one locale: that language, else English, else the first one given. */
|
|
export function textFor(text: LocalizedText | undefined, locale: string): string | undefined {
|
|
if (text === undefined) {return undefined}
|
|
if (typeof text === 'string') {return text}
|
|
return text[locale] ?? text.en ?? Object.values(text)[0]
|
|
}
|
|
|
|
/** What a scan suggests for an entry, resolved for the project's locales (default locale first). */
|
|
export function suggestionFor(finding: CookieFinding, locales: string[]): CookieReviewValues {
|
|
const perLocale = (text: LocalizedText | undefined) => {
|
|
if (text === undefined) {return undefined}
|
|
// A locale without its own text gets the default locale's (locales[0]), then English, then any.
|
|
const fallback = typeof text === 'string' ? text : (text[locales[0]] ?? undefined)
|
|
return Object.fromEntries(locales.map((l) => [l, (typeof text === 'string' ? text : text[l]) ?? fallback ?? textFor(text, l) ?? null]))
|
|
}
|
|
const out: CookieReviewValues = {}
|
|
if (finding.category) {out.category = finding.category}
|
|
if (finding.domain) {out.domain = finding.domain}
|
|
if (finding.vendor) {out.vendor = finding.vendor}
|
|
if (typeof finding.setBeforeConsent === 'boolean') {out.setBeforeConsent = finding.setBeforeConsent}
|
|
const purpose = perLocale(finding.purpose)
|
|
const retention = perLocale(finding.retention)
|
|
if (purpose) {out.purpose = purpose}
|
|
if (retention) {out.retention = retention}
|
|
return out
|
|
}
|
|
|
|
const isEmpty = (v: unknown) => v === undefined || v === null || v === ''
|
|
|
|
/**
|
|
* New values for an entry after a scan. A value takes the new suggestion only
|
|
* while it is empty or still equal to what the previous scan suggested — once an
|
|
* editor changed it, it is theirs and stays. Text is compared per locale.
|
|
*/
|
|
export function mergeCookieReview(
|
|
current: CookieReviewValues,
|
|
previous: CookieReviewValues | null | undefined,
|
|
next: CookieReviewValues,
|
|
): { changed: boolean; values: CookieReviewValues } {
|
|
const prev = previous ?? {}
|
|
const values: CookieReviewValues = { ...current }
|
|
let changed = false
|
|
const pick = <V>(cur: V, before: undefined | V, suggested: undefined | V): V => {
|
|
if (suggested === undefined || isEmpty(suggested)) {return cur}
|
|
if (isEmpty(cur) || cur === before) {return suggested}
|
|
return cur
|
|
}
|
|
for (const key of SCALARS) {
|
|
const v = pick(current[key], prev[key], next[key])
|
|
if (v !== current[key]) {
|
|
;(values as Record<string, unknown>)[key] = v
|
|
changed = true
|
|
}
|
|
}
|
|
for (const key of TEXTS) {
|
|
const cur = current[key] ?? {}
|
|
const merged = { ...cur }
|
|
for (const [locale, suggested] of Object.entries(next[key] ?? {})) {
|
|
const v = pick(cur[locale], prev[key]?.[locale], suggested)
|
|
if (v !== cur[locale]) {
|
|
merged[locale] = v ?? null
|
|
changed = true
|
|
}
|
|
}
|
|
if (Object.keys(merged).length) {values[key] = merged}
|
|
}
|
|
return { changed, values }
|
|
}
|