Skip to content

feat: Maintain Scroll Position When Closing a Thread #973

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/react/src/views/ChatBody/ChatBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,11 @@ const ChatBody = ({
};

useEffect(() => {
if (messageListRef.current) {
messageListRef.current.scrollTop = messageListRef.current.scrollHeight;
const lastMessage = messages[0];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please explain this change ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scroll to the bottom only if the last message is not about pinning. When you pin a message, A new message is sent to chat about the pinning. Need to ignore it and not scroll down to it.
image

if (lastMessage && lastMessage.t !== 'message_pinned') {
scrollToBottom();
}
}, [messages]);
}, [messages.length]);

useEffect(() => {
checkOverflow();
Expand Down
18 changes: 17 additions & 1 deletion packages/react/src/views/ChatHeader/ChatHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@ const ChatHeader = ({

const closeThread = useMessageStore((state) => state.closeThread);

const handleCloseThread = useCallback(() => {
const threadMessageId = threadMainMessage?._id;
closeThread();
setTimeout(() => {
const element = document.getElementById(
`ec-message-body-${threadMessageId}`
);
if (element) {
element.scrollIntoView({
behavior: 'instant',
block: 'start',
});
}
}, 300);
}, [closeThread, threadMainMessage]);

const setShowMembers = useMemberStore((state) => state.setShowMembers);
const setShowSearch = useSearchMessageStore((state) => state.setShowSearch);
const setShowPinned = usePinnedMessageStore((state) => state.setShowPinned);
Expand Down Expand Up @@ -430,7 +446,7 @@ const ChatHeader = ({
{isThreadOpen && (
<DynamicHeader
title={threadMainMessage}
handleClose={closeThread}
handleClose={handleCloseThread}
iconName="arrow-back"
/>
)}
Expand Down