import { CookieFirstPublicApi, initialContext } from '../context'; import { useCallback, useEffect, useState } from 'react'; const INIT_EVENT = 'cf_init'; declare global { interface Window { CookieFirst: CookieFirstPublicApi; } } const getContextFromCfObject = ( CookieFirst: CookieFirstPublicApi, isError: boolean ): CookieFirstPublicApi => ({ isErrorState: isError, consent: CookieFirst.consent, acceptedServices: CookieFirst.acceptedServices, stealthMode: CookieFirst.stealthMode, hasConsented: CookieFirst.hasConsented, openPanel: CookieFirst.openPanel, closePanel: CookieFirst.closePanel, acceptCategory: CookieFirst.acceptCategory, declineCategory: CookieFirst.declineCategory, acceptService: CookieFirst.acceptService, declineService: CookieFirst.declineService, acceptAllCategories: CookieFirst.acceptAllCategories, acceptPreselectedCategories: CookieFirst.acceptPreselectedCategories, deny: CookieFirst.deny, declineAllCategories: CookieFirst.declineAllCategories, withdrawConsent: CookieFirst.withdrawConsent, fetchLatestScan: CookieFirst.fetchLatestScan, renderEmbeds: CookieFirst.renderEmbeds, notifyViewChanged: CookieFirst.notifyViewChanged, changeLanguage: CookieFirst.changeLanguage, visitorId: CookieFirst.visitorId, consentTimestamp: CookieFirst.consentTimestamp, TCFconsent: CookieFirst.TCFconsent, TCFeventStatus: CookieFirst.TCFeventStatus, TCFcountry: CookieFirst.TCFcountry, TCFgvl: CookieFirst.TCFgvl, TCFgatp: CookieFirst.TCFgatp, TCFisEnabled: CookieFirst.TCFisEnabled, TCFgetConsentStructure: CookieFirst.TCFgetConsentStructure, TCFacceptCategory: CookieFirst.TCFacceptCategory, TCFdeclineCategory: CookieFirst.TCFdeclineCategory, TCFacceptCategoryLegitimateInterest: CookieFirst.TCFacceptCategoryLegitimateInterest, TCFdeclineCategoryLegitimateInterest: CookieFirst.TCFdeclineCategoryLegitimateInterest, TCFacceptVendor: CookieFirst.TCFacceptVendor, TCFdeclineVendor: CookieFirst.TCFdeclineVendor, TCFacceptVendorLegitimateInterest: CookieFirst.TCFacceptVendorLegitimateInterest, TCFdeclineVendorLegitimateInterest: CookieFirst.TCFdeclineVendorLegitimateInterest, TCFisVendorAccepted: CookieFirst.TCFisVendorAccepted, TCFisVendorLegitimateInterestAccepted: CookieFirst.TCFisVendorLegitimateInterestAccepted, TCFisCategoryAccepted: CookieFirst.TCFisCategoryAccepted, TCFisCategoryLegitimateInterestAccepted: CookieFirst.TCFisCategoryLegitimateInterestAccepted, TCFacceptGatpVendor: CookieFirst.TCFacceptGatpVendor, TCFdeclineGatpVendor: CookieFirst.TCFdeclineGatpVendor, TCFisGatpVendorAccepted: CookieFirst.TCFisGatpVendorAccepted, updateConsent: CookieFirst.updateConsent, }); const useContextManager = (): ContextManagerReturnType => { const [isLoaded, setIsLoaded] = useState(false); const [isError, setIsError] = useState(false); const [context, setContext] = useState(initialContext); const handleInit = useCallback(() => { if (!window.CookieFirst) { return; } setContext(getContextFromCfObject(window.CookieFirst, isError)); setIsLoaded(true); }, []); const handleError = () => { setIsError(true); setContext({ ...context, isErrorState: true }); }; const syncContext = useCallback(() => { if (!window.CookieFirst) { return; } setContext(getContextFromCfObject(window.CookieFirst, isError)); }, []); // List of events on which context should be synced const EVENTS_LIST = ['cf_consent', 'banner_cookie_api_sync']; useEffect(() => { window.addEventListener(INIT_EVENT, handleInit); EVENTS_LIST.forEach((event) => { window.addEventListener(event, syncContext); }); // List of events that trigger error state const ERROR_EVENTS_LIST = ['banner_cookie_error_state']; ERROR_EVENTS_LIST.forEach((event) => { window.addEventListener(event, () => { handleError(); }); }); return () => { window.removeEventListener(INIT_EVENT, handleInit); EVENTS_LIST.forEach((event) => { window.removeEventListener(event, syncContext); }); ERROR_EVENTS_LIST.forEach((event) => { window.removeEventListener(event, handleError); }); }; }, []); return { isLoaded, context, isError, handleError }; }; export type ContextManagerReturnType = { isLoaded: boolean; context: CookieFirstPublicApi; isError: boolean; handleError: () => void; }; export default useContextManager;