Files
KVideo/lib/hooks/useInfiniteScroll.ts
T
kuekhaoyang 3dfcc6565d feat: Add history management components and movie grid with infinite scroll
- Implemented HistoryEmptyState component for displaying when no viewing history exists.
- Created HistoryItem component to represent individual watch history items with video details and delete functionality.
- Developed MovieCard component to display individual movie details including poster, title, and rating.
- Added MovieGrid component for displaying a grid of movie cards with infinite scroll capabilities.
- Introduced TagManager component for managing custom tags with creation, deletion, and filtering functionalities.
- Created TypeBadgeItem and TypeBadgeList components for displaying selectable badges with counts.
- Added custom hook useInfiniteScroll for managing infinite scroll behavior.
- Implemented contrast testing script to ensure WCAG compliance for UI components.
2025-11-18 14:50:14 +08:00

49 lines
1.1 KiB
TypeScript

/**
* useInfiniteScroll - Custom hook for infinite scroll functionality
* Manages intersection observer for prefetching and loading more content
*/
'use client';
import { useEffect, useRef } from 'react';
interface UseInfiniteScrollProps {
hasMore: boolean;
loading: boolean;
page: number;
onLoadMore: (nextPage: number) => void;
}
export function useInfiniteScroll({
hasMore,
loading,
page,
onLoadMore
}: UseInfiniteScrollProps) {
const prefetchRef = useRef<HTMLDivElement>(null);
const loadMoreRef = useRef<HTMLDivElement>(null);
useEffect(() => {
if (!prefetchRef.current) return;
const prefetchObserver = new IntersectionObserver(
(entries) => {
const target = entries[0];
if (target.isIntersecting && hasMore && !loading) {
const nextPage = page + 1;
onLoadMore(nextPage);
}
},
{ threshold: 0.1, rootMargin: '400px' }
);
prefetchObserver.observe(prefetchRef.current);
return () => {
prefetchObserver.disconnect();
};
}, [hasMore, loading, page, onLoadMore]);
return { prefetchRef, loadMoreRef };
}