Files
OpenCut/apps/web/src/hooks/use-infinite-scroll.ts
T

36 lines
854 B
TypeScript
Raw Normal View History

2025-08-15 10:11:21 +10:00
import { useRef, useCallback } from "react";
interface UseInfiniteScrollOptions {
2026-01-31 00:20:04 +01:00
onLoadMore: () => void;
hasMore: boolean;
isLoading: boolean;
threshold?: number;
enabled?: boolean;
2025-08-15 10:11:21 +10:00
}
export function useInfiniteScroll({
2026-01-31 00:20:04 +01:00
onLoadMore,
hasMore,
isLoading,
threshold = 200,
enabled = true,
2025-08-15 10:11:21 +10:00
}: UseInfiniteScrollOptions) {
2026-01-31 00:20:04 +01:00
const scrollAreaRef = useRef<HTMLDivElement>(null);
2025-08-15 10:11:21 +10:00
2026-01-31 00:20:04 +01:00
const handleScroll = useCallback(
(event: React.UIEvent<HTMLDivElement>) => {
if (!enabled) return;
2025-08-15 10:11:21 +10:00
2026-01-31 00:20:04 +01:00
const { scrollTop, scrollHeight, clientHeight } = event.currentTarget;
const nearBottom = scrollTop + clientHeight >= scrollHeight - threshold;
2025-08-15 10:11:21 +10:00
2026-01-31 00:20:04 +01:00
if (nearBottom && hasMore && !isLoading) {
onLoadMore();
}
},
[onLoadMore, hasMore, isLoading, threshold, enabled],
);
2025-08-15 10:11:21 +10:00
2026-01-31 00:20:04 +01:00
return { scrollAreaRef, handleScroll };
2025-08-15 10:11:21 +10:00
}