Chat List: Fix missing chats on page load
This commit is contained in:
parent
9108a030a0
commit
7ab5d3bc17
@ -23,15 +23,14 @@ const useInfiniteScroll = <ListId extends string | number>(
|
||||
offsetId?: ListId;
|
||||
}>();
|
||||
|
||||
const viewportIdsRef = useRef<ListId[] | undefined>((() => {
|
||||
// Only run once to initialize
|
||||
if (!listIds || requestParamsRef.current) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const { newViewportIds } = getViewportSlice(listIds, LoadMoreDirection.Forwards, listSlice, listIds[0]);
|
||||
return newViewportIds;
|
||||
})());
|
||||
const currentStateRef = useRef<{ viewportIds: ListId[]; isOnTop: boolean } | undefined>();
|
||||
if (!currentStateRef.current && listIds && !isDisabled) {
|
||||
const {
|
||||
newViewportIds,
|
||||
newIsOnTop,
|
||||
} = getViewportSlice(listIds, LoadMoreDirection.Forwards, listSlice, listIds[0]);
|
||||
currentStateRef.current = { viewportIds: newViewportIds, isOnTop: newIsOnTop };
|
||||
}
|
||||
|
||||
const forceUpdate = useForceUpdate();
|
||||
|
||||
@ -42,27 +41,26 @@ const useInfiniteScroll = <ListId extends string | number>(
|
||||
const prevListIds = usePrevious(listIds);
|
||||
const prevIsDisabled = usePrevious(isDisabled);
|
||||
if (listIds && !isDisabled && (listIds !== prevListIds || isDisabled !== prevIsDisabled)) {
|
||||
const viewportIds = viewportIdsRef.current;
|
||||
const isOnTop = viewportIds && viewportIds[0] === listIds[0];
|
||||
const currentMiddleId = !isOnTop && viewportIds ? viewportIds[Math.round(viewportIds.length / 2)] : undefined;
|
||||
const { viewportIds, isOnTop } = currentStateRef.current || {};
|
||||
const currentMiddleId = viewportIds && !isOnTop ? 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);
|
||||
const { newViewportIds, newIsOnTop } = getViewportSlice(listIds, direction, listSlice, offsetId);
|
||||
|
||||
requestParamsRef.current = {};
|
||||
|
||||
if (!viewportIds || !areSortedArraysEqual(viewportIds, newViewportIds)) {
|
||||
viewportIdsRef.current = newViewportIds;
|
||||
currentStateRef.current = { viewportIds: newViewportIds, isOnTop: newIsOnTop };
|
||||
}
|
||||
} else if (!listIds) {
|
||||
viewportIdsRef.current = undefined;
|
||||
currentStateRef.current = undefined;
|
||||
}
|
||||
|
||||
const getMore: GetMore = useLastCallback(({
|
||||
direction,
|
||||
noScroll,
|
||||
}: { direction: LoadMoreDirection; noScroll?: boolean }) => {
|
||||
const viewportIds = viewportIdsRef.current;
|
||||
const { viewportIds } = currentStateRef.current || {};
|
||||
|
||||
const offsetId = viewportIds
|
||||
? direction === LoadMoreDirection.Backwards ? viewportIds[viewportIds.length - 1] : viewportIds[0]
|
||||
@ -77,11 +75,11 @@ const useInfiniteScroll = <ListId extends string | number>(
|
||||
}
|
||||
|
||||
const {
|
||||
newViewportIds, areSomeLocal, areAllLocal,
|
||||
newViewportIds, areSomeLocal, areAllLocal, newIsOnTop,
|
||||
} = getViewportSlice(listIds, direction, listSlice, offsetId);
|
||||
|
||||
if (areSomeLocal && !(viewportIds && areSortedArraysEqual(viewportIds, newViewportIds))) {
|
||||
viewportIdsRef.current = newViewportIds;
|
||||
currentStateRef.current = { viewportIds: newViewportIds, isOnTop: newIsOnTop };
|
||||
forceUpdate();
|
||||
}
|
||||
|
||||
@ -94,7 +92,7 @@ const useInfiniteScroll = <ListId extends string | number>(
|
||||
}
|
||||
});
|
||||
|
||||
return isDisabled ? [listIds] : [viewportIdsRef.current, getMore];
|
||||
return isDisabled ? [listIds] : [currentStateRef.current?.viewportIds, getMore];
|
||||
};
|
||||
|
||||
function getViewportSlice<ListId extends string | number>(
|
||||
@ -124,7 +122,12 @@ function getViewportSlice<ListId extends string | number>(
|
||||
break;
|
||||
}
|
||||
|
||||
return { newViewportIds, areSomeLocal, areAllLocal };
|
||||
return {
|
||||
newViewportIds,
|
||||
areSomeLocal,
|
||||
areAllLocal,
|
||||
newIsOnTop: newViewportIds[0] === sourceIds[0],
|
||||
};
|
||||
}
|
||||
|
||||
export default useInfiniteScroll;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user