Skip to content

Commit 4eb0e16

Browse files
authored
fix: restore failed messages present in db (#2111)
1 parent 3039600 commit 4eb0e16

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

package/src/components/ChannelList/hooks/usePaginatedChannels.ts

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { useEffect, useMemo, useRef, useState } from 'react';
22

3-
import type { Channel, ChannelFilters, ChannelOptions, ChannelSort } from 'stream-chat';
3+
import type {
4+
Channel,
5+
ChannelFilters,
6+
ChannelOptions,
7+
ChannelSort,
8+
MessageResponse,
9+
} from 'stream-chat';
410

511
import { useActiveChannelsRefContext } from '../../../contexts/activeChannelsRefContext/ActiveChannelsRefContext';
612
import { useChatContext } from '../../../contexts/chatContext/ChatContext';
@@ -10,6 +16,7 @@ import { getChannelsForFilterSort } from '../../../store/apis/getChannelsForFilt
1016
import type { DefaultStreamChatGenerics } from '../../../types/types';
1117
import { ONE_SECOND_IN_MS } from '../../../utils/date';
1218
import { DBSyncManager } from '../../../utils/DBSyncManager';
19+
import { MessageStatusTypes } from '../../../utils/utils';
1320
import { MAX_QUERY_CHANNELS_LIMIT } from '../utils';
1421

1522
const waitSeconds = (seconds: number) =>
@@ -101,11 +108,13 @@ export const usePaginatedChannels = <
101108
};
102109

103110
try {
104-
const activeChannelIds: string[] = [];
105-
for (const cid in client.activeChannels) {
106-
if (client.activeChannels[cid].id) {
107-
// @ts-ignore
108-
activeChannelIds.push(client.activeChannels[cid].id);
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);
109118
}
110119
}
111120

@@ -121,6 +130,13 @@ export const usePaginatedChannels = <
121130
return;
122131
}
123132

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+
124140
const newChannels =
125141
queryType === 'loadChannels' && !staticChannelsActive && channels
126142
? [...channels, ...channelQueryResponse]
@@ -276,3 +292,21 @@ export const usePaginatedChannels = <
276292
staticChannelsActive,
277293
};
278294
};
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

Comments
 (0)