-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathChatMessageComponent.tsx
More file actions
158 lines (152 loc) · 5.77 KB
/
ChatMessageComponent.tsx
File metadata and controls
158 lines (152 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { _formatString } from '@internal/acs-ui-common';
import React, { useCallback, useState } from 'react';
import { ChatMessageComponentAsEditBox } from './ChatMessageComponentAsEditBox';
import { MessageThreadStrings } from '../MessageThread';
import { ChatMessage, ComponentSlotStyle, OnRenderAvatarCallback } from '../../types';
/* @conditional-compile-remove(data-loss-prevention) */
import { BlockedMessage } from '../../types';
import { ChatMessageComponentAsMessageBubble } from './ChatMessageComponentAsMessageBubble';
import { FileDownloadHandler, AttachmentMetadata } from '../FileDownloadCards';
/* @conditional-compile-remove(mention) */
import { MentionOptions } from '../MentionPopover';
/* @conditional-compile-remove(image-overlay) */
import { InlineImageOptions } from './ChatMessageContent';
type ChatMessageComponentProps = {
message: ChatMessage | /* @conditional-compile-remove(data-loss-prevention) */ BlockedMessage;
userId: string;
messageContainerStyle?: ComponentSlotStyle;
showDate?: boolean;
disableEditing?: boolean;
onUpdateMessage?: (
messageId: string,
content: string,
metadata?: Record<string, string>,
options?: {
attachmentMetadata?: AttachmentMetadata[];
}
) => Promise<void>;
onCancelEditMessage?: (messageId: string) => void;
/**
* Callback to delete a message. Also called before resending a message that failed to send.
* @param messageId ID of the message to delete
*/
onDeleteMessage?: (messageId: string) => Promise<void>;
/**
* Callback to send a message
* @param content The message content to send
*/
onSendMessage?: (content: string) => Promise<void>;
strings: MessageThreadStrings;
messageStatus?: string;
/**
* Optional text to display when the message status is 'failed'.
*/
failureReason?: string;
/**
* Whether the status indicator for each message is displayed or not.
*/
showMessageStatus?: boolean;
/**
* Whether to overlap avatar and message when the view is width constrained.
*/
shouldOverlapAvatarAndMessage: boolean;
/**
* Optional callback to render uploaded files in the message component.
*/
onRenderFileDownloads?: (userId: string, message: ChatMessage) => JSX.Element;
/**
* Optional function called when someone clicks on the file download icon.
*/
fileDownloadHandler?: FileDownloadHandler;
remoteParticipantsCount?: number;
onActionButtonClick: (
message: ChatMessage,
setMessageReadBy: (readBy: { id: string; displayName: string }[]) => void
) => void;
/**
* Optional callback to override render of the avatar.
*
* @param userId - user Id
*/
onRenderAvatar?: OnRenderAvatarCallback;
/* @conditional-compile-remove(date-time-customization) */
/**
* Optional function to provide customized date format.
* @beta
*/
onDisplayDateTimeString?: (messageDate: Date) => string;
/* @conditional-compile-remove(mention) */
/**
* Optional props needed to lookup suggestions and display mentions in the mention scenario.
* @beta
*/
mentionOptions?: MentionOptions;
/* @conditional-compile-remove(image-overlay) */
/**
* Optional callback called when an inline image is clicked.
* @beta
*/
inlineImageOptions?: InlineImageOptions;
};
/**
* @private
*/
export const ChatMessageComponent = (props: ChatMessageComponentProps): JSX.Element => {
const { onDeleteMessage, onSendMessage, message } = props;
const [isEditing, setIsEditing] = useState(false);
const onEditClick = useCallback(() => setIsEditing(true), [setIsEditing]);
const clientMessageId = 'clientMessageId' in message ? message.clientMessageId : undefined;
const content = 'content' in message ? message.content : undefined;
const onRemoveClick = useCallback(() => {
if (onDeleteMessage && message.messageId) {
onDeleteMessage(message.messageId);
}
// when fail to send, message does not have message id, delete message using clientMessageId
else if (onDeleteMessage && message.messageType === 'chat' && clientMessageId) {
onDeleteMessage(clientMessageId);
}
}, [onDeleteMessage, message.messageId, message.messageType, clientMessageId]);
const onResendClick = useCallback(() => {
onDeleteMessage && clientMessageId && onDeleteMessage(clientMessageId);
onSendMessage && onSendMessage(content !== undefined ? content : '');
}, [clientMessageId, content, onSendMessage, onDeleteMessage]);
if (isEditing && message.messageType === 'chat') {
return (
<ChatMessageComponentAsEditBox
message={message}
strings={props.strings}
onSubmit={async (text, metadata, options) => {
props.onUpdateMessage &&
message.messageId &&
(await props.onUpdateMessage(message.messageId, text, metadata, options));
setIsEditing(false);
}}
onCancel={(messageId) => {
props.onCancelEditMessage && props.onCancelEditMessage(messageId);
setIsEditing(false);
}}
/* @conditional-compile-remove(mention) */
mentionLookupOptions={props.mentionOptions?.lookupOptions}
/>
);
} else {
return (
<ChatMessageComponentAsMessageBubble
{...props}
onRemoveClick={onRemoveClick}
onEditClick={onEditClick}
onResendClick={onResendClick}
onRenderAvatar={props.onRenderAvatar}
/* @conditional-compile-remove(date-time-customization) */
onDisplayDateTimeString={props.onDisplayDateTimeString}
strings={props.strings}
/* @conditional-compile-remove(image-overlay) */
inlineImageOptions={props.inlineImageOptions}
/* @conditional-compile-remove(mention) */
mentionDisplayOptions={props.mentionOptions?.displayOptions}
/>
);
}
};