@@ -49,8 +49,7 @@ import {
4949import { mergeThemes , useTheme } from '../../contexts/themeContext/ThemeContext' ;
5050import { ThreadContextValue , useThreadContext } from '../../contexts/threadContext/ThreadContext' ;
5151
52- import { useStableCallback , useStateStore } from '../../hooks' ;
53- import { ChannelUnreadStateStoreType } from '../../state-store/channel-unread-state' ;
52+ import { useStableCallback } from '../../hooks' ;
5453import { FileTypes } from '../../types/types' ;
5554import { MessageWrapper } from '../Message/MessageSimple/MessageWrapper' ;
5655
@@ -259,13 +258,6 @@ const renderItem = ({ item: message }: { item: LocalMessage }) => {
259258 return < MessageWrapper message = { message } /> ;
260259} ;
261260
262- const channelUnreadStateSelector = ( state : ChannelUnreadStateStoreType ) => ( {
263- first_unread_message_id : state . channelUnreadState ?. first_unread_message_id ,
264- last_read : state . channelUnreadState ?. last_read ,
265- last_read_message_id : state . channelUnreadState ?. last_read_message_id ,
266- unread_messages : state . channelUnreadState ?. unread_messages ,
267- } ) ;
268-
269261const MessageFlashListWithContext = ( props : MessageFlashListPropsWithContext ) => {
270262 const LoadingMoreRecentIndicator = props . threadList
271263 ? InlineLoadingMoreRecentThreadIndicator
@@ -320,10 +312,6 @@ const MessageFlashListWithContext = (props: MessageFlashListPropsWithContext) =>
320312 UnreadMessagesNotification,
321313 } = props ;
322314 const flashListRef = useRef < FlashListRef < LocalMessage > | null > ( null ) ;
323- const channelUnreadState = useStateStore (
324- channelUnreadStateStore . state ,
325- channelUnreadStateSelector ,
326- ) ;
327315
328316 const [ hasMoved , setHasMoved ] = useState ( false ) ;
329317 const [ scrollToBottomButtonVisible , setScrollToBottomButtonVisible ] = useState ( false ) ;
@@ -566,6 +554,7 @@ const MessageFlashListWithContext = (props: MessageFlashListPropsWithContext) =>
566554 */
567555 useEffect ( ( ) => {
568556 const shouldMarkRead = ( ) => {
557+ const channelUnreadState = channelUnreadStateStore . channelUnreadState ;
569558 return (
570559 ! channelUnreadState ?. first_unread_message_id &&
571560 ! scrollToBottomButtonVisible &&
@@ -577,6 +566,7 @@ const MessageFlashListWithContext = (props: MessageFlashListPropsWithContext) =>
577566 const handleEvent = async ( event : Event ) => {
578567 const mainChannelUpdated = ! event . message ?. parent_id || event . message ?. show_in_channel ;
579568 const isMyOwnMessage = event . message ?. user ?. id === client . user ?. id ;
569+ const channelUnreadState = channelUnreadStateStore . channelUnreadState ;
580570 // When the scrollToBottomButtonVisible is true, we need to manually update the channelUnreadState when its a received message.
581571 if (
582572 ( scrollToBottomButtonVisible || channelUnreadState ?. first_unread_message_id ) &&
@@ -587,7 +577,7 @@ const MessageFlashListWithContext = (props: MessageFlashListPropsWithContext) =>
587577 setChannelUnreadState ( {
588578 ...channelUnreadState ,
589579 last_read :
590- channelUnreadState . last_read ??
580+ channelUnreadState ? .last_read ??
591581 ( previousUnreadCount === 0 && previousLastMessage ?. created_at
592582 ? new Date ( previousLastMessage . created_at )
593583 : new Date ( 0 ) ) , // not having information about the last read message means the whole channel is unread,
@@ -605,7 +595,7 @@ const MessageFlashListWithContext = (props: MessageFlashListPropsWithContext) =>
605595 } ;
606596 } , [
607597 channel ,
608- channelUnreadState ,
598+ channelUnreadStateStore ,
609599 client . user ?. id ,
610600 markRead ,
611601 scrollToBottomButtonVisible ,
@@ -646,12 +636,19 @@ const MessageFlashListWithContext = (props: MessageFlashListPropsWithContext) =>
646636 * This function should show or hide the unread indicator depending on the
647637 */
648638 const updateStickyUnreadIndicator = useStableCallback ( ( viewableItems : ViewToken [ ] ) => {
649- if ( ! viewableItems . length || ! readEvents ) {
650- setIsUnreadNotificationOpen ( false ) ;
651- return ;
652- }
639+ const channelUnreadState = channelUnreadStateStore . channelUnreadState ;
640+ // we need this check to make sure that regular list change do not trigger
641+ // the unread notification to appear (for example if the old last read messages
642+ // go out of the viewport).
643+ const lastReadMessageId = channelUnreadState ?. last_read_message_id ;
644+ const lastReadMessageVisible = viewableItems . some ( ( item ) => item . item . id === lastReadMessageId ) ;
653645
654- if ( selectedPicker === 'images' ) {
646+ if (
647+ ! viewableItems . length ||
648+ ! readEvents ||
649+ lastReadMessageVisible ||
650+ selectedPicker === 'images'
651+ ) {
655652 setIsUnreadNotificationOpen ( false ) ;
656653 return ;
657654 }
0 commit comments