import { useEffect, useState } from 'react'; import { httpGet, httpPatch, httpPost } from '../lib/http'; import { sponsorHidden } from '../stores/page'; import { useStore } from '@nanostores/react'; import { X } from 'lucide-react'; import { setViewSponsorCookie } from '../lib/jwt'; import { isMobile } from '../lib/is-mobile'; import Cookies from 'js-cookie'; import { getUrlUtmParams } from '../lib/browser.ts'; export type PageSponsorType = { company: string; description: string; gaLabel: string; imageUrl: string; pageUrl: string; title: string; url: string; }; type V1GetSponsorResponse = { id?: string; href?: string; sponsor?: PageSponsorType; }; type PageSponsorProps = { gaPageIdentifier?: string; }; const CLOSE_SPONSOR_KEY = 'sponsorClosed'; function markSponsorHidden(sponsorId: string) { Cookies.set(`${CLOSE_SPONSOR_KEY}-${sponsorId}`, '1', { path: '/', expires: 1, sameSite: 'lax', secure: true, domain: import.meta.env.DEV ? 'localhost' : '.roadmap.sh', }); } function isSponsorMarkedHidden(sponsorId: string) { return Cookies.get(`${CLOSE_SPONSOR_KEY}-${sponsorId}`) === '1'; } export function PageSponsor(props: PageSponsorProps) { const { gaPageIdentifier } = props; const $isSponsorHidden = useStore(sponsorHidden); const [sponsorId, setSponsorId] = useState(null); const [sponsor, setSponsor] = useState(); useEffect(() => { const foundUtmParams = getUrlUtmParams(); if (!foundUtmParams.utmSource) { return; } localStorage.setItem('utm_params', JSON.stringify(foundUtmParams)); }, []); 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, mobile: isMobile() ? 'true' : 'false', }, ); if (error) { console.error(error); return; } if ( !response?.sponsor || !response.id || isSponsorMarkedHidden(response.id) ) { return; } setSponsor(response.sponsor); setSponsorId(response.id); window.fireEvent({ category: 'SponsorImpression', action: `${response.sponsor?.company} Impression`, label: response.sponsor.gaLabel || `${gaPageIdentifier} / ${response.sponsor?.company} Link`, }); }; const clickSponsor = async (sponsorId: string) => { const clickUrl = new URL( `${import.meta.env.PUBLIC_API_URL}/v1-view-sponsor/${sponsorId}`, ); const { response, error } = await httpPatch<{ status: 'ok' }>( clickUrl.toString(), { mobile: isMobile(), }, ); if (error || !response) { console.error(error); return; } setViewSponsorCookie(sponsorId); }; useEffect(() => { window.setTimeout(loadSponsor); }, []); if ($isSponsorHidden || !sponsor) { return null; } const { url, title, imageUrl, description, company, gaLabel } = sponsor; const isRoadmapAd = title.toLowerCase() === 'advertise with us!'; return ( { window.fireEvent({ category: 'SponsorClick', action: `${company} Redirect`, label: gaLabel || `${gaPageIdentifier} / ${company} Link`, }); await clickSponsor(sponsorId || ''); }} > { e.preventDefault(); markSponsorHidden(sponsorId || ''); sponsorHidden.set(true); }} > Sponsor Banner {title} {description} {!isRoadmapAd && ( <> Partner Content Partner Content )} ); }