From 0b8c9936f3a4b88e4bda8ee25debff636a913a72 Mon Sep 17 00:00:00 2001 From: Reed <3893871+dharit-tan@users.noreply.github.com> Date: Wed, 7 Jun 2023 16:35:44 -0400 Subject: [PATCH] [PAY-1356] Mobile chats scroll to bottom when re-entering chat screen (#3536) --- .../src/screens/chat-screen/ChatScreen.tsx | 36 +++++++++++++------ 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/packages/mobile/src/screens/chat-screen/ChatScreen.tsx b/packages/mobile/src/screens/chat-screen/ChatScreen.tsx index 50caae4326a..c19433193e3 100644 --- a/packages/mobile/src/screens/chat-screen/ChatScreen.tsx +++ b/packages/mobile/src/screens/chat-screen/ChatScreen.tsx @@ -275,16 +275,6 @@ export const ChatScreen = () => { } }, [chatId, dispatch]) - useFocusEffect( - useCallback(() => { - return () => { - if (chatId) { - dispatch(markChatAsRead({ chatId })) - } - } - }, [chatId, dispatch]) - ) - // Fetch all permissions, blockers/blockees, and recheck_permissions flag useEffect(() => { dispatch(fetchBlockees()) @@ -332,6 +322,32 @@ export const ChatScreen = () => { } }, [earliestUnreadIndex, chatMessages]) + // Scroll to bottom when user navigates to this screen and there are no unread + // messages, because if there are we want to scroll to the earliest unread message. + useFocusEffect( + useCallback(() => { + if (chat?.unread_message_count === 0) { + flatListRef.current?.scrollToOffset({ offset: 0 }) + } + }, [chat?.unread_message_count]) + ) + + // Mark chat as read when user leaves this screen + useFocusEffect( + useCallback(() => { + return () => { + if (chatId) { + dispatch(markChatAsRead({ chatId })) + } + // Hacky way to get unread indicator to disappear when user navigates away + // then returns to this screen while staying in the same navigation stack. + if (chatFrozenRef.current) { + chatFrozenRef.current.unread_message_count = 0 + } + } + }, [chatId, dispatch]) + ) + const latestMessage = chatMessages.length > 0 ? chatMessages[0] : null // If most recent message changes and we are scrolled up, fire a toast