@@ -114,7 +114,7 @@ import {
114114} from '../../utils/utils' ;
115115import { NotificationAnnouncer } from '../Accessibility/NotificationAnnouncer' ;
116116import { AttachmentPicker } from '../AttachmentPicker/AttachmentPicker' ;
117- import type { KeyboardCompatibleViewProps } from '../KeyboardCompatibleView/KeyboardControllerAvoidingView ' ;
117+ import type { KeyboardCompatibleViewProps } from '../KeyboardCompatibleView/KeyboardCompatibleView ' ;
118118import { Emoji } from '../MessageMenu/EmojiPickerList' ;
119119import { emojis } from '../MessageMenu/emojis' ;
120120import { toUnicodeScalarString } from '../MessageMenu/utils/toUnicodeScalarString' ;
@@ -496,7 +496,7 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
496496 thread : threadFromProps ,
497497 threadList,
498498 threadMessages,
499- topInset,
499+ topInset = 0 ,
500500 isOnline,
501501 maximumMessageLimit,
502502 initializeOnMount = true ,
@@ -567,6 +567,18 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
567567 channel,
568568 } ) ;
569569
570+ const shouldLoadInitialChannelAtFirstUnreadMessage = useStableCallback ( ( unreadCount ?: number ) => {
571+ if ( messageId || ! initialScrollToFirstUnreadMessage || ! client . user ) {
572+ return false ;
573+ }
574+
575+ return ( unreadCount ?? channel . countUnread ( ) ) > scrollToFirstUnreadThreshold ;
576+ } ) ;
577+
578+ const hasPendingInitialTargetLoad = useStableCallback ( ( ) => {
579+ return ! ! messageId || shouldLoadInitialChannelAtFirstUnreadMessage ( ) ;
580+ } ) ;
581+
570582 const { setMessages : copyMessagesStateFromChannel , viewabilityChangedCallback } =
571583 usePrunableMessageList ( { maximumMessageLimit, setMessages : rawCopyMessagesStateFromChannel } ) ;
572584
@@ -693,6 +705,7 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
693705 const initChannel = async ( ) => {
694706 setLastRead ( new Date ( ) ) ;
695707 const unreadCount = channel . countUnread ( ) ;
708+ const shouldLoadAtFirstUnread = shouldLoadInitialChannelAtFirstUnreadMessage ( unreadCount ) ;
696709 if ( ! channel || ! shouldSyncChannel ) {
697710 return ;
698711 }
@@ -722,13 +735,14 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
722735
723736 if ( messageId ) {
724737 await loadChannelAroundMessage ( { messageId, setTargetedMessage } ) ;
725- } else if (
726- initialScrollToFirstUnreadMessage &&
727- client . user &&
728- unreadCount > scrollToFirstUnreadThreshold
729- ) {
738+ } else if ( shouldLoadAtFirstUnread ) {
739+ const clientUserId = client . user ?. id ;
740+ if ( ! clientUserId ) {
741+ return ;
742+ }
743+
730744 // eslint-disable-next-line @typescript-eslint/no-unused-vars
731- const { user, ...ownReadState } = channel . state . read [ client . user . id ] ;
745+ const { user, ...ownReadState } = channel . state . read [ clientUserId ] ;
732746
733747 await loadChannelAtFirstUnreadMessage ( {
734748 channelUnreadState : ownReadState ,
@@ -1578,6 +1592,7 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
15781592 setChannelUnreadState,
15791593 setLastRead,
15801594 setTargetedMessage,
1595+ hasPendingInitialTargetLoad,
15811596 targetedMessage,
15821597 threadList,
15831598 uploadAbortControllerRef,
0 commit comments