@@ -108,35 +108,18 @@ export const usePaginatedChannels = <
108108 } ;
109109
110110 try {
111- // If failed messages were present in DB it would be in the channel state now and be overwritten by the queryChannels call
112- // So we store them in a separate object and add them back to the channel state after the queryChannels call
113- const failedMessagesInDb : Map < string , MessageResponse < StreamChatGenerics > [ ] > = new Map ( ) ;
114- if ( enableOfflineSupport ) {
115- for ( const cid in client . activeChannels ) {
116- const failedMessages = getFailedMessages ( client . activeChannels [ cid ] ) ;
117- if ( failedMessages ) failedMessagesInDb . set ( cid , failedMessages ) ;
118- }
119- }
120-
121111 /**
122112 * We skipInitialization here for handling race condition between ChannelList, Channel (and Thread)
123113 * when they all (may) update the channel state at the same time (when connection state recovers)
124114 * TODO: if we move the channel state to a single context and share it between ChannelList, Channel and Thread we can remove this
125115 */
126116 const channelQueryResponse = await client . queryChannels ( filters , sort , newOptions , {
127- skipInitialization : activeChannels . current ,
117+ skipInitialization : enableOfflineSupport ? undefined : activeChannels . current ,
128118 } ) ;
129119 if ( isQueryStale ( ) || ! isMountedRef . current ) {
130120 return ;
131121 }
132122
133- if ( failedMessagesInDb . size ) {
134- for ( const cid in client . activeChannels ) {
135- const msgsToAdd = failedMessagesInDb . get ( cid ) ;
136- if ( msgsToAdd ) client . activeChannels [ cid ] . state . addMessagesSorted ( msgsToAdd ) ;
137- }
138- }
139-
140123 const newChannels =
141124 queryType === 'loadChannels' && ! staticChannelsActive && channels
142125 ? [ ...channels , ...channelQueryResponse ]
@@ -224,11 +207,12 @@ export const usePaginatedChannels = <
224207 } ) ;
225208
226209 if ( channelsFromDB ) {
227- setChannels (
228- client . hydrateActiveChannels ( channelsFromDB , {
229- offlineMode : true ,
230- } ) ,
231- ) ;
210+ const offlineChannels = client . hydrateActiveChannels ( channelsFromDB , {
211+ offlineMode : true ,
212+ skipInitialization : [ ] , // passing empty array will clear out the existing messages from channel state, this removes the possibility of duplicate messages
213+ } ) ;
214+
215+ setChannels ( offlineChannels ) ;
232216 setStaticChannelsActive ( true ) ;
233217 }
234218 } catch ( e ) {
@@ -292,21 +276,3 @@ export const usePaginatedChannels = <
292276 staticChannelsActive,
293277 } ;
294278} ;
295-
296- function getFailedMessages <
297- StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics ,
298- > ( channel : Channel < StreamChatGenerics > ) : MessageResponse < StreamChatGenerics > [ ] | undefined {
299- const failedMsgs = channel . state . messages . filter ( ( m ) => m . status === MessageStatusTypes . FAILED ) ;
300- if ( failedMsgs . length ) {
301- return failedMsgs . map (
302- ( m ) =>
303- ( {
304- ...m ,
305- created_at : m . created_at . toISOString ( ) ,
306- pinned_at : m . pinned_at ? m . pinned_at . toISOString ( ) : null ,
307- updated_at : m . updated_at . toISOString ( ) ,
308- } as MessageResponse < StreamChatGenerics > ) ,
309- ) ;
310- }
311- return undefined ;
312- }
0 commit comments