Skip to content

Commit 3ae3858

Browse files
committed
fix: properly handle failed messages in the offline case
1 parent a223bd6 commit 3ae3858

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

package/src/__tests__/offline-support/optimistic-update.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ export const OptimisticUpdates = () => {
113113
channels: [channelResponse],
114114
isLatestMessagesSet: true,
115115
});
116+
chatClient.wsConnection = { isHealthy: true, onlineStatusChanged: jest.fn() };
116117
});
117118

118119
afterEach(() => {

package/src/components/Channel/Channel.tsx

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ export type ChannelPropsWithContext = Pick<ChannelContextValue, 'channel'> &
258258
| 'StickyHeader'
259259
>
260260
> &
261-
Pick<ChatContextValue, 'client' | 'enableOfflineSupport'> &
261+
Pick<ChatContextValue, 'client' | 'enableOfflineSupport' | 'isOnline'> &
262262
Partial<
263263
Omit<
264264
InputMessageInputContextValue,
@@ -670,6 +670,7 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
670670
UploadProgressIndicator = UploadProgressIndicatorDefault,
671671
UrlPreview = CardDefault,
672672
VideoThumbnail = VideoThumbnailDefault,
673+
isOnline,
673674
} = props;
674675

675676
const { thread: threadProps, threadInstance } = threadFromProps;
@@ -1345,7 +1346,33 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
13451346

13461347
const sendMessageRequest = useStableCallback(
13471348
async (message: MessageResponse, retrying?: boolean) => {
1349+
let failedMessageUpdated = false;
1350+
const handleFailedMessage = async () => {
1351+
if (!failedMessageUpdated) {
1352+
const updatedMessage = {
1353+
...message,
1354+
cid: channel.cid,
1355+
status: MessageStatusTypes.FAILED,
1356+
};
1357+
updateMessage(updatedMessage);
1358+
threadInstance?.upsertReplyLocally?.({ message: updatedMessage });
1359+
optimisticallyUpdatedNewMessages.delete(message.id);
1360+
1361+
if (enableOfflineSupport) {
1362+
await dbApi.updateMessage({
1363+
message: updatedMessage,
1364+
});
1365+
}
1366+
1367+
failedMessageUpdated = true;
1368+
}
1369+
};
1370+
13481371
try {
1372+
if (!isOnline) {
1373+
await handleFailedMessage();
1374+
}
1375+
13491376
const updatedMessage = await uploadPendingAttachments(message);
13501377
const extraFields = omit(updatedMessage, [
13511378
'__html',
@@ -1385,6 +1412,7 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
13851412
} as StreamMessage;
13861413

13871414
let messageResponse = {} as SendMessageAPIResponse;
1415+
13881416
if (doSendMessageRequest) {
13891417
messageResponse = await doSendMessageRequest(channel?.cid || '', messageData);
13901418
} else if (channel) {
@@ -1410,16 +1438,7 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
14101438
}
14111439
} catch (err) {
14121440
console.log(err);
1413-
const updatedMessage = { ...message, cid: channel.cid, status: MessageStatusTypes.FAILED };
1414-
updateMessage(updatedMessage);
1415-
threadInstance?.upsertReplyLocally?.({ message: updatedMessage });
1416-
optimisticallyUpdatedNewMessages.delete(message.id);
1417-
1418-
if (enableOfflineSupport) {
1419-
await dbApi.updateMessage({
1420-
message: updatedMessage,
1421-
});
1422-
}
1441+
await handleFailedMessage();
14231442
}
14241443
},
14251444
);
@@ -2007,7 +2026,7 @@ export type ChannelProps = Partial<Omit<ChannelPropsWithContext, 'channel' | 'th
20072026
* @example ./Channel.md
20082027
*/
20092028
export const Channel = (props: PropsWithChildren<ChannelProps>) => {
2010-
const { client, enableOfflineSupport, isMessageAIGenerated } = useChatContext();
2029+
const { client, enableOfflineSupport, isOnline, isMessageAIGenerated } = useChatContext();
20112030
const { t } = useTranslationContext();
20122031

20132032
const threadFromProps = props?.thread;
@@ -2039,6 +2058,7 @@ export const Channel = (props: PropsWithChildren<ChannelProps>) => {
20392058
shouldSyncChannel={shouldSyncChannel}
20402059
{...{
20412060
isMessageAIGenerated,
2061+
isOnline,
20422062
setThreadMessages,
20432063
thread,
20442064
threadMessages,

0 commit comments

Comments
 (0)