Skip to content

Commit 38ca199

Browse files
committed
fix: save a render cycle through restructuring
1 parent 0c37e4c commit 38ca199

File tree

2 files changed

+20
-44
lines changed

2 files changed

+20
-44
lines changed

package/src/components/MessageList/MessageFlashList.tsx

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

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-
403+
const goToMessage = useStableCallback(async (messageId: string) => {
412404
const indexOfParentInMessageList = processedMessageList.findIndex(
413-
(message) => message?.id === targetedMessage,
405+
(message) => message?.id === messageId,
414406
);
415407

416408
// the message we want to scroll to has not been loaded in the state yet
417409
if (indexOfParentInMessageList === -1) {
418-
loadChannelAroundMessage({ messageId: targetedMessage, setTargetedMessage });
410+
await loadChannelAroundMessage({ messageId, setTargetedMessage });
419411
} else {
420412
scrollToDebounceTimeoutRef.current = setTimeout(() => {
421413
clearTimeout(scrollToDebounceTimeoutRef.current);
422-
423414
// now scroll to it
424415
flashListRef.current?.scrollToIndex({
425416
animated: true,
@@ -429,12 +420,20 @@ const MessageFlashListWithContext = (props: MessageFlashListPropsWithContext) =>
429420
setTargetedMessage(undefined);
430421
}, WAIT_FOR_SCROLL_TIMEOUT);
431422
}
432-
}, [loadChannelAroundMessage, processedMessageList, setTargetedMessage, targetedMessage]);
433-
434-
const goToMessage = useStableCallback((messageId: string) => {
435-
setTargetedMessage(messageId);
436423
});
437424

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]);
436+
438437
useEffect(() => {
439438
/**
440439
* Condition to check if a message is removed from MessageList.

package/src/components/MessageList/MessageList.tsx

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,6 @@ const MessageListWithContext = (props: MessageListPropsWithContext) => {
713713
try {
714714
if (indexOfParentInMessageList === -1) {
715715
await loadChannelAroundMessage({ messageId });
716-
return;
717716
} else {
718717
if (!flatListRef.current) {
719718
return;
@@ -729,7 +728,6 @@ const MessageListWithContext = (props: MessageListPropsWithContext) => {
729728
index: indexOfParentInMessageList,
730729
viewPosition: 0.5, // try to place message in the center of the screen
731730
});
732-
return;
733731
}
734732
} catch (e) {
735733
console.warn('Error while scrolling to message', e);
@@ -745,34 +743,13 @@ const MessageListWithContext = (props: MessageListPropsWithContext) => {
745743
return;
746744
}
747745
scrollToDebounceTimeoutRef.current = setTimeout(async () => {
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-
}
746+
await goToMessage(targetedMessage);
772747
}, WAIT_FOR_SCROLL_TIMEOUT);
773748

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

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

0 commit comments

Comments
 (0)