100 lines
4.1 KiB
JavaScript
100 lines
4.1 KiB
JavaScript
import { resolveLocalized } from '../i18n/resolveLocalized.js';
|
|
import { getSystemPagePath } from '../pages/index.js';
|
|
import { getGlobal } from '../payload/index.js';
|
|
import { CONSENT_CATEGORIES } from './categories.js';
|
|
/** English fallback used when the global has no value for a field. */ const FALLBACK = {
|
|
buttons: {
|
|
acceptAll: 'Accept all',
|
|
back: 'Back',
|
|
reject: 'Reject',
|
|
save: 'Save preferences',
|
|
settings: 'Settings'
|
|
},
|
|
categories: {
|
|
analytics: {
|
|
description: 'Help us understand usage.',
|
|
title: 'Analytics'
|
|
},
|
|
functional: {
|
|
description: 'Remember preferences.',
|
|
title: 'Functional'
|
|
},
|
|
marketing: {
|
|
description: 'Used to show relevant ads.',
|
|
title: 'Marketing'
|
|
},
|
|
necessary: {
|
|
description: 'Required for the site to work, including your language and theme preferences. Always on.',
|
|
title: 'Necessary'
|
|
}
|
|
},
|
|
message: 'We use cookies to run the site, analyze traffic, and improve your experience. You can accept all, reject non-essential, or choose which to allow.',
|
|
privacyLink: null,
|
|
settingsTitle: 'Cookie settings'
|
|
};
|
|
/** Last-resort link text — only when neither the panel nor the page has one. */ const FALLBACK_PRIVACY_LABEL = 'Privacy policy';
|
|
/** Reads SiteSettings.privacyPolicy with all locales (slug + title as maps). */ async function readPrivacyPage(payload, settingsSlug) {
|
|
try {
|
|
const settings = await payload.findGlobal({
|
|
slug: settingsSlug,
|
|
depth: 1,
|
|
locale: 'all'
|
|
});
|
|
const page = settings.privacyPolicy;
|
|
return page && typeof page === 'object' ? page : 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 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' }) {
|
|
const g = await getGlobal(payload, 'cookie-settings', {
|
|
locale
|
|
});
|
|
const categories = {};
|
|
for (const cat of CONSENT_CATEGORIES){
|
|
const entry = g.categories?.find((c)=>c.key === cat);
|
|
categories[cat] = {
|
|
description: entry?.description || FALLBACK.categories[cat].description,
|
|
title: entry?.title || FALLBACK.categories[cat].title
|
|
};
|
|
}
|
|
let privacyLink = null;
|
|
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
|
|
};
|
|
}
|
|
}
|
|
return {
|
|
buttons: {
|
|
acceptAll: g.buttons?.acceptAll || FALLBACK.buttons.acceptAll,
|
|
back: g.buttons?.back || FALLBACK.buttons.back,
|
|
reject: g.buttons?.reject || FALLBACK.buttons.reject,
|
|
save: g.buttons?.save || FALLBACK.buttons.save,
|
|
settings: g.buttons?.settings || FALLBACK.buttons.settings
|
|
},
|
|
categories,
|
|
message: g.message || FALLBACK.message,
|
|
privacyLink,
|
|
settingsTitle: g.settingsTitle || FALLBACK.settingsTitle
|
|
};
|
|
}
|
|
|
|
//# sourceMappingURL=getConsentTexts.js.map
|