From b73c61889cd49fbb31473123770abf8b1f4ea662 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Tue, 6 Jun 2023 11:09:55 +0200 Subject: [PATCH] Chat List: Fix part of chats missing until scroll --- src/global/actions/api/messages.ts | 2 +- src/hooks/useInfiniteScroll.ts | 26 ++++++++++++++++---------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/global/actions/api/messages.ts b/src/global/actions/api/messages.ts index f63fe8cf4..554d647b8 100644 --- a/src/global/actions/api/messages.ts +++ b/src/global/actions/api/messages.ts @@ -1111,7 +1111,7 @@ function getViewportSlice( let areAllLocal; switch (direction) { case LoadMoreDirection.Backwards: - areSomeLocal = indexForDirection > 0; + areSomeLocal = indexForDirection >= 0; areAllLocal = from >= 0; break; case LoadMoreDirection.Forwards: diff --git a/src/hooks/useInfiniteScroll.ts b/src/hooks/useInfiniteScroll.ts index 200fe010d..c7831393d 100644 --- a/src/hooks/useInfiniteScroll.ts +++ b/src/hooks/useInfiniteScroll.ts @@ -16,14 +16,14 @@ const useInfiniteScroll = ( isDisabled = false, listSlice = DEFAULT_LIST_SLICE, ): [ListId[]?, GetMore?] => { - const lastParamsRef = useRef<{ + const requestParamsRef = useRef<{ direction?: LoadMoreDirection; offsetId?: ListId; }>(); const viewportIdsRef = useRef((() => { // Only run once to initialize - if (!listIds || lastParamsRef.current) { + if (!listIds || requestParamsRef.current) { return undefined; } @@ -34,16 +34,22 @@ const useInfiniteScroll = ( const forceUpdate = useForceUpdate(); if (isDisabled) { - lastParamsRef.current = {}; + requestParamsRef.current = {}; } const prevListIds = usePrevious(listIds); const prevIsDisabled = usePrevious(isDisabled); if (listIds && !isDisabled && (listIds !== prevListIds || isDisabled !== prevIsDisabled)) { - const { offsetId = listIds[0], direction = LoadMoreDirection.Forwards } = lastParamsRef.current || {}; + const viewportIds = viewportIdsRef.current; + const isOnTop = viewportIds && viewportIds[0] === listIds[0]; + const currentMiddleId = !isOnTop && viewportIds ? viewportIds[Math.round(viewportIds.length / 2)] : undefined; + const defaultOffsetId = currentMiddleId && listIds.includes(currentMiddleId) ? currentMiddleId : listIds[0]; + const { offsetId = defaultOffsetId, direction = LoadMoreDirection.Forwards } = requestParamsRef.current || {}; const { newViewportIds } = getViewportSlice(listIds, direction, listSlice, offsetId); - if (!viewportIdsRef.current || !areSortedArraysEqual(viewportIdsRef.current, newViewportIds)) { + requestParamsRef.current = {}; + + if (!viewportIds || !areSortedArraysEqual(viewportIds, newViewportIds)) { viewportIdsRef.current = newViewportIds; } } else if (!listIds) { @@ -68,10 +74,6 @@ const useInfiniteScroll = ( return; } - if (!noScroll) { - lastParamsRef.current = { ...lastParamsRef.current, direction, offsetId }; - } - const { newViewportIds, areSomeLocal, areAllLocal, } = getViewportSlice(listIds, direction, listSlice, offsetId); @@ -82,6 +84,10 @@ const useInfiniteScroll = ( } if (!areAllLocal && loadMoreBackwards) { + if (!noScroll) { + requestParamsRef.current = { ...requestParamsRef.current, direction, offsetId }; + } + loadMoreBackwards({ offsetId }); } }, [listIds, listSlice, loadMoreBackwards, forceUpdate]); @@ -107,7 +113,7 @@ function getViewportSlice( let areAllLocal; switch (direction) { case LoadMoreDirection.Forwards: - areSomeLocal = indexForDirection > 0; + areSomeLocal = indexForDirection >= 0; areAllLocal = from >= 0; break; case LoadMoreDirection.Backwards: