import { cn } from '../../lib/classname.ts'; import { useScrollPosition } from '../../hooks/use-scroll-position.ts'; import { X } from 'lucide-react'; import type { StickyTopSponsorType } from './PageSponsors.tsx'; import { useEffect, useState } from 'react'; import { isOnboardingStripHidden } from '../../stores/page.ts'; type StickyTopSponsorProps = { sponsor: StickyTopSponsorType; onSponsorImpression: () => void; onSponsorClick: () => void; onSponsorHidden: () => void; }; const SCROLL_DISTANCE = 100; export function StickyTopSponsor(props: StickyTopSponsorProps) { const { sponsor, onSponsorHidden, onSponsorImpression, onSponsorClick } = props; const { y: scrollY } = useScrollPosition(); const [isImpressionLogged, setIsImpressionLogged] = useState(false); const [isHidden, setIsHidden] = useState(false); useEffect(() => { if (!sponsor) { return; } // hide the onboarding strip when the sponsor is visible isOnboardingStripHidden.set(true); }, [sponsor]); useEffect(() => { if (scrollY < SCROLL_DISTANCE || isImpressionLogged) { return; } setIsImpressionLogged(true); onSponsorImpression(); }, [scrollY]); if (scrollY < SCROLL_DISTANCE || isHidden) { return null; } return ( {'ad'} {sponsor.description} ); }