import { VendorName } from '../types'; import { Dispatch, SetStateAction } from 'react'; import { socialMediaVendors } from './socialMediaVendors'; export const checkVendorConsent = (vendorName: VendorName): boolean => { let isSocialVendorAllowed = false; if (window.__tcfapi) { window.__tcfapi( 'getCustomVendorConsents', 2, (data: any, success: boolean) => { if (success && data && data.consentedVendors) { isSocialVendorAllowed = data.consentedVendors.some( (vendor: { name: string }) => vendor.name.toLowerCase() === vendorName.toLowerCase() ); } else { // tslint:disable-next-line:no-console console.log( `Error fetching consent data or ${vendorName} embed not allowed` ); isSocialVendorAllowed = false; } } ); } return isSocialVendorAllowed; }; // Async version that properly checks consent and updates state export const checkVendorConsentAsync = ( vendorName: VendorName, setIsSocialEmbedAllowed: Dispatch>> ): void => { if (window.__tcfapi) { const vendorConfig = socialMediaVendors[vendorName]; const vendorId = vendorConfig ? vendorConfig.id : undefined; window.__tcfapi( 'getCustomVendorConsents', 2, (data: any, success: boolean) => { if (success && data) { // Check by vendor name in consentedVendors array let hasConsent = false; if (data.consentedVendors) { hasConsent = data.consentedVendors.some( (vendor: { name: string; _id?: string }) => vendor.name.toLowerCase() === vendorName.toLowerCase() || vendor._id === vendorId ); } // Also check by vendor ID in grants object if (!hasConsent && vendorId && data.grants && data.grants[vendorId]) { hasConsent = data.grants[vendorId].vendorGrant === true; } setIsSocialEmbedAllowed(prev => ({ ...prev, [vendorName]: hasConsent })); } } ); } };