@@ -813,7 +813,6 @@ export function CometChatConversations(props: ConversationsProps) {
813813 const fetchNextIdRef = useRef ( "" ) ;
814814 const errorHandler = useCometChatErrorHandler ( onError ) ;
815815 const attachListenerOnFetch = useRef < boolean > ( false ) ;
816- const isConnectionReestablished = useRef < boolean > ( false ) ;
817816 const [ activeConversationState , setActiveConversationState ] = useState ( activeConversation ) ;
818817 const customSoundForMessagesRef = useRefSync ( customSoundForMessages ) ;
819818
@@ -823,7 +822,6 @@ export function CometChatConversations(props: ConversationsProps) {
823822 state . isFirstReload = false ;
824823 }
825824 } ) ( ) ;
826-
827825 /**
828826 * Initiates a fetch request and appends the fetched conversations to the `conversationList` state
829827 *
@@ -833,28 +831,29 @@ export function CometChatConversations(props: ConversationsProps) {
833831 * @param fetchId - Fetch Id to decide if the fetched data should be appended to the `conversationList` state
834832 */
835833 const fetchNextAndAppendConversations = useCallback (
836- async ( fetchId : string ) : Promise < void > => {
834+ async ( fetchId : string , isConnected : boolean = false ) : Promise < void > => {
837835 try {
838836 const conversationManager = conversationsManagerRef . current ;
839837 if ( ! conversationManager ) {
840838 return ;
841839 }
842- let initialState = isConnectionReestablished . current
840+ let initialState = isConnected && ! attachListenerOnFetch . current && state . conversationList . length > 0
843841 ? States . loaded
844842 : States . loading ;
845843 dispatch ( { type : "setFetchState" , fetchState : initialState } ) ;
846844
847845 const conversations = await conversationManager . fetchNext ( ) ;
848846
849847 if ( conversations . length !== 0 && fetchNextIdRef . current === fetchId ) {
850- let removeOldConversation = isConnectionReestablished . current
848+ let removeOldConversation = isConnected
851849 ? true
852850 : false ;
853851 dispatch ( {
854852 type : "appendConversations" ,
855853 conversations,
856854 removeOldConversation,
857855 } ) ;
856+
858857 }
859858 if ( attachListenerOnFetch . current ) {
860859 ConversationsManager . attachConnestionListener ( ( ) => {
@@ -863,18 +862,18 @@ export function CometChatConversations(props: ConversationsProps) {
863862 errorHandler
864863
865864 } ) ;
866- isConnectionReestablished . current = true ;
867865 fetchNextAndAppendConversations (
868866 ( fetchNextIdRef . current =
869- "initialFetchNext_" + String ( Date . now ( ) ) )
867+ "initialFetchNext_" + String ( Date . now ( ) ) ) ,
868+ true
870869 ) ;
871870 } ) ;
872- attachListenerOnFetch . current = false ;
873871 }
874- if ( ! isConnectionReestablished . current ) {
875- dispatch ( { type : "setFetchState" , fetchState : States . loaded } ) ;
876- } else {
877- isConnectionReestablished . current = false ;
872+ if ( attachListenerOnFetch . current && conversations . length == 0 && state . conversationList . length == 0 ) {
873+ dispatch ( { type : "setFetchState" , fetchState : States . empty } ) ;
874+ }
875+ if ( attachListenerOnFetch . current ) {
876+ attachListenerOnFetch . current = false
878877 }
879878 } catch ( error ) {
880879 if ( state . conversationList . length <= 0 ) {
@@ -1797,12 +1796,8 @@ export function CometChatConversations(props: ConversationsProps) {
17971796 )
17981797 }
17991798 showSectionHeader = { false }
1800- state = {
1801- state . fetchState === States . loaded &&
1802- state . conversationList . length === 0
1803- ? States . empty
1804- : state . fetchState
1805- }
1799+ state = { state . fetchState }
1800+
18061801 loadingView = { getLoadingView ( ) }
18071802 emptyView = { getEmptyView ( ) }
18081803 errorView = { getErrorView ( ) }
0 commit comments