'use client'; import { useEffect, useRef, useState } from 'react'; interface IntersectionObserverOptions { threshold?: number; rootMargin?: string; triggerOnce?: boolean; } export function useIntersectionObserver( options: IntersectionObserverOptions = {} ) { const { threshold = 0.1, rootMargin = '0px 0px -100px 0px', triggerOnce = true } = options; const [isIntersecting, setIsIntersecting] = useState(false); const [hasIntersected, setHasIntersected] = useState(false); const elementRef = useRef(null); useEffect(() => { const element = elementRef.current; if (!element) return; const observer = new IntersectionObserver( ([entry]) => { const isVisible = entry.isIntersecting; setIsIntersecting(isVisible); if (isVisible && !hasIntersected) { setHasIntersected(true); } }, { threshold, rootMargin, } ); observer.observe(element); return () => { observer.unobserve(element); }; }, [threshold, rootMargin, hasIntersected]); return { elementRef, isVisible: triggerOnce ? hasIntersected : isIntersecting, }; }