@@ -713,6 +713,7 @@ const MessageListWithContext = (props: MessageListPropsWithContext) => {
713713 try {
714714 if ( indexOfParentInMessageList === - 1 ) {
715715 await loadChannelAroundMessage ( { messageId } ) ;
716+ return ;
716717 } else {
717718 if ( ! flatListRef . current ) {
718719 return ;
@@ -728,6 +729,7 @@ const MessageListWithContext = (props: MessageListPropsWithContext) => {
728729 index : indexOfParentInMessageList ,
729730 viewPosition : 0.5 , // try to place message in the center of the screen
730731 } ) ;
732+ return ;
731733 }
732734 } catch ( e ) {
733735 console . warn ( 'Error while scrolling to message' , e ) ;
@@ -743,13 +745,34 @@ const MessageListWithContext = (props: MessageListPropsWithContext) => {
743745 return ;
744746 }
745747 scrollToDebounceTimeoutRef . current = setTimeout ( async ( ) => {
746- await goToMessage ( targetedMessage ) ;
748+ const indexOfParentInMessageList = processedMessageList . findIndex (
749+ ( message ) => message ?. id === targetedMessage ,
750+ ) ;
751+
752+ // the message we want to scroll to has not been loaded in the state yet
753+ if ( indexOfParentInMessageList === - 1 ) {
754+ await loadChannelAroundMessage ( { messageId : targetedMessage , setTargetedMessage } ) ;
755+ } else {
756+ if ( ! flatListRef . current ) {
757+ return ;
758+ }
759+ // By a fresh scroll we should clear the retries for the previous failed scroll
760+ clearTimeout ( scrollToDebounceTimeoutRef . current ) ;
761+ clearTimeout ( failScrollTimeoutId . current ) ;
762+ // reset the retry count
763+ scrollToIndexFailedRetryCountRef . current = 0 ;
764+ // now scroll to it
765+ flatListRef . current . scrollToIndex ( {
766+ animated : true ,
767+ index : indexOfParentInMessageList ,
768+ viewPosition : 0.5 , // try to place message in the center of the screen
769+ } ) ;
770+ setTargetedMessage ( undefined ) ;
771+ }
747772 } , WAIT_FOR_SCROLL_TIMEOUT ) ;
748773
749- return ( ) => {
750- clearTimeout ( scrollToDebounceTimeoutRef . current ) ;
751- } ;
752- } , [ goToMessage , targetedMessage ] ) ;
774+ // eslint-disable-next-line react-hooks/exhaustive-deps
775+ } , [ targetedMessage ] ) ;
753776
754777 const renderItem = useCallback (
755778 ( { index, item : message } : { index : number ; item : LocalMessage } ) => {
0 commit comments