"use client"; import React, {useCallback, useEffect} from "react"; import Script from "next/script"; import {Image} from "@heroui/react"; import carbonOptimize from "./carbon-optimize"; import {loadScript} from "@/utils/scripts"; import {useIsMounted} from "@/hooks/use-is-mounted"; import {__PROD__, __ENABLE_ADS__} from "@/utils"; const EA_PROVIDER_RATIO = 0.85; const PRODUCT_HUNT_ENABLED = false; const PH_INFO = { description: "Join the conversation and help us get #1 Product of the Week! ↗", title: "We're live on Product Hunt! (30% OFF)", url: "https://ph.heroui.chat?utm_source=heroui.chat&utm_medium=banner", }; export const CarbonAd: React.FC = () => { const carbonRef = React.useRef(null); const [showEthicalAds, setShowEthicalAds] = React.useState(false); const isMounted = useIsMounted(); const loadEthicalAds = useCallback(() => { return new Promise((resolve) => { const script = document.createElement("script"); script.src = "https://media.ethicalads.io/media/client/ethicalads.min.js"; script.async = true; script.onload = () => { // @ts-ignore resolve(window.ethicalads); }; script.onerror = () => { resolve(null); }; document.body.appendChild(script); return () => { document.body.removeChild(script); }; }); }, []); useEffect(() => { if (PRODUCT_HUNT_ENABLED) return; const shouldShowEthicalAds = Math.random() < EA_PROVIDER_RATIO; let loadCarbon: any = null; const loadCarbonAds = () => { setShowEthicalAds(false); // The isolation logic of carbonads is flawed. // Once the script starts loading, it will asynchronous resolve, with no way to stop it. // This leads to duplication of the ad. To solve the issue, we debounce the load action. loadCarbon = isMounted && setTimeout(() => { const script = loadScript( "https://cdn.carbonads.com/carbon.js?serve=CESIC53Y&placement=nextuiorg", carbonRef.current, ); script.id = "_carbonads_js"; carbonOptimize.init(); }); }; const loadAdProvider = async () => { if (shouldShowEthicalAds) { try { const ethicalads = await loadEthicalAds(); if (!ethicalads) { loadCarbonAds(); return; } // @ts-ignore ethicalads.wait.then((placements) => { if (!placements.length) { loadCarbonAds(); } else { setShowEthicalAds(true); } }); } catch (error) { loadCarbonAds(); } } else { loadCarbonAds(); } }; loadAdProvider(); return () => { loadCarbon && clearTimeout(loadCarbon); }; }, [isMounted]); if (PRODUCT_HUNT_ENABLED) { return (
Product Hunt
{PH_INFO.title}
{PH_INFO.description}
); } if (!__PROD__ || !__ENABLE_ADS__) return null; return ( <> <>