From 911f80cae1ab7bc556db6bd7ebb4b01187cb0b2e Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Sun, 13 Jun 2021 18:38:19 +0300 Subject: [PATCH] Infinite Scroll: Fix missing chats --- src/components/ui/InfiniteScroll.tsx | 12 ++++++++---- src/hooks/useInfiniteScroll.ts | 11 ++++++++--- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/components/ui/InfiniteScroll.tsx b/src/components/ui/InfiniteScroll.tsx index dfd91a813..ebc112d9d 100644 --- a/src/components/ui/InfiniteScroll.tsx +++ b/src/components/ui/InfiniteScroll.tsx @@ -11,7 +11,7 @@ import resetScroll from '../../util/resetScroll'; type OwnProps = { ref?: RefObject; className?: string; - onLoadMore?: ({ direction }: { direction: LoadMoreDirection }) => void; + onLoadMore?: ({ direction }: { direction: LoadMoreDirection; noScroll?: boolean }) => void; onScroll?: (e: UIEvent) => void; onKeyDown?: (e: React.KeyboardEvent) => void; items?: any[]; @@ -66,8 +66,12 @@ const InfiniteScroll: FC = ({ } return [ - debounce(() => onLoadMore({ direction: LoadMoreDirection.Backwards }), 1000, true, false), - debounce(() => onLoadMore({ direction: LoadMoreDirection.Forwards }), 1000, true, false), + debounce((noScroll = false) => { + onLoadMore({ direction: LoadMoreDirection.Backwards, noScroll }); + }, 1000, true, false), + debounce(() => { + onLoadMore({ direction: LoadMoreDirection.Forwards }); + }, 1000, true, false), ]; // eslint-disable-next-line react-hooks/exhaustive-deps }, [onLoadMore, items]); @@ -79,7 +83,7 @@ const InfiniteScroll: FC = ({ } if (preloadBackwards > 0 && (!items || items.length < preloadBackwards)) { - loadMoreBackwards(); + loadMoreBackwards(true); return; } diff --git a/src/hooks/useInfiniteScroll.ts b/src/hooks/useInfiniteScroll.ts index a0b5b3fd2..23588f7f0 100644 --- a/src/hooks/useInfiniteScroll.ts +++ b/src/hooks/useInfiniteScroll.ts @@ -52,7 +52,10 @@ export default ( } }, [listIds, isDisabled, loadMoreBackwards, forceFullPreload]); - const getMore: GetMore = useCallback(({ direction }: { direction: LoadMoreDirection }) => { + const getMore: GetMore = useCallback(({ + direction, + noScroll, + }: { direction: LoadMoreDirection; noScroll?: boolean }) => { const viewportIds = viewportIdsRef.current; const offsetId = viewportIds @@ -67,8 +70,6 @@ export default ( return; } - lastParamsRef.current = { ...lastParamsRef.current, direction, offsetId }; - const { newViewportIds, areSomeLocal, areAllLocal, } = getViewportSlice(listIds, offsetId, direction, listSlice); @@ -79,6 +80,10 @@ export default ( } if (!areAllLocal && loadMoreBackwards) { + if (!noScroll) { + lastParamsRef.current = { ...lastParamsRef.current, direction, offsetId }; + } + loadMoreBackwards({ offsetId }); } }, [listIds, listSlice, loadMoreBackwards, forceUpdate]);