"use client" import { useEffect, type RefObject } from "react" type UseMailListInfiniteScrollOptions = { enabled: boolean sentinelRef: RefObject scrollRootRef: RefObject hasMore: boolean isLoadingMore: boolean onLoadMore: () => void /** Charge avant le bas visible pour éviter l’attente au scroll. */ rootMargin?: string } export function useMailListInfiniteScroll({ enabled, sentinelRef, scrollRootRef, hasMore, isLoadingMore, onLoadMore, rootMargin = "480px", }: UseMailListInfiniteScrollOptions) { useEffect(() => { if (!enabled || !hasMore) return const sentinel = sentinelRef.current const root = scrollRootRef.current if (!sentinel || !root) return const observer = new IntersectionObserver( ([entry]) => { if (!entry?.isIntersecting || isLoadingMore) return onLoadMore() }, { root, rootMargin, threshold: 0 } ) observer.observe(sentinel) return () => observer.disconnect() }, [ enabled, hasMore, isLoadingMore, onLoadMore, rootMargin, sentinelRef, scrollRootRef, ]) }