import { useEffect, useState } from 'react'; import CloseIcon from '../icons/close.svg'; import { httpGet } from '../lib/http'; import { sponsorHidden } from '../stores/page'; import { useStore } from '@nanostores/react'; export type PageSponsorType = { company: string; description: string; gaLabel: string; imageUrl: string; pageUrl: string; title: string; url: string; }; type V1GetSponsorResponse = { href?: string; sponsor?: PageSponsorType; }; type PageSponsorProps = { gaPageIdentifier?: string; }; export function PageSponsor(props: PageSponsorProps) { const { gaPageIdentifier } = props; const $isSponsorHidden = useStore(sponsorHidden); const [sponsor, setSponsor] = useState(); const loadSponsor = async () => { const currentPath = window.location.pathname; if ( currentPath === '/' || currentPath === '/best-practices' || currentPath === '/roadmaps' || currentPath.startsWith('/guides') || currentPath.startsWith('/videos') || currentPath.startsWith('/account') || currentPath.startsWith('/team') ) { return; } const { response, error } = await httpGet( `${import.meta.env.PUBLIC_API_URL}/v1-get-sponsor`, { href: window.location.pathname, } ); if (error) { console.error(error); return; } if (!response?.sponsor) { return; } setSponsor(response.sponsor); window.fireEvent({ category: 'SponsorImpression', action: `${response.sponsor?.company} Impression`, label: response.sponsor.gaLabel || `${gaPageIdentifier} / ${response.sponsor?.company} Link`, }); }; useEffect(() => { window.setTimeout(loadSponsor); }, []); if ($isSponsorHidden || !sponsor) { return null; } const { url, title, imageUrl, description, company, gaLabel } = sponsor; return ( { window.fireEvent({ category: 'SponsorClick', action: `${company} Redirect`, label: gaLabel || `${gaPageIdentifier} / ${company} Link`, }); }} > { e.preventDefault(); sponsorHidden.set(true); }} > Close Sponsor Banner {title} {description} Partner Content ); }