use throttle for item loader, simplify implementation

This commit is contained in:
jeffvli
2025-11-14 03:06:44 -08:00
parent 2da6894ee5
commit 8ac3f2a6f7
3 changed files with 112 additions and 247 deletions
@@ -1,6 +1,5 @@
import { useElementSize, useMergedRef } from '@mantine/hooks';
import clsx from 'clsx';
import debounce from 'lodash/debounce';
import throttle from 'lodash/throttle';
import { AnimatePresence } from 'motion/react';
import { useOverlayScrollbars } from 'overlayscrollbars-react';
@@ -114,38 +113,23 @@ const VirtualizedGridList = React.memo(
itemType,
]);
const debouncedOnScrollEnd = useMemo(
() =>
onScrollEnd
? debounce((scrollOffset: number, direction: 'down' | 'up') => {
onScrollEnd(scrollOffset, direction);
}, 100)
: undefined,
[onScrollEnd],
);
useEffect(() => {
return () => {
debouncedOnScrollEnd?.cancel();
};
}, [debouncedOnScrollEnd]);
const handleOnScroll = useCallback(
({ scrollDirection, scrollOffset }: ListOnScrollProps) => {
onScroll?.(scrollOffset, scrollDirection === 'forward' ? 'down' : 'up');
debouncedOnScrollEnd?.(scrollOffset, scrollDirection === 'forward' ? 'down' : 'up');
onScrollEnd?.(scrollOffset, scrollDirection === 'forward' ? 'down' : 'up');
},
[onScroll, debouncedOnScrollEnd],
[onScroll, onScrollEnd],
);
const debouncedOnItemsRendered = useMemo(() => {
return debounce((items: ListOnItemsRenderedProps) => {
const handleOnItemsRendered = useCallback(
(items: ListOnItemsRenderedProps) => {
onRangeChanged?.({
startIndex: items.visibleStartIndex * (tableMeta?.columnCount || 0),
stopIndex: items.visibleStopIndex * (tableMeta?.columnCount || 0),
});
}, 50);
}, [onRangeChanged, tableMeta?.columnCount]);
},
[onRangeChanged, tableMeta?.columnCount],
);
if (!tableMeta) {
return null;
@@ -169,7 +153,7 @@ const VirtualizedGridList = React.memo(
itemCount={itemData.tableMeta?.rowCount || 0}
itemData={itemData}
itemSize={itemData.tableMeta?.itemHeight || 0}
onItemsRendered={debouncedOnItemsRendered}
onItemsRendered={handleOnItemsRendered}
onScroll={handleOnScroll}
outerRef={outerRef}
ref={ref}