Skip to content

Commit fe0a416

Browse files
committed
Revert "fix: save a render cycle through restructuring"
This reverts commit 38ca199.
1 parent 84cfe17 commit fe0a416

File tree

2 files changed

+44
-20
lines changed

2 files changed

+44
-20
lines changed

package/src/components/MessageList/MessageFlashList.tsx

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -400,17 +400,26 @@ const MessageFlashListWithContext = (props: MessageFlashListPropsWithContext) =>
400400
}
401401
}, [disabled]);
402402

403-
const goToMessage = useStableCallback(async (messageId: string) => {
403+
/**
404+
* Check if a messageId needs to be scrolled to after list loads, and scroll to it
405+
* Note: This effect fires on every list change with a small debounce so that scrolling isnt abrupted by an immediate rerender
406+
*/
407+
useEffect(() => {
408+
if (!targetedMessage) {
409+
return;
410+
}
411+
404412
const indexOfParentInMessageList = processedMessageList.findIndex(
405-
(message) => message?.id === messageId,
413+
(message) => message?.id === targetedMessage,
406414
);
407415

408416
// the message we want to scroll to has not been loaded in the state yet
409417
if (indexOfParentInMessageList === -1) {
410-
await loadChannelAroundMessage({ messageId, setTargetedMessage });
418+
loadChannelAroundMessage({ messageId: targetedMessage, setTargetedMessage });
411419
} else {
412420
scrollToDebounceTimeoutRef.current = setTimeout(() => {
413421
clearTimeout(scrollToDebounceTimeoutRef.current);
422+
414423
// now scroll to it
415424
flashListRef.current?.scrollToIndex({
416425
animated: true,
@@ -420,19 +429,11 @@ const MessageFlashListWithContext = (props: MessageFlashListPropsWithContext) =>
420429
setTargetedMessage(undefined);
421430
}, WAIT_FOR_SCROLL_TIMEOUT);
422431
}
423-
});
432+
}, [loadChannelAroundMessage, processedMessageList, setTargetedMessage, targetedMessage]);
424433

425-
/**
426-
* Check if a messageId needs to be scrolled to after list loads, and scroll to it
427-
* Note: This effect fires on every list change with a small debounce so that scrolling isnt abrupted by an immediate rerender
428-
*/
429-
useEffect(() => {
430-
if (!targetedMessage) {
431-
return;
432-
}
433-
434-
goToMessage(targetedMessage);
435-
}, [targetedMessage, goToMessage]);
434+
const goToMessage = useStableCallback((messageId: string) => {
435+
setTargetedMessage(messageId);
436+
});
436437

437438
useEffect(() => {
438439
/**

package/src/components/MessageList/MessageList.tsx

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,7 @@ const MessageListWithContext = (props: MessageListPropsWithContext) => {
713713
try {
714714
if (indexOfParentInMessageList === -1) {
715715
await loadChannelAroundMessage({ messageId });
716+
return;
716717
} else {
717718
if (!flatListRef.current) {
718719
return;
@@ -728,6 +729,7 @@ const MessageListWithContext = (props: MessageListPropsWithContext) => {
728729
index: indexOfParentInMessageList,
729730
viewPosition: 0.5, // try to place message in the center of the screen
730731
});
732+
return;
731733
}
732734
} catch (e) {
733735
console.warn('Error while scrolling to message', e);
@@ -743,13 +745,34 @@ const MessageListWithContext = (props: MessageListPropsWithContext) => {
743745
return;
744746
}
745747
scrollToDebounceTimeoutRef.current = setTimeout(async () => {
746-
await goToMessage(targetedMessage);
748+
const indexOfParentInMessageList = processedMessageList.findIndex(
749+
(message) => message?.id === targetedMessage,
750+
);
751+
752+
// the message we want to scroll to has not been loaded in the state yet
753+
if (indexOfParentInMessageList === -1) {
754+
await loadChannelAroundMessage({ messageId: targetedMessage, setTargetedMessage });
755+
} else {
756+
if (!flatListRef.current) {
757+
return;
758+
}
759+
// By a fresh scroll we should clear the retries for the previous failed scroll
760+
clearTimeout(scrollToDebounceTimeoutRef.current);
761+
clearTimeout(failScrollTimeoutId.current);
762+
// reset the retry count
763+
scrollToIndexFailedRetryCountRef.current = 0;
764+
// now scroll to it
765+
flatListRef.current.scrollToIndex({
766+
animated: true,
767+
index: indexOfParentInMessageList,
768+
viewPosition: 0.5, // try to place message in the center of the screen
769+
});
770+
setTargetedMessage(undefined);
771+
}
747772
}, WAIT_FOR_SCROLL_TIMEOUT);
748773

749-
return () => {
750-
clearTimeout(scrollToDebounceTimeoutRef.current);
751-
};
752-
}, [goToMessage, targetedMessage]);
774+
// eslint-disable-next-line react-hooks/exhaustive-deps
775+
}, [targetedMessage]);
753776

754777
const renderItem = useCallback(
755778
({ index, item: message }: { index: number; item: LocalMessage }) => {

0 commit comments

Comments
 (0)