|
| 1 | +import React, { useEffect, useState, useRef } from "react"; |
| 2 | + |
| 3 | +export interface AdBannerProps { |
| 4 | + zoneId: string; |
| 5 | + contentId: string; |
| 6 | +} |
| 7 | + |
| 8 | +export const AdBanner = ({ zoneId, contentId }: AdBannerProps) => { |
| 9 | + const [isAdLoaded, setIsAdLoaded] = useState(false); |
| 10 | + const adContainerRef = useRef<HTMLDivElement>(null); |
| 11 | + const adContentRef = useRef<HTMLDivElement>(null); |
| 12 | + const adDetectedRef = useRef(false); |
| 13 | + |
| 14 | + useEffect(() => { |
| 15 | + const adContainer = adContainerRef.current; |
| 16 | + if (!adContainer) return; |
| 17 | + |
| 18 | + const insElement = adContainer.querySelector("ins"); |
| 19 | + if (!insElement) return; |
| 20 | + |
| 21 | + const checkForAd = () => { |
| 22 | + if (adDetectedRef.current) return true; |
| 23 | + |
| 24 | + const isLoaded = |
| 25 | + insElement.getAttribute("data-content-loaded") === "1" || |
| 26 | + insElement.querySelector(".revive-banner") !== null || |
| 27 | + insElement.querySelector("a.revive-banner") !== null || |
| 28 | + (insElement.children.length > 0 && insElement.offsetHeight > 10); |
| 29 | + |
| 30 | + if (isLoaded) { |
| 31 | + adDetectedRef.current = true; |
| 32 | + setIsAdLoaded(true); |
| 33 | + return true; |
| 34 | + } |
| 35 | + return false; |
| 36 | + }; |
| 37 | + |
| 38 | + const observer = new MutationObserver(() => { |
| 39 | + checkForAd(); |
| 40 | + }); |
| 41 | + |
| 42 | + observer.observe(insElement, { |
| 43 | + childList: true, |
| 44 | + subtree: true, |
| 45 | + attributes: true, |
| 46 | + attributeFilter: ["data-content-loaded", "style", "class", "id"], |
| 47 | + }); |
| 48 | + |
| 49 | + observer.observe(adContainer, { |
| 50 | + childList: true, |
| 51 | + subtree: true, |
| 52 | + attributes: true, |
| 53 | + }); |
| 54 | + |
| 55 | + const handleAdLoaded = () => { |
| 56 | + if (!adDetectedRef.current) { |
| 57 | + adDetectedRef.current = true; |
| 58 | + setIsAdLoaded(true); |
| 59 | + } |
| 60 | + }; |
| 61 | + |
| 62 | + const eventName = `content-${contentId}-loaded`; |
| 63 | + document.addEventListener(eventName, handleAdLoaded); |
| 64 | + |
| 65 | + checkForAd(); |
| 66 | + |
| 67 | + const intervals = [ |
| 68 | + setTimeout(checkForAd, 500), |
| 69 | + setTimeout(checkForAd, 1000), |
| 70 | + setTimeout(checkForAd, 2000), |
| 71 | + setTimeout(checkForAd, 3000), |
| 72 | + setTimeout(checkForAd, 5000), |
| 73 | + ]; |
| 74 | + |
| 75 | + return () => { |
| 76 | + observer.disconnect(); |
| 77 | + intervals.forEach(clearTimeout); |
| 78 | + document.removeEventListener(eventName, handleAdLoaded); |
| 79 | + }; |
| 80 | + }, [contentId]); |
| 81 | + |
| 82 | + useEffect(() => { |
| 83 | + const container = adContainerRef.current; |
| 84 | + const content = adContentRef.current; |
| 85 | + if (!container || !content) return; |
| 86 | + |
| 87 | + if (isAdLoaded) { |
| 88 | + const updateHeight = () => { |
| 89 | + const height = content.scrollHeight || content.offsetHeight || 100; |
| 90 | + container.style.maxHeight = `${Math.max(height, 100)}px`; |
| 91 | + container.style.overflow = "visible"; |
| 92 | + }; |
| 93 | + requestAnimationFrame(updateHeight); |
| 94 | + setTimeout(updateHeight, 100); |
| 95 | + setTimeout(updateHeight, 500); |
| 96 | + } else { |
| 97 | + container.style.maxHeight = "0px"; |
| 98 | + container.style.overflow = "hidden"; |
| 99 | + } |
| 100 | + }, [isAdLoaded]); |
| 101 | + |
| 102 | + return ( |
| 103 | + <div |
| 104 | + ref={adContainerRef} |
| 105 | + className="w-full transition-all duration-500 ease-in-out" |
| 106 | + style={{ |
| 107 | + opacity: isAdLoaded ? 1 : 0, |
| 108 | + }} |
| 109 | + > |
| 110 | + <div ref={adContentRef} className="w-full"> |
| 111 | + <ins data-content-zoneid={zoneId} data-content-id={contentId}></ins> |
| 112 | + </div> |
| 113 | + <script |
| 114 | + async |
| 115 | + src="//revive-adserver.swmansion.com/www/assets/js/lib.js" |
| 116 | + ></script> |
| 117 | + </div> |
| 118 | + ); |
| 119 | +}; |
0 commit comments