Skip to content

Commit 9df519d

Browse files
authored
fix: auto scroll to bottom when the message was deleted or removed from the list (#3187)
* fix: auto scroll to bottom when the message was deleted or removed from the list * fix: auto scroll to bottom when the message was deleted or removed from the list * fix: update comment
1 parent 1243e11 commit 9df519d

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

package/src/components/MessageList/MessageList.tsx

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,13 @@ const MessageListWithContext = (props: MessageListPropsWithContext) => {
575575
]);
576576

577577
useEffect(() => {
578+
/**
579+
* Condition to check if a message is removed from MessageList.
580+
* Eg: This would happen when giphy search is cancelled, message is deleted with visibility "never" etc.
581+
* If such a case arises, we scroll to bottom.
582+
*/
583+
const isMessageRemovedFromMessageList =
584+
messageListLengthAfterUpdate < messageListLengthBeforeUpdate.current;
578585
/**
579586
* Scroll down when
580587
* created_at timestamp of top message before update is lesser than created_at timestamp of top message after update - channel has resynced
@@ -584,18 +591,10 @@ const MessageListWithContext = (props: MessageListPropsWithContext) => {
584591
return;
585592
}
586593

587-
/**
588-
* Condition to check if a message is removed from MessageList.
589-
* Eg: This would happen when giphy search is cancelled, etc.
590-
* If such a case arises, we scroll to bottom.
591-
*/
592-
const isMessageRemovedFromMessageList =
593-
messageListLengthAfterUpdate < messageListLengthBeforeUpdate.current;
594594
if (
595-
isMessageRemovedFromMessageList ||
596-
(topMessageBeforeUpdate.current?.created_at &&
597-
topMessageAfterUpdate?.created_at &&
598-
topMessageBeforeUpdate.current.created_at < topMessageAfterUpdate.created_at)
595+
topMessageBeforeUpdate.current?.created_at &&
596+
topMessageAfterUpdate?.created_at &&
597+
topMessageBeforeUpdate.current.created_at < topMessageAfterUpdate.created_at
599598
) {
600599
channelResyncScrollSet.current = false;
601600
setScrollToBottomButtonVisible(false);
@@ -615,8 +614,7 @@ const MessageListWithContext = (props: MessageListPropsWithContext) => {
615614
}
616615
};
617616

618-
// TODO: Think about if this is really needed?
619-
if (threadList) {
617+
if (threadList || isMessageRemovedFromMessageList) {
620618
scrollToBottomIfNeeded();
621619
}
622620

0 commit comments

Comments
 (0)