import React, { useEffect, useRef } from "react"; import { useTranslation } from "../hooks/useTranslation"; interface InfiniteScrollProps { children: React.ReactNode; onLoadMore: () => void; hasMore: boolean; isLoading: boolean; className?: string; loader?: React.ReactNode; } export const InfiniteScroll: React.FC = ({ children, onLoadMore, hasMore, isLoading, className = "", loader, }) => { const { t } = useTranslation(); const observerTarget = useRef(null); const onLoadMoreRef = useRef(onLoadMore); const hasMoreRef = useRef(hasMore); const isLoadingRef = useRef(isLoading); useEffect(() => { onLoadMoreRef.current = onLoadMore; hasMoreRef.current = hasMore; isLoadingRef.current = isLoading; }, [onLoadMore, hasMore, isLoading]); useEffect(() => { const observer = new IntersectionObserver( (entries) => { if (entries[0].isIntersecting && hasMoreRef.current && !isLoadingRef.current) { onLoadMoreRef.current(); } }, { root: null, rootMargin: "200px", threshold: 0 } ); if (observerTarget.current) { observer.observe(observerTarget.current); } return () => observer.disconnect(); }, []); return (
{children}
{isLoading && ( loader || (
{t.loading || "Loading..."}
) )}
); };