| | import { useCallback, useMemo } from 'react'; |
| |
|
| | interface UseVirtualGridProps { |
| | itemCount: number; |
| | containerWidth: number; |
| | itemHeight: number; |
| | gapSize: number; |
| | mobileColumnsCount: number; |
| | desktopColumnsCount: number; |
| | mobileBreakpoint: number; |
| | } |
| |
|
| | interface UseVirtualGridReturn { |
| | cardsPerRow: number; |
| | rowCount: number; |
| | rowHeight: number; |
| | getRowItems: (rowIndex: number, items: any[]) => any[]; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | export const useVirtualGrid = ({ |
| | itemCount, |
| | containerWidth, |
| | itemHeight, |
| | gapSize, |
| | mobileColumnsCount, |
| | desktopColumnsCount, |
| | mobileBreakpoint = 768, |
| | }: UseVirtualGridProps): UseVirtualGridReturn => { |
| | |
| | const cardsPerRow = useMemo(() => { |
| | return containerWidth >= mobileBreakpoint ? desktopColumnsCount : mobileColumnsCount; |
| | }, [containerWidth, mobileBreakpoint, desktopColumnsCount, mobileColumnsCount]); |
| |
|
| | |
| | const rowCount = useMemo(() => { |
| | return Math.ceil(itemCount / cardsPerRow); |
| | }, [itemCount, cardsPerRow]); |
| |
|
| | |
| | const rowHeight = useMemo(() => { |
| | return itemHeight + gapSize; |
| | }, [itemHeight, gapSize]); |
| |
|
| | |
| | const getRowItems = useCallback( |
| | (rowIndex: number, items: any[]) => { |
| | const startIndex = rowIndex * cardsPerRow; |
| | const endIndex = Math.min(startIndex + cardsPerRow, items.length); |
| | return items.slice(startIndex, endIndex); |
| | }, |
| | [cardsPerRow], |
| | ); |
| |
|
| | return { |
| | cardsPerRow, |
| | rowCount, |
| | rowHeight, |
| | getRowItems, |
| | }; |
| | }; |
| |
|
| | export default useVirtualGrid; |
| |
|