Skip to content

Commit ccae3b4

Browse files
renaming from messageListLength to messageListLengthAfterUpdate
1 parent a99c687 commit ccae3b4

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

src/components/MessageList/MessageList.tsx

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,16 @@ const MessageListWithContext = <
345345
threadList,
346346
});
347347
const messageListLengthBeforeUpdate = useRef(0);
348-
const messageListLength = messageList.length;
348+
const messageListLengthAfterUpdate = messageList.length;
349+
350+
/**
351+
* We need topMessage and channelLastRead values to set the initial scroll position.
352+
* So these values only get used if `initialScrollToFirstUnreadMessage` prop is true.
353+
*/
354+
const topMessageBeforeUpdate = useRef<
355+
FormatMessageResponse<At, Ch, Co, Me, Re, Us>
356+
>();
357+
const topMessageAfterUpdate = channel?.state.messages[0];
349358

350359
const [autoscrollToTop, setAutoscrollToTop] = useState(false);
351360

@@ -381,13 +390,6 @@ const MessageListWithContext = <
381390
*/
382391
const getLastReadSafely = () =>
383392
channel?.initialized ? channel.lastRead() : undefined;
384-
/**
385-
* We need topMessage and channelLastRead values to set the initial scroll position.
386-
* So these values only get used if `initialScrollToFirstUnreadMessage` prop is true.
387-
*/
388-
const topMessageBeforeUpdate = useRef<
389-
FormatMessageResponse<At, Ch, Co, Me, Re, Us>
390-
>();
391393

392394
const channelLastRead = useRef(getLastReadSafely());
393395

@@ -489,7 +491,6 @@ const MessageListWithContext = <
489491
}, [loading]);
490492

491493
useEffect(() => {
492-
const topMessageAfterUpdate = channel?.state.messages[0];
493494
const lastReceivedMessage = getLastReceivedMessage(messageList);
494495

495496
const hasNewMessage = lastReceivedId !== lastReceivedMessage?.id;
@@ -510,7 +511,7 @@ const MessageListWithContext = <
510511

511512
if (
512513
(hasNewMessage && isMyMessage) ||
513-
messageListLength < messageListLengthBeforeUpdate.current ||
514+
messageListLengthAfterUpdate < messageListLengthBeforeUpdate.current ||
514515
(topMessageBeforeUpdate.current &&
515516
topMessageAfterUpdate &&
516517
topMessageBeforeUpdate.current.created_at <
@@ -521,8 +522,10 @@ const MessageListWithContext = <
521522
resetPaginationTrackers();
522523

523524
if (flatListRef.current) {
524-
flatListRef.current?.scrollToOffset({
525-
offset: 0,
525+
setTimeout(() => {
526+
flatListRef.current?.scrollToOffset({
527+
offset: 0,
528+
});
526529
});
527530
}
528531
setTimeout(() => {
@@ -550,17 +553,17 @@ const MessageListWithContext = <
550553
channelLastRead.current = getLastReadSafely();
551554
}
552555

553-
messageListLengthBeforeUpdate.current = messageListLength;
556+
messageListLengthBeforeUpdate.current = messageListLengthAfterUpdate;
554557
topMessageBeforeUpdate.current = topMessageAfterUpdate;
555-
}, [messageListLength]);
558+
}, [messageListLengthAfterUpdate, topMessageAfterUpdate?.id]);
556559

557560
useEffect(() => {
558561
if (!channel?.state.isUpToDate && autoscrollToTop) {
559562
setAutoscrollToTop(false);
560563
} else if (channel?.state.isUpToDate && !autoscrollToTop) {
561564
setAutoscrollToTop(true);
562565
}
563-
}, [messageListLength]);
566+
}, [messageListLengthAfterUpdate]);
564567

565568
const renderItem = ({
566569
index,
@@ -980,7 +983,7 @@ const MessageListWithContext = <
980983
<View style={styles.stickyHeader}>
981984
{StickyHeader ? (
982985
<StickyHeader dateString={stickyHeaderDateToRender} />
983-
) : messageListLength ? (
986+
) : messageListLengthAfterUpdate ? (
984987
<DateHeader dateString={stickyHeaderDateToRender} />
985988
) : null}
986989
</View>

0 commit comments

Comments
 (0)