Instruction clarification
This commit is contained in:
@@ -3,6 +3,7 @@ import type { BasePayload } from 'payload'
|
||||
import type { I18nConfig } from '../i18n/index.js'
|
||||
import type { ConsentTexts } from './texts.js'
|
||||
|
||||
import { resolveLocalized } from '../i18n/resolveLocalized.js'
|
||||
import { getSystemPagePath } from '../pages/index.js'
|
||||
import { getGlobal } from '../payload/index.js'
|
||||
import { CONSENT_CATEGORIES } from './categories.js'
|
||||
@@ -32,37 +33,72 @@ const FALLBACK: ConsentTexts = {
|
||||
settingsTitle: 'Cookie settings',
|
||||
}
|
||||
|
||||
/** Last-resort link text — only when neither the panel nor the page has one. */
|
||||
const FALLBACK_PRIVACY_LABEL = 'Privacy policy'
|
||||
|
||||
type CookieSettingsData = {
|
||||
buttons?: null | Partial<ConsentTexts['buttons']>
|
||||
categories?: Array<{ description?: null | string; key: string; title?: null | string }> | null
|
||||
message?: null | string
|
||||
privacyLinkLabel?: null | string
|
||||
settingsTitle?: null | string
|
||||
}
|
||||
|
||||
type PrivacyPage = { slug?: null | Record<string, unknown>; title?: unknown } | null
|
||||
|
||||
type GetConsentTextsArgs = {
|
||||
config: I18nConfig
|
||||
locale?: string
|
||||
payload: BasePayload
|
||||
/**
|
||||
* Privacy-policy label + the system page (from SiteSettings.privacyPolicy,
|
||||
* read with locale:'all') to link to. Optional — omit for no link.
|
||||
* Privacy-policy link. Omit it — the page is read automatically from
|
||||
* SiteSettings → System Pages → Privacy Policy, and the link text from
|
||||
* CookieSettings → privacyLinkLabel (per locale, editable in the panel).
|
||||
*
|
||||
* - `false` → no link.
|
||||
* - `{ page }` → use this page (SiteSettings.privacyPolicy read with locale:'all').
|
||||
* - `label` → DEPRECATED (hardcoded text); used only when the panel field is empty.
|
||||
*/
|
||||
privacyPolicy?: {
|
||||
label: string
|
||||
page: { slug?: null | Record<string, unknown> } | null
|
||||
privacyPolicy?:
|
||||
| {
|
||||
/** @deprecated Set CookieSettings → privacyLinkLabel in the panel instead. */
|
||||
label?: string
|
||||
page?: PrivacyPage
|
||||
}
|
||||
| false
|
||||
/** SiteSettings global slug for the automatic privacy page lookup. Default 'site-settings'. */
|
||||
settingsSlug?: string
|
||||
}
|
||||
|
||||
/** Reads SiteSettings.privacyPolicy with all locales (slug + title as maps). */
|
||||
async function readPrivacyPage(payload: BasePayload, settingsSlug: string): Promise<PrivacyPage> {
|
||||
try {
|
||||
const settings = (await payload.findGlobal({
|
||||
slug: settingsSlug as never,
|
||||
depth: 1,
|
||||
locale: 'all' as never,
|
||||
})) as { privacyPolicy?: unknown }
|
||||
const page = settings.privacyPolicy
|
||||
return page && typeof page === 'object' ? (page as PrivacyPage) : null
|
||||
} catch (error) {
|
||||
// No pages option / no privacyPolicy field / DB unreachable at build → no link.
|
||||
console.warn('[ipal] getConsentTexts: privacy page lookup failed, banner without link:', error)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves consent banner texts from the CookieSettings global, falling back to
|
||||
* English defaults per field. The privacy-policy link is built from the pages
|
||||
* module (system page role), not stored in CookieSettings — one source of truth.
|
||||
* English defaults per field. The privacy-policy link target comes from the
|
||||
* pages module (system page role, read automatically); its text from
|
||||
* CookieSettings.privacyLinkLabel — both editable in the panel, per locale.
|
||||
*/
|
||||
export async function getConsentTexts({
|
||||
config,
|
||||
locale,
|
||||
payload,
|
||||
privacyPolicy,
|
||||
settingsSlug = 'site-settings',
|
||||
}: GetConsentTextsArgs): Promise<ConsentTexts> {
|
||||
const g = await getGlobal<CookieSettingsData>(payload, 'cookie-settings', { locale })
|
||||
|
||||
@@ -76,10 +112,20 @@ export async function getConsentTexts({
|
||||
}
|
||||
|
||||
let privacyLink: ConsentTexts['privacyLink'] = null
|
||||
if (privacyPolicy?.page && locale) {
|
||||
const href = getSystemPagePath({ config, locale, page: privacyPolicy.page })
|
||||
if (href) {
|
||||
privacyLink = { href, label: privacyPolicy.label }
|
||||
if (privacyPolicy !== false && locale) {
|
||||
const page =
|
||||
privacyPolicy?.page !== undefined
|
||||
? privacyPolicy.page
|
||||
: await readPrivacyPage(payload, settingsSlug)
|
||||
const href = page ? getSystemPagePath({ config, locale, page }) : undefined
|
||||
if (page && href) {
|
||||
// Panel first, then the deprecated code label, then the page's own title.
|
||||
const label =
|
||||
g.privacyLinkLabel?.trim() ||
|
||||
privacyPolicy?.label?.trim() ||
|
||||
resolveLocalized(page.title, locale).trim() ||
|
||||
FALLBACK_PRIVACY_LABEL
|
||||
privacyLink = { href, label }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user