Skip to content

Commit 1eceae9

Browse files
authored
Merge pull request #800 from GetStream/mentions-optimistic-UI-fix
Mentions optimistic UI fix
2 parents d769a9c + 97ed80e commit 1eceae9

File tree

5 files changed

+47
-15
lines changed

5 files changed

+47
-15
lines changed

src/components/Channel/Channel.tsx

Lines changed: 32 additions & 5 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,36 @@ 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+
// channel.sendMessage expects an array of user id strings
499+
const mentions = isUserResponseArray(mentioned_users)
500+
? mentioned_users.map(({ id }) => id)
501+
: mentioned_users;
476502

477503
const messageData = {
478504
attachments,
479505
id,
480-
mentioned_users,
506+
mentioned_users: mentions,
481507
parent_id,
482508
text,
483509
} as Message<At, Me, Us>;
@@ -524,10 +550,11 @@ const ChannelInner = <
524550
text: string,
525551
attachments: MessageAttachments<At>,
526552
parent: MessageResponse<At, Ch, Co, Me, Re, Us> | undefined,
527-
mentioned_users: string[],
553+
mentioned_users: UserResponse<Us>[],
528554
) => {
529555
// create a preview of the message
530556
const clientSideID = `${client.userID}-${uuidv4()}`;
557+
531558
return ({
532559
__html: text,
533560
attachments,

src/components/MessageInput/MessageInput.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import React from 'react';
22

33
import { MessageInputLarge } from './MessageInputLarge';
44

5-
import type { Attachment, Channel, SendFileAPIResponse } from 'stream-chat';
5+
import type {
6+
Attachment,
7+
Channel,
8+
SendFileAPIResponse,
9+
UserResponse,
10+
} from 'stream-chat';
611

712
import type { FileUpload, ImageUpload } from './hooks/messageInput';
813
import type { SendButtonProps } from './icons';
@@ -101,7 +106,7 @@ export type MessageInputProps<
101106
overrideSubmitHandler?: (
102107
message: {
103108
attachments: Attachment<At>[];
104-
mentioned_users: string[];
109+
mentioned_users: UserResponse<Us>[];
105110
text: string;
106111
parent?: StreamMessage<At, Ch, Co, Ev, Me, Re, Us>;
107112
},

src/components/MessageInput/__tests__/MessageInput.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ const ActiveChannelSetter = ({ activeChannel }) => {
700700
expect.objectContaining(image),
701701
expect.objectContaining(file),
702702
]),
703-
mentioned_users: [userId],
703+
mentioned_users: [{ id: userId, name: username }],
704704
text: message.text,
705705
}),
706706
);

src/components/MessageInput/hooks/messageInput.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -679,12 +679,10 @@ export const useMessageInput = <
679679
// and allows users to easily undo any accidental deletion
680680
const actualMentionedUsers = Array.from(
681681
new Set(
682-
mentioned_users
683-
.filter(
684-
({ id, name }) =>
685-
text.includes(`@${id}`) || text.includes(`@${name}`),
686-
)
687-
.map(({ id }) => id),
682+
mentioned_users.filter(
683+
({ id, name }) =>
684+
text.includes(`@${id}`) || text.includes(`@${name}`),
685+
),
688686
),
689687
);
690688

src/context/ChannelContext.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,10 @@ export type MessageToSend<
8080
> = {
8181
attachments?: MessageAttachments<At>;
8282
id?: string;
83-
mentioned_users?: string[];
83+
mentioned_users?: UserResponse<Us>[];
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)