@trustarc/react-cookie-consent-manager
Version:
React components to integrate TrustArc Cookie Consent Manager into React Frameworks
159 lines (158 loc) • 8.01 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import { useEffect } from "react";
/**
* Enum representing the different versions of TrustArc.
*
* @enum {string}
* @property {string} Pro - Represents the "pro" version of TrustArc.
* @property {string} Advanced - Represents the "advanced" version of TrustArc.
*/
var TrustArcVersion;
(function (TrustArcVersion) {
TrustArcVersion["Pro"] = "pro";
TrustArcVersion["Advanced"] = "advanced";
})(TrustArcVersion || (TrustArcVersion = {}));
/**
* Enum representing the different TrustArc banner IDs.
*
* @enum {string}
* @property {string} consent-banner - Represents the banner container ID for the "pro" version of TrustArc.
* @property {string} consent_blackbar - Represents the banner container ID for the "advanced" version of TrustArc.
*/
var TrustArcBannerID;
(function (TrustArcBannerID) {
TrustArcBannerID["Pro"] = "consent-banner";
TrustArcBannerID["Advanced"] = "consent_blackbar";
})(TrustArcBannerID || (TrustArcBannerID = {}));
export const createTrustArcComponents = (CMPID, options = {}) => {
const logDebug = (...args) => {
if (debug) {
console.log(...args);
}
};
const { cookiePreferencesContainer = "teconsent", showCookiePreferencesAsButton = false, irmContainer = "", ccmVersion = TrustArcVersion.Advanced, js = "nj", noticeType = "bb", countryCodeorIP = "", state = "", language = "", behavior = "", pcookie = true, onclose = false, ostype = false, fade = 0, px = 0, pn = "", debug = false, privacyPolicyLink = "", cookiePolicyLink = "", loadScriptAsync = true, } = options;
const bannerId = (ccmVersion === TrustArcVersion.Pro ? TrustArcBannerID.Pro : TrustArcBannerID.Advanced);
const TrustArcScript = () => {
useEffect(() => {
if (document.querySelector('script[data-trustarc="true"]')) {
logDebug("TrustArc script already exists, skipping injection.");
return;
}
const script = document.createElement("script");
if (loadScriptAsync)
script.async = true;
script.src =
ccmVersion === TrustArcVersion.Pro
? `https://consent.trustarc.com/v2/notice/${CMPID}` +
(language || pcookie || countryCodeorIP || privacyPolicyLink || cookiePolicyLink ? "?" : "") +
(language ? `&locale=${language}` : "") +
(pcookie ? `&pcookie` : "") +
(countryCodeorIP ? `&ipaddress=${countryCodeorIP}` : "") +
(privacyPolicyLink ? `&privacyPolicyLink=${encodeURIComponent(privacyPolicyLink)}` : "") +
(cookiePolicyLink ? `&cookieLink=${encodeURIComponent(cookiePolicyLink)}` : "")
: `https://consent.trustarc.com/notice?domain=${CMPID}>m=1&c=${cookiePreferencesContainer}&js=${js}¬iceType=${noticeType}` +
(countryCodeorIP ? `&country=${countryCodeorIP}` : "") +
(state ? `&state=${state}` : "") +
(!showCookiePreferencesAsButton ? `&text=true` : "") +
(pcookie ? `&pcookie` : "") +
(language ? `&language=${language}` : "") +
(behavior ? `&behavior=${behavior}` : "") +
(irmContainer ? `&irmc=${irmContainer}` : "") +
(pn ? `&pn=${pn}` : "") +
(fade > 0 ? `&fade=${fade}` : "") +
(px > 0 ? `&px=${px}` : "") +
(onclose ? `&oc=1}` : "") +
(ostype ? `&ostype=mobile` : "") +
(privacyPolicyLink ? `&privacypolicylink=${encodeURIComponent(privacyPolicyLink)}` : "") +
(cookiePolicyLink ? `&cookieLink=${encodeURIComponent(cookiePolicyLink)}` : "");
script.setAttribute("data-trustarc", "true");
document.head.prepend(script);
script.onload = () => {
logDebug("TrustArc script loaded.");
logDebug("window.truste object:", window.truste);
};
return () => {
logDebug("TrustArcScript component unmounted, but script remains.");
};
}, []);
return null;
};
const repopulateDiv = (divId) => {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
if (divId == "") {
return;
}
const element = document.getElementById(divId);
if (!element) {
logDebug(`Div ${divId} not found, skipping update.`);
return;
}
if (element.childNodes.length === 0) {
logDebug(`Repopulating empty div: ${divId}`);
switch (divId) {
case bannerId:
//window?.truste?.bn?.reopenBanner();
(_b = (_a = window === null || window === void 0 ? void 0 : window.truste) === null || _a === void 0 ? void 0 : _a.bn) === null || _b === void 0 ? void 0 : _b.bannerMain();
logDebug("Called window.truste.bn.bannerMain() for consent banner.");
break;
case cookiePreferencesContainer:
(_e = (_d = (_c = window === null || window === void 0 ? void 0 : window.truste) === null || _c === void 0 ? void 0 : _c.eu) === null || _d === void 0 ? void 0 : _d.icon) === null || _e === void 0 ? void 0 : _e.initialize();
logDebug("Called window.truste.eu.icon.initialize() for cookie preferences link.");
break;
case irmContainer:
{
const irmLink = (_g = (_f = window === null || window === void 0 ? void 0 : window.truste) === null || _f === void 0 ? void 0 : _f.eu) === null || _g === void 0 ? void 0 : _g.irmLink;
const irmLinkContainer = (_j = (_h = window === null || window === void 0 ? void 0 : window.truste) === null || _h === void 0 ? void 0 : _h.eu) === null || _j === void 0 ? void 0 : _j.irmLinkContainer;
if (irmLink && irmLinkContainer) {
(_k = element.parentElement) === null || _k === void 0 ? void 0 : _k.insertBefore(irmLinkContainer, element.nextSibling);
irmLinkContainer.appendChild(irmLink);
element.remove();
}
}
break;
default:
logDebug(`No specific repopulation function for div: ${divId}`);
}
}
else {
logDebug(`Div ${divId} is already populated, skipping update.`);
}
};
const TrustArcConsentBanner = () => {
useEffect(() => {
logDebug("Consent banner mounted");
repopulateDiv(bannerId);
return () => logDebug("Consent banner unmounted");
}, []);
return _jsx("div", { id: bannerId });
};
const TrustArcCookiePreferencesLink = () => {
if (cookiePreferencesContainer == "") {
return null;
}
useEffect(() => {
logDebug("Cookie preferences link mounted");
repopulateDiv(cookiePreferencesContainer);
return () => logDebug("Cookie preferences link unmounted");
}, []);
return _jsx("div", { id: cookiePreferencesContainer });
};
const TrustArcIRMLink = () => {
if (irmContainer == "") {
return null;
}
useEffect(() => {
logDebug("UseEffect TrustArcIRMLink");
repopulateDiv(irmContainer);
return () => { };
}, []);
logDebug("Loading TrustArcIRMLink");
return _jsx("div", { id: irmContainer });
};
return {
TrustArcScript,
TrustArcConsentBanner,
TrustArcCookiePreferencesLink,
TrustArcIRMLink,
};
};