@@ -4,13 +4,13 @@ import { ScrollViewProps, StyleSheet, View, ViewabilityConfig, ViewToken } from
44import { FlashList , FlashListProps , FlashListRef } from '@shopify/flash-list' ;
55import type { Channel , Event , LocalMessage , MessageResponse } from 'stream-chat' ;
66
7- import { useMessageFlashList } from './hooks/useMessageFlashList' ;
8-
7+ import { useMessageList } from './hooks/useMessageList' ;
98import { useShouldScrollToRecentOnNewOwnMessage } from './hooks/useShouldScrollToRecentOnNewOwnMessage' ;
109import { InlineLoadingMoreIndicator } from './InlineLoadingMoreIndicator' ;
1110import { InlineLoadingMoreRecentIndicator } from './InlineLoadingMoreRecentIndicator' ;
1211import { InlineLoadingMoreRecentThreadIndicator } from './InlineLoadingMoreRecentThreadIndicator' ;
13- import { getLastReceivedMessage } from './utils/getLastReceivedMessage' ;
12+
13+ import { getLastReceivedMessageFlashList } from './utils/getLastReceivedMessageFlashList' ;
1414
1515import {
1616 AttachmentPickerContextValue ,
@@ -263,7 +263,6 @@ const MessageListFlashListWithContext = (props: MessageListFlashListPropsWithCon
263263 */
264264 const scrollToDebounceTimeoutRef = useRef < ReturnType < typeof setTimeout > > ( undefined ) ;
265265
266- const messageListLengthBeforeUpdate = useRef ( 0 ) ;
267266 const channelResyncScrollSet = useRef < boolean > ( true ) ;
268267 const { theme } = useTheme ( ) ;
269268
@@ -281,7 +280,8 @@ const MessageListFlashListWithContext = (props: MessageListFlashListPropsWithCon
281280 ) ;
282281
283282 const { dateSeparatorsRef, messageGroupStylesRef, processedMessageList, rawMessageList } =
284- useMessageFlashList ( {
283+ useMessageList ( {
284+ isFlashList : true ,
285285 noGroupByUser,
286286 threadList,
287287 } ) ;
@@ -295,6 +295,7 @@ const MessageListFlashListWithContext = (props: MessageListFlashListPropsWithCon
295295
296296 const latestNonCurrentMessageBeforeUpdateRef = useRef < LocalMessage > ( undefined ) ;
297297
298+ const messageListLengthBeforeUpdate = useRef ( 0 ) ;
298299 const messageListLengthAfterUpdate = processedMessageList . length ;
299300
300301 const shouldScrollToRecentOnNewOwnMessageRef = useShouldScrollToRecentOnNewOwnMessage (
@@ -303,7 +304,7 @@ const MessageListFlashListWithContext = (props: MessageListFlashListPropsWithCon
303304 ) ;
304305
305306 const lastReceivedId = useMemo (
306- ( ) => getLastReceivedMessage ( processedMessageList ) ?. id ,
307+ ( ) => getLastReceivedMessageFlashList ( processedMessageList ) ?. id ,
307308 [ processedMessageList ] ,
308309 ) ;
309310
@@ -338,22 +339,18 @@ const MessageListFlashListWithContext = (props: MessageListFlashListPropsWithCon
338339 loadChannelAroundMessage ( { messageId : targetedMessage , setTargetedMessage } ) ;
339340 } else {
340341 scrollToDebounceTimeoutRef . current = setTimeout ( ( ) => {
341- if ( ! flashListRef . current ) {
342- return ;
343- }
344342 clearTimeout ( scrollToDebounceTimeoutRef . current ) ;
345343
346344 // now scroll to it
347- flashListRef . current . scrollToIndex ( {
345+ flashListRef . current ? .scrollToIndex ( {
348346 animated : true ,
349347 index : indexOfParentInMessageList ,
350348 viewPosition : 0.5 ,
351349 } ) ;
352350 setTargetedMessage ( undefined ) ;
353351 } , WAIT_FOR_SCROLL_TIMEOUT ) ;
354352 }
355- // eslint-disable-next-line react-hooks/exhaustive-deps
356- } , [ targetedMessage ] ) ;
353+ } , [ loadChannelAroundMessage , processedMessageList , setTargetedMessage , targetedMessage ] ) ;
357354
358355 const goToMessage = useStableCallback ( async ( messageId : string ) => {
359356 const indexOfParentInMessageList = processedMessageList . findIndex (
@@ -460,7 +457,6 @@ const MessageListFlashListWithContext = (props: MessageListFlashListPropsWithCon
460457
461458 if ( ! didMergeMessageSetsWithNoUpdates ) {
462459 const shouldScrollToRecentOnNewOwnMessage = shouldScrollToRecentOnNewOwnMessageRef . current ( ) ;
463-
464460 // we should scroll to bottom where ever we are now
465461 // as we have sent a new own message
466462 if ( shouldScrollToRecentOnNewOwnMessage ) {
@@ -469,6 +465,9 @@ const MessageListFlashListWithContext = (props: MessageListFlashListPropsWithCon
469465 }
470466 } , [ channel , processedMessageList , shouldScrollToRecentOnNewOwnMessageRef , threadList ] ) ;
471467
468+ /**
469+ * Effect to scroll to the bottom of the message list when a new message is received if the scroll to bottom button is not visible.
470+ */
472471 useEffect ( ( ) => {
473472 const handleEvent = ( event : Event ) => {
474473 if ( event . message ?. user ?. id !== client . userID ) {
0 commit comments