Skip to content

Commit 9b0d121

Browse files
committed
mentions optimistic UI
1 parent 22d042a commit 9b0d121

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

src/components/Channel/Channel.tsx

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
Channel as StreamChannel,
2626
StreamChat,
2727
UpdatedMessage,
28+
UserResponse,
2829
} from 'stream-chat';
2930
import { v4 as uuidv4 } from 'uuid';
3031

@@ -451,7 +452,11 @@ const ChannelInner = <
451452
);
452453

453454
const updateMessage = useCallback(
454-
(updatedMessage: StreamMessage<At, Ch, Co, Ev, Me, Re, Us>) => {
455+
(
456+
updatedMessage:
457+
| MessageToSend<At, Ch, Co, Me, Re, Us>
458+
| StreamMessage<At, Ch, Co, Ev, Me, Re, Us>,
459+
) => {
455460
if (!channel) return;
456461
// adds the message to the local channel state..
457462
// this adds to both the main channel state as well as any reply threads
@@ -469,15 +474,35 @@ const ChannelInner = <
469474
[channel, state.thread],
470475
);
471476

477+
const isUserResponseArray = (
478+
output: string[] | UserResponse<Us>[],
479+
): output is UserResponse<Us>[] =>
480+
(output as UserResponse<Us>[])[0]?.id != null;
481+
472482
const doSendMessage = useCallback(
473-
async (message: StreamMessage<At, Ch, Co, Ev, Me, Re, Us>) => {
483+
async (
484+
message:
485+
| MessageToSend<At, Ch, Co, Me, Re, Us>
486+
| StreamMessage<At, Ch, Co, Ev, Me, Re, Us>,
487+
) => {
474488
if (!channel) return;
475-
const { attachments, id, mentioned_users, parent_id, text } = message;
489+
490+
const {
491+
attachments,
492+
id,
493+
mentioned_users = [],
494+
parent_id,
495+
text,
496+
} = message;
497+
498+
const mentions = isUserResponseArray(mentioned_users)
499+
? mentioned_users.map(({ id }) => id)
500+
: mentioned_users;
476501

477502
const messageData = {
478503
attachments,
479504
id,
480-
mentioned_users,
505+
mentioned_users: mentions,
481506
parent_id,
482507
text,
483508
} as Message<At, Me, Us>;
@@ -528,13 +553,19 @@ const ChannelInner = <
528553
) => {
529554
// create a preview of the message
530555
const clientSideID = `${client.userID}-${uuidv4()}`;
556+
557+
// channel.state.addMessageSorted expects an array of user responses
558+
const mappedMentions = mentioned_users.map((mention) => ({
559+
id: mention,
560+
}));
561+
531562
return ({
532563
__html: text,
533564
attachments,
534565
created_at: new Date(),
535566
html: text,
536567
id: clientSideID,
537-
mentioned_users,
568+
mentioned_users: mappedMentions,
538569
reactions: [],
539570
status: 'sending',
540571
text,
@@ -569,7 +600,12 @@ const ChannelInner = <
569600
// first we add the message to the UI
570601
updateMessage(messagePreview);
571602

572-
await doSendMessage(messagePreview);
603+
const messageToSend = {
604+
...messagePreview,
605+
mentioned_users,
606+
};
607+
608+
await doSendMessage(messageToSend);
573609
},
574610
[channel?.state, createMessagePreview, doSendMessage, updateMessage],
575611
);

src/context/ChannelContext.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ export type MessageToSend<
8282
id?: string;
8383
mentioned_users?: string[];
8484
parent?: MessageResponse<At, Ch, Co, Me, Re, Us>;
85+
parent_id?: string;
86+
status?: string;
8587
text?: string;
8688
};
8789

0 commit comments

Comments
 (0)