Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | 1x 1x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 13x 13x 13x 13x 13x 13x 13x 13x 11x 10x 9x 7x 7x 6x 13x 6x 6x 6x 6x 6x 13x 2x 2x 2x 13x 1x 13x 12x 12x 13x 55x 55x 55x 27x 27x 18x 18x 18x 18x 18x 18x 18x 18x 55x 55x 27x 27x 16x 16x 16x 55x 55x 55x 55x 55x 55x 55x | import { useCallback, useEffect, useRef, useState } from 'react';
export interface InfiniteScrollOptions {
threshold?: number;
rootMargin?: string;
enabled?: boolean;
hasNextPage?: boolean;
isFetchingNextPage?: boolean;
onLoadMore?: () => void;
}
export interface InfiniteScrollReturn {
sentinelRef: React.RefObject<HTMLDivElement>;
isIntersecting: boolean;
hasReachedEnd: boolean;
}
/**
* Custom hook for infinite scroll functionality using Intersection Observer
* Provides business logic for detecting when user has scrolled to the bottom
* and triggering load more actions
*/
export const useInfiniteScroll = ({
threshold = 0.1,
rootMargin = '100px',
enabled = true,
hasNextPage = false,
isFetchingNextPage = false,
onLoadMore,
}: InfiniteScrollOptions): InfiniteScrollReturn => {
const sentinelRef = useRef<HTMLDivElement>(null);
const [isIntersecting, setIsIntersecting] = useState(false);
const [hasReachedEnd, setHasReachedEnd] = useState(false);
// Track if we've already triggered a load to prevent duplicate calls
const loadTriggeredRef = useRef(false);
const lastLoadTimeRef = useRef(0);
const minimumLoadInterval = 500; // Minimum time between loads in ms
const handleIntersection = useCallback(
(entries: IntersectionObserverEntry[]) => {
const [entry] = entries;
const isCurrentlyIntersecting = entry.isIntersecting;
setIsIntersecting(isCurrentlyIntersecting);
// Only trigger load more when:
// 1. Element is intersecting
// 2. We have more pages to load
// 3. We're not already fetching
// 4. We haven't already triggered a load for this intersection
// 5. Infinite scroll is enabled
// 6. Enough time has passed since last load
const now = Date.now();
const timeSinceLastLoad = now - lastLoadTimeRef.current;
// Debug logging for development
if (process.env.NODE_ENV === 'development') {
console.log('InfiniteScroll Debug:', {
isCurrentlyIntersecting,
hasNextPage,
isFetchingNextPage,
loadTriggered: loadTriggeredRef.current,
enabled,
timeSinceLastLoad,
minimumLoadInterval,
shouldTrigger: isCurrentlyIntersecting && hasNextPage && !isFetchingNextPage && !loadTriggeredRef.current && enabled && timeSinceLastLoad >= minimumLoadInterval,
});
}
if (
isCurrentlyIntersecting &&
hasNextPage &&
!isFetchingNextPage &&
!loadTriggeredRef.current &&
enabled &&
onLoadMore &&
timeSinceLastLoad >= minimumLoadInterval
) {
loadTriggeredRef.current = true;
lastLoadTimeRef.current = now;
if (process.env.NODE_ENV === 'development') {
console.log('InfiniteScroll: Triggering load more');
}
onLoadMore();
}
// Reset load trigger when element is no longer intersecting
if (!isCurrentlyIntersecting) {
loadTriggeredRef.current = false;
if (process.env.NODE_ENV === 'development') {
console.log('InfiniteScroll: Reset load trigger (no longer intersecting)');
}
}
// Update end state when there are no more pages
if (!hasNextPage && !isFetchingNextPage) {
setHasReachedEnd(true);
} else {
setHasReachedEnd(false);
}
},
[hasNextPage, isFetchingNextPage, enabled, onLoadMore]
);
useEffect(() => {
const sentinel = sentinelRef.current;
if (!sentinel || !enabled) return;
const observer = new IntersectionObserver(handleIntersection, {
threshold,
rootMargin,
});
observer.observe(sentinel);
return () => {
observer.disconnect();
};
}, [handleIntersection, threshold, rootMargin, enabled]);
// Update hasReachedEnd state when dependencies change
useEffect(() => {
setHasReachedEnd(!hasNextPage && !isFetchingNextPage);
// Only reset loadTriggeredRef when we're done fetching AND there are no more pages
// This prevents the problematic reset that was causing the infinite loop
if (!isFetchingNextPage && !hasNextPage) {
loadTriggeredRef.current = false;
if (process.env.NODE_ENV === 'development') {
console.log('InfiniteScroll: Reset load trigger (no more pages and not fetching)');
}
}
}, [hasNextPage, isFetchingNextPage]);
return {
sentinelRef,
isIntersecting,
hasReachedEnd,
};
};
|