|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { usePathname } from 'next/navigation'; |
| 4 | +import { useEffect, useRef, useState } from 'react'; |
| 5 | + |
| 6 | +import useMediaQuery from '#site/hooks/useMediaQuery'; |
| 7 | +import { |
| 8 | + CARBON_ADS_COUNT_IMPRESSIONS, |
| 9 | + CARBON_ADS_URL, |
| 10 | +} from '#site/next.constants.mjs'; |
| 11 | + |
| 12 | +import type { CSSProperties } from 'react'; |
| 13 | + |
| 14 | +export type CarbonAd = Record<string, string>; |
| 15 | + |
| 16 | +export const adColors = (ad: CarbonAd) => |
| 17 | + ({ |
| 18 | + '--ad-bg': ad.backgroundColor, |
| 19 | + '--ad-bg-dark': ad.backgroundColorDark ?? ad.backgroundColor, |
| 20 | + '--ad-text': ad.textColor, |
| 21 | + '--ad-text-dark': ad.textColorDark ?? ad.textColor, |
| 22 | + '--ad-cta-bg': ad.ctaBackgroundColor, |
| 23 | + '--ad-cta-bg-dark': ad.ctaBackgroundColorDark ?? ad.ctaBackgroundColor, |
| 24 | + '--ad-cta-text': ad.ctaTextColor, |
| 25 | + '--ad-cta-text-dark': ad.ctaTextColorDark ?? ad.ctaTextColor, |
| 26 | + }) as CSSProperties; |
| 27 | + |
| 28 | +const useCarbonAd = (zone: string, media?: string) => { |
| 29 | + const [ad, setAd] = useState<CarbonAd | null>(null); |
| 30 | + const ref = useRef<HTMLElement>(null); |
| 31 | + // Layouts persist across navigations, so refetch per page |
| 32 | + const pathname = usePathname(); |
| 33 | + const matches = useMediaQuery(media ?? 'all'); |
| 34 | + |
| 35 | + useEffect(() => { |
| 36 | + if (!matches) { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + const controller = new AbortController(); |
| 41 | + const url = new URL(`${zone}.json`, CARBON_ADS_URL); |
| 42 | + |
| 43 | + if (!CARBON_ADS_COUNT_IMPRESSIONS) { |
| 44 | + url.searchParams.set('ignore', 'yes'); |
| 45 | + } |
| 46 | + |
| 47 | + fetch(url, { signal: controller.signal }) |
| 48 | + .then(response => (response.ok ? response.json() : null)) |
| 49 | + .then(data => data?.ads?.[0]?.statlink && setAd(data.ads[0])) |
| 50 | + .catch(() => {}); |
| 51 | + |
| 52 | + return () => { |
| 53 | + controller.abort(); |
| 54 | + setAd(null); |
| 55 | + }; |
| 56 | + }, [zone, matches, pathname]); |
| 57 | + |
| 58 | + useEffect(() => { |
| 59 | + if (!ad?.statview || !ref.current) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + let timer: ReturnType<typeof setTimeout>; |
| 64 | + |
| 65 | + const observer = new IntersectionObserver( |
| 66 | + entries => { |
| 67 | + clearTimeout(timer); |
| 68 | + |
| 69 | + if (entries.at(-1)!.intersectionRatio >= 0.5) { |
| 70 | + timer = setTimeout(() => { |
| 71 | + new Image().src = ad.statview; |
| 72 | + observer.disconnect(); |
| 73 | + }, 1000); |
| 74 | + } |
| 75 | + }, |
| 76 | + { threshold: 0.5 } |
| 77 | + ); |
| 78 | + |
| 79 | + observer.observe(ref.current); |
| 80 | + |
| 81 | + return () => { |
| 82 | + clearTimeout(timer); |
| 83 | + observer.disconnect(); |
| 84 | + }; |
| 85 | + }, [ad]); |
| 86 | + |
| 87 | + return { ad, ref }; |
| 88 | +}; |
| 89 | + |
| 90 | +export default useCarbonAd; |
0 commit comments