-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathChatScreen.tsx
More file actions
465 lines (433 loc) · 16 KB
/
ChatScreen.tsx
File metadata and controls
465 lines (433 loc) · 16 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/* @conditional-compile-remove(image-overlay) */
import { isIOS } from '@fluentui/react';
import { mergeStyles, Stack } from '@fluentui/react';
/* @conditional-compile-remove(image-overlay) */
import { PersonaSize } from '@fluentui/react';
import {
CommunicationParticipant,
ErrorBar,
MessageProps,
MessageRenderer,
MessageThread,
MessageThreadStyles,
ParticipantMenuItemsCallback,
SendBox,
SendBoxStylesProps,
TypingIndicator,
TypingIndicatorStylesProps,
useTheme
} from '@internal/react-components';
/* @conditional-compile-remove(file-sharing) */
import { ChatMessage } from '@internal/react-components';
import React, { useCallback, useEffect, useMemo } from 'react';
/* @conditional-compile-remove(image-overlay) */
import { useState } from 'react';
import { AvatarPersona, AvatarPersonaDataCallback, AvatarPersonaProps } from '../common/AvatarPersona';
import { useAdapter } from './adapter/ChatAdapterProvider';
import { ChatCompositeOptions } from './ChatComposite';
import { ChatHeader, getHeaderProps } from './ChatHeader';
import { FileDownloadHandler } from '@internal/react-components';
import { FileUploadButtonWrapper as FileUploadButton, FileUploadHandler } from './file-sharing';
import { useAdaptedSelector } from './hooks/useAdaptedSelector';
import { usePropsFor } from './hooks/usePropsFor';
import {
chatArea,
chatContainer,
chatWrapper,
messageThreadChatCompositeStyles,
sendboxContainerStyles,
typingIndicatorContainerStyles
} from './styles/Chat.styles';
import { participantListContainerPadding } from '../common/styles/ParticipantContainer.styles';
/* @conditional-compile-remove(chat-composite-participant-pane) */
import { ChatScreenPeoplePane } from './ChatScreenPeoplePane';
import { toFlatCommunicationIdentifier } from '@internal/acs-ui-common';
/* @conditional-compile-remove(file-sharing) */
import { fileUploadsSelector } from './selectors/fileUploadsSelector';
/* @conditional-compile-remove(file-sharing) */
import { useSelector } from './hooks/useSelector';
/* @conditional-compile-remove(file-sharing) */
import { FileDownloadErrorBar } from './FileDownloadErrorBar';
/* @conditional-compile-remove(file-sharing) */
import { _FileDownloadCards } from '@internal/react-components';
/* @conditional-compile-remove(image-overlay) */
import { ImageOverlay } from '@internal/react-components';
/* @conditional-compile-remove(image-overlay) */
import { InlineImage } from '@internal/react-components';
/**
* @private
*/
export type ChatScreenProps = {
options?: ChatCompositeOptions;
onFetchAvatarPersonaData?: AvatarPersonaDataCallback;
onRenderMessage?: (messageProps: MessageProps, defaultOnRender?: MessageRenderer) => JSX.Element;
onRenderTypingIndicator?: (typingUsers: CommunicationParticipant[]) => JSX.Element;
onFetchParticipantMenuItems?: ParticipantMenuItemsCallback;
styles?: ChatScreenStyles;
hasFocusOnMount?: 'sendBoxTextField';
fileSharing?: FileSharingOptions;
formFactor?: 'desktop' | 'mobile';
};
/**
* @private
*/
export type ChatScreenStyles = {
messageThread?: MessageThreadStyles;
sendBox?: SendBoxStylesProps;
typingIndicator?: TypingIndicatorStylesProps;
};
/**
* Properties for configuring the File Sharing feature.
* @beta
*/
export interface FileSharingOptions {
/**
* A string containing the comma separated list of accepted file types.
* Similar to the `accept` attribute of the `<input type="file" />` element.
* Accepts any type of file if not specified.
* @beta
*/
accept?: string;
/**
* Allows multiple files to be selected if set to `true`.
* Similar to the `multiple` attribute of the `<input type="file" />` element.
* @defaultValue false
* @beta
*/
multiple?: boolean;
/**
* A function of type {@link FileUploadHandler} for handling file uploads.
* @beta
*/
uploadHandler: FileUploadHandler;
/**
* A function of type {@link FileDownloadHandler} for handling file downloads.
* If the function is not specified, the file's `url` will be opened in a new tab to
* initiate the download.
*/
downloadHandler?: FileDownloadHandler;
}
/**
* @private
*/
/* @conditional-compile-remove(image-overlay) */
interface OverlayImageItem {
imageSrc: string;
title: string;
titleIcon: JSX.Element;
attachmentId: string;
messageId: string;
imageUrl: string;
}
/**
* @private
*/
export const ChatScreen = (props: ChatScreenProps): JSX.Element => {
const {
onFetchAvatarPersonaData,
onRenderMessage,
onRenderTypingIndicator,
options,
styles,
fileSharing,
formFactor
} = props;
const defaultNumberOfChatMessagesToReload = 5;
/* @conditional-compile-remove(file-sharing) */
const [downloadErrorMessage, setDownloadErrorMessage] = React.useState('');
/* @conditional-compile-remove(image-overlay) */
const [overlayImageItem, setOverlayImageItem] = useState<OverlayImageItem>();
/* @conditional-compile-remove(image-overlay) */
const [isImageOverlayOpen, setIsImageOverlayOpen] = useState<boolean>(false);
const adapter = useAdapter();
const theme = useTheme();
useEffect(() => {
// Initial data should be always fetched by the composite(or external caller) instead of the adapter
const fetchData: () => Promise<void> = async () => {
// Fetch initial data for adapter
await adapter.fetchInitialData();
// Fetch initial set of messages. Without fetching messages here, if the Composite's adapter is changed the message thread does not load new messages.
await adapter.loadPreviousChatMessages(defaultNumberOfChatMessagesToReload);
};
fetchData();
}, [adapter]);
const messageThreadProps = usePropsFor(MessageThread);
const sendBoxProps = usePropsFor(SendBox);
const typingIndicatorProps = usePropsFor(TypingIndicator);
const headerProps = useAdaptedSelector(getHeaderProps);
const errorBarProps = usePropsFor(ErrorBar);
/* @conditional-compile-remove(image-overlay) */
useEffect(() => {
if (overlayImageItem === undefined) {
return;
}
const message = adapter.getState().thread.chatMessages[overlayImageItem?.messageId];
const resourceCache = message.resourceCache;
if (overlayImageItem.imageSrc === '' && resourceCache) {
const fullSizeImageSrc = resourceCache[overlayImageItem.imageUrl];
if (fullSizeImageSrc === undefined || fullSizeImageSrc === '' || overlayImageItem.imageSrc === fullSizeImageSrc) {
return;
}
setOverlayImageItem({
...overlayImageItem,
imageSrc: fullSizeImageSrc
});
}
// Disable eslint because we are using the overlayImageItem in this effect but don't want to have it as a dependency, as it will cause an infinite loop.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [messageThreadProps.messages]);
const onRenderAvatarCallback = useCallback(
(userId?: string, defaultOptions?: AvatarPersonaProps) => {
return (
<AvatarPersona
userId={userId}
hidePersonaDetails={true}
{...defaultOptions}
dataProvider={onFetchAvatarPersonaData}
/>
);
},
[onFetchAvatarPersonaData]
);
const messageThreadStyles = useMemo(() => {
return Object.assign(
{},
messageThreadChatCompositeStyles(theme.semanticColors.bodyBackground),
styles?.messageThread
);
}, [styles?.messageThread, theme.semanticColors.bodyBackground]);
const typingIndicatorStyles = useMemo(() => {
return Object.assign({}, styles?.typingIndicator);
}, [styles?.typingIndicator]);
const sendBoxStyles = useMemo(() => {
return Object.assign({}, styles?.sendBox);
}, [styles?.sendBox]);
const userId = toFlatCommunicationIdentifier(adapter.getState().userId);
const fileUploadButtonOnChange = useCallback(
(files: FileList | null): void => {
if (!files) {
return;
}
/* @conditional-compile-remove(file-sharing) */
const fileUploads = adapter.registerActiveFileUploads(Array.from(files));
/* @conditional-compile-remove(file-sharing) */
fileSharing?.uploadHandler(userId, fileUploads);
},
[adapter, fileSharing, userId]
);
/* @conditional-compile-remove(file-sharing) */
const onRenderFileDownloads = useCallback(
(userId: string, message: ChatMessage) => (
<_FileDownloadCards
userId={userId}
fileMetadata={message.files || []}
downloadHandler={fileSharing?.downloadHandler}
onDownloadErrorMessage={(errorMessage: string) => {
setDownloadErrorMessage(errorMessage);
}}
/>
),
[fileSharing?.downloadHandler]
);
/* @conditional-compile-remove(image-overlay) */
const onInlineImageClicked = useCallback(
async (attachmentId: string, messageId: string): Promise<void> => {
const message = adapter.getState().thread.chatMessages[messageId];
const inlinedImages = message.content?.attachments?.filter((attachment) => {
return attachment.attachmentType === 'image' && attachment.id === attachmentId;
});
if (!inlinedImages || inlinedImages.length <= 0) {
return;
}
const attachment = inlinedImages[0];
const resourceCache = message.resourceCache;
let imageSrc = '';
if (attachment.url) {
if (resourceCache && resourceCache[attachment.url]) {
imageSrc = resourceCache[attachment.url];
} else {
adapter.downloadResourceToCache({
threadId: adapter.getState().thread.threadId,
messageId: messageId,
resourceUrl: attachment.url
});
}
}
const titleIconRenderOptions = {
text: message.senderDisplayName,
size: PersonaSize.size32,
showOverflowTooltip: false,
imageAlt: message.senderDisplayName
};
const messageSenderId = message.sender !== undefined ? toFlatCommunicationIdentifier(message.sender) : userId;
const titleIcon = onRenderAvatarCallback && onRenderAvatarCallback(messageSenderId, titleIconRenderOptions);
const overlayImage: OverlayImageItem = {
title: message.senderDisplayName || '',
titleIcon: titleIcon,
attachmentId: attachment.id,
imageSrc: imageSrc,
messageId: messageId,
imageUrl: attachment.url || ''
};
setIsImageOverlayOpen(true);
setOverlayImageItem(overlayImage);
},
[adapter, onRenderAvatarCallback, userId]
);
/* @conditional-compile-remove(image-overlay) */
const inlineImageOptions = {
onRenderInlineImage: (
inlineImage: InlineImage,
defaultOnRender: (inlineImage: InlineImage) => JSX.Element
): JSX.Element => {
const message = adapter.getState().thread.chatMessages[inlineImage.messageId];
const attachments = message?.content?.attachments?.find(
(attachment) => attachment.id === inlineImage.imgAttrs.id
);
if (attachments === undefined) {
return defaultOnRender(inlineImage);
}
return (
<span
key={inlineImage.imgAttrs.id}
onClick={() => onInlineImageClicked(inlineImage.imgAttrs.id || '', inlineImage.messageId)}
tabIndex={0}
role="button"
onKeyDown={(e) => {
if (e.key === 'Enter') {
onInlineImageClicked(inlineImage.imgAttrs.id || '', inlineImage.messageId);
}
}}
style={{ cursor: 'pointer' }}
>
{defaultOnRender(inlineImage)}
</span>
);
}
};
/* @conditional-compile-remove(image-overlay) */
const onDownloadButtonClicked = useCallback(
(imageSrc: string): void => {
if (imageSrc === '') {
return;
}
if (isIOS()) {
window.open(imageSrc, '_blank');
} else {
// Create a new anchor element
const a = document.createElement('a');
// Set the href and download attributes for the anchor element
a.href = imageSrc;
a.download = overlayImageItem?.attachmentId || '';
a.rel = 'noopener noreferrer';
a.target = '_blank';
// Programmatically click the anchor element to trigger the download
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
},
[overlayImageItem?.attachmentId]
);
const AttachFileButton = useCallback(() => {
if (!fileSharing?.uploadHandler) {
return null;
}
return (
<FileUploadButton
accept={fileSharing?.accept}
multiple={fileSharing?.multiple}
onChange={fileUploadButtonOnChange}
/>
);
}, [fileSharing?.accept, fileSharing?.multiple, fileSharing?.uploadHandler, fileUploadButtonOnChange]);
return (
<Stack className={chatContainer} grow>
{options?.topic !== false && <ChatHeader {...headerProps} />}
<Stack className={chatArea} tokens={participantListContainerPadding} horizontal grow>
<Stack className={chatWrapper} grow>
{options?.errorBar !== false && <ErrorBar {...errorBarProps} />}
{
/* @conditional-compile-remove(file-sharing) */
<FileDownloadErrorBar
onDismissDownloadErrorMessage={useCallback(() => {
setDownloadErrorMessage('');
}, [])}
fileDownloadErrorMessage={downloadErrorMessage || ''}
/>
}
<MessageThread
{...messageThreadProps}
onRenderAvatar={onRenderAvatarCallback}
onRenderMessage={onRenderMessage}
/* @conditional-compile-remove(file-sharing) */
onRenderFileDownloads={onRenderFileDownloads}
/* @conditional-compile-remove(image-overlay) */
inlineImageOptions={inlineImageOptions}
numberOfChatMessagesToReload={defaultNumberOfChatMessagesToReload}
styles={messageThreadStyles}
/>
<Stack className={mergeStyles(sendboxContainerStyles)}>
<div className={mergeStyles(typingIndicatorContainerStyles)}>
{onRenderTypingIndicator ? (
onRenderTypingIndicator(typingIndicatorProps.typingUsers)
) : (
<TypingIndicator {...typingIndicatorProps} styles={typingIndicatorStyles} />
)}
</div>
<Stack horizontal={formFactor === 'mobile'}>
{formFactor === 'mobile' && (
<Stack verticalAlign="center">
<AttachFileButton />
</Stack>
)}
<Stack grow>
<SendBox
{...sendBoxProps}
autoFocus={options?.autoFocus}
styles={sendBoxStyles}
/* @conditional-compile-remove(file-sharing) */
activeFileUploads={useSelector(fileUploadsSelector).files}
/* @conditional-compile-remove(file-sharing) */
onCancelFileUpload={adapter.cancelFileUpload}
/>
</Stack>
{formFactor !== 'mobile' && <AttachFileButton />}
</Stack>
</Stack>
</Stack>
{
/* @conditional-compile-remove(chat-composite-participant-pane) */
options?.participantPane === true && (
<ChatScreenPeoplePane
onFetchAvatarPersonaData={onFetchAvatarPersonaData}
onFetchParticipantMenuItems={props.onFetchParticipantMenuItems}
isMobile={formFactor === 'mobile'}
/>
)
}
</Stack>
{
/* @conditional-compile-remove(image-overlay) */
overlayImageItem && (
<ImageOverlay
{...overlayImageItem}
isOpen={isImageOverlayOpen}
onDismiss={() => {
setOverlayImageItem(undefined);
setIsImageOverlayOpen(false);
adapter.removeResourceFromCache({
threadId: adapter.getState().thread.threadId,
messageId: overlayImageItem.messageId,
resourceUrl: overlayImageItem.imageUrl
});
}}
onDownloadButtonClicked={onDownloadButtonClicked}
/>
)
}
</Stack>
);
};