-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathCallWithChatComposite.tsx
More file actions
517 lines (482 loc) · 18.8 KB
/
CallWithChatComposite.tsx
File metadata and controls
517 lines (482 loc) · 18.8 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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import React, { useCallback, useState, useMemo, useEffect, useRef } from 'react';
import { mergeStyles, PartialTheme, Stack, Theme } from '@fluentui/react';
import { CallCompositePage } from '../CallComposite';
import { CallState } from '@azure/communication-calling';
import { callCompositeContainerStyles, compositeOuterContainerStyles } from './styles/CallWithChatCompositeStyles';
import { CallWithChatAdapter } from './adapter/CallWithChatAdapter';
import { CallWithChatBackedCallAdapter } from './adapter/CallWithChatBackedCallAdapter';
import { CallWithChatBackedChatAdapter } from './adapter/CallWithChatBackedChatAdapter';
import { CallAdapter } from '../CallComposite';
import { ChatComposite, ChatCompositeProps } from '../ChatComposite';
import { BaseProvider, BaseCompositeProps } from '../common/BaseComposite';
import { CallWithChatCompositeIcons } from '../common/icons';
import { AvatarPersonaDataCallback } from '../common/AvatarPersona';
import { CallWithChatAdapterState } from './state/CallWithChatAdapterState';
import {
ParticipantMenuItemsCallback,
_useContainerHeight,
_useContainerWidth,
useTheme
} from '@internal/react-components';
import { useId } from '@fluentui/react-hooks';
/* @conditional-compile-remove(file-sharing) */
import { FileSharingOptions } from '../ChatComposite';
import { containerDivStyles } from '../common/ContainerRectProps';
import { useCallWithChatCompositeStrings } from './hooks/useCallWithChatCompositeStrings';
import { CallCompositeInner, CallCompositeOptions } from '../CallComposite/CallComposite';
/* @conditional-compile-remove(call-readiness) */
import { DeviceCheckOptions } from '../CallComposite/CallComposite';
import {
CommonCallControlOptions,
CustomCallControlButtonCallbackArgs,
_CommonCallControlOptions
} from '../common/types/CommonCallControlOptions';
import { ChatButtonWithUnreadMessagesBadge } from './ChatButton/ChatButtonWithUnreadMessagesBadge';
import { getDesktopCommonButtonStyles } from '../common/ControlBar/CommonCallControlBar';
import { InjectedSidePaneProps } from '../CallComposite/components/SidePane/SidePaneProvider';
import { isDisabled } from '../CallComposite/utils';
import { CustomCallControlButtonCallback } from '../common/ControlBar/CustomButton';
import { SidePaneHeader } from '../common/SidePaneHeader';
import { _CallControlOptions } from '../CallComposite/types/CallControlOptions';
import { useUnreadMessagesTracker } from './ChatButton/useUnreadMessagesTracker';
/**
* Props required for the {@link CallWithChatComposite}
*
* @public
*/
export interface CallWithChatCompositeProps extends BaseCompositeProps<CallWithChatCompositeIcons> {
adapter: CallWithChatAdapter;
/**
* Fluent theme for the composite.
*
* Defaults to a light theme if undefined.
*/
fluentTheme?: PartialTheme | Theme;
/**
* Optimizes the composite form factor for either desktop or mobile.
* @remarks `mobile` is currently only optimized for Portrait mode on mobile devices and does not support landscape.
* @defaultValue 'desktop'
*/
formFactor?: 'desktop' | 'mobile';
/**
* URL that can be used to copy a call-with-chat invite to the Users clipboard.
*/
joinInvitationURL?: string;
/**
* Flags to enable/disable or customize UI elements of the {@link CallWithChatComposite}
*/
options?: CallWithChatCompositeOptions;
}
/**
* Customization options for the control bar in calling with chat experience.
*
* @public
*/
export interface CallWithChatControlOptions extends CommonCallControlOptions {
/**
* Show or hide the chat button in the call-with-chat composite control bar.
* @defaultValue true
*/
chatButton?: boolean | /* @conditional-compile-remove(PSTN-calls) */ { disabled: boolean };
}
/**
* Optional features of the {@link CallWithChatComposite}.
*
* @public
*/
export type CallWithChatCompositeOptions = {
/**
* Call control options to change what buttons show on the call-with-chat composite control bar.
* If using the boolean values, true will cause default behavior across the whole control bar. False hides the whole control bar.
*/
callControls?: boolean | CallWithChatControlOptions;
/* @conditional-compile-remove(file-sharing) */
/**
* Properties for configuring the File Sharing feature.
* If undefined, file sharing feature will be disabled.
* @beta
*/
fileSharing?: FileSharingOptions;
/* @conditional-compile-remove(call-readiness) */
/**
* Device permissions check options for your call.
* Here you can choose what device permissions you prompt the user for,
* as well as what device permissions must be accepted before starting a call.
*/
deviceChecks?: DeviceCheckOptions;
/* @conditional-compile-remove(call-readiness) */
/**
* Callback you may provide to supply users with further steps to troubleshoot why they have been
* unable to grant your site the required permissions for the call.
*
* @example
* ```ts
* onPermissionsTroubleshootingClick: () =>
* window.open('https://contoso.com/permissions-troubleshooting', '_blank');
* ```
*
* @remarks
* if this is not supplied, the composite will not show a 'further troubleshooting' link.
*/
onPermissionsTroubleshootingClick?: (permissionsState: {
camera: PermissionState;
microphone: PermissionState;
}) => void;
/* @conditional-compile-remove(call-readiness) */
/**
* Optional callback to supply users with further troubleshooting steps for network issues
* experienced when connecting to a call.
*
* @example
* ```ts
* onNetworkingTroubleShootingClick?: () =>
* window.open('https://contoso.com/network-troubleshooting', '_blank');
* ```
*
* @remarks
* if this is not supplied, the composite will not show a 'network troubleshooting' link.
*/
onNetworkingTroubleShootingClick?: () => void;
/* @conditional-compile-remove(unsupported-browser) */
/**
* Callback you may provide to supply users with a provided page to showcase supported browsers by ACS.
*
* @example
* ```ts
* onBrowserTroubleShootingClick?: () =>
* window.open('https://contoso.com/browser-troubleshooting', '_blank');
* ```
*
* @remarks
* if this is not supplied, the composite will not show a unsupported browser page.
*/
onEnvironmentInfoTroubleshootingClick?: () => void;
};
type CallWithChatScreenProps = {
callWithChatAdapter: CallWithChatAdapter;
fluentTheme?: PartialTheme | Theme;
formFactor?: 'desktop' | 'mobile';
joinInvitationURL?: string;
callControls?: boolean | CallWithChatControlOptions;
onFetchAvatarPersonaData?: AvatarPersonaDataCallback;
onFetchParticipantMenuItems?: ParticipantMenuItemsCallback;
/* @conditional-compile-remove(file-sharing) */
fileSharing?: FileSharingOptions;
rtl?: boolean;
/* @conditional-compile-remove(call-readiness) */
deviceChecks?: DeviceCheckOptions;
/* @conditional-compile-remove(call-readiness) */
onPermissionsTroubleshootingClick?: (permissionsState: {
camera: PermissionState;
microphone: PermissionState;
}) => void;
/* @conditional-compile-remove(call-readiness) */
onNetworkingTroubleShootingClick?: () => void;
/* @conditional-compile-remove(unsupported-browser) */
onEnvironmentInfoTroubleshootingClick?: () => void;
};
const CallWithChatScreen = (props: CallWithChatScreenProps): JSX.Element => {
const { callWithChatAdapter, fluentTheme, formFactor = 'desktop' } = props;
const mobileView = formFactor === 'mobile';
if (!callWithChatAdapter) {
throw new Error('CallWithChatAdapter is undefined');
}
const callAdapter: CallAdapter = useMemo(
() => new CallWithChatBackedCallAdapter(callWithChatAdapter),
[callWithChatAdapter]
);
const [currentCallState, setCurrentCallState] = useState<CallState>();
const [currentPage, setCurrentPage] = useState<CallCompositePage>();
const [isChatOpen, setIsChatOpen] = useState(false);
const containerRef = useRef<HTMLDivElement>(null);
useEffect(() => {
const updateCallWithChatPage = (newState: CallWithChatAdapterState): void => {
setCurrentPage(newState.page);
setCurrentCallState(newState.call?.state);
};
updateCallWithChatPage(callWithChatAdapter.getState());
callWithChatAdapter.onStateChange(updateCallWithChatPage);
return () => {
callWithChatAdapter.offStateChange(updateCallWithChatPage);
};
}, [callWithChatAdapter]);
const chatProps: ChatCompositeProps = useMemo(() => {
return {
adapter: new CallWithChatBackedChatAdapter(callWithChatAdapter)
};
}, [callWithChatAdapter]);
/** Constant setting of id for the parent stack of the composite */
const compositeParentDivId = useId('callWithChatCompositeParentDiv-internal');
const closeChat = useCallback(() => {
setIsChatOpen(false);
}, []);
const openChat = useCallback(() => {
setIsChatOpen(true);
// timeout is required to give the window time to render the sendbox so we have something to send focus to.
// TODO: Selecting elements in the DOM via attributes is not stable. We should expose an API from ChatComposite to be able to focus on the sendbox.
const chatFocusTimeout = setInterval(() => {
const callWithChatCompositeRootDiv = document.querySelector(`[id="${compositeParentDivId}"]`);
const sendbox = callWithChatCompositeRootDiv?.querySelector(`[id="sendbox"]`) as HTMLTextAreaElement;
if (sendbox !== null) {
sendbox.focus();
clearInterval(chatFocusTimeout);
}
}, 3);
setTimeout(() => {
clearInterval(chatFocusTimeout);
}, 300);
}, [compositeParentDivId]);
const isOnHold = isOnHoldTrampoline(currentPage);
useEffect(() => {
if (isOnHold) {
closeChat();
}
}, [closeChat, isOnHold]);
const hasJoinedCall = !!(currentPage && hasJoinedCallFn(currentPage, currentCallState ?? 'None'));
const toggleChat = useCallback(() => {
isChatOpen || !hasJoinedCall ? closeChat() : openChat();
}, [closeChat, hasJoinedCall, isChatOpen, openChat]);
const callWithChatStrings = useCallWithChatCompositeStrings();
const chatButtonStrings = useMemo(
() => ({
label: callWithChatStrings.chatButtonLabel,
tooltipOffContent: callWithChatStrings.chatButtonTooltipOpen,
tooltipOnContent: callWithChatStrings.chatButtonTooltipClose
}),
[callWithChatStrings]
);
const theme = useTheme();
const commonButtonStyles = useMemo(
() => (!mobileView ? getDesktopCommonButtonStyles(theme) : undefined),
[mobileView, theme]
);
const showChatButton = checkShowChatButton(props.callControls);
const chatButtonDisabled =
showChatButton && (checkChatButtonIsDisabled(props.callControls) || !hasJoinedCall || isOnHold);
const chatTabHeaderProps = useMemo(
() =>
mobileView && showChatButton
? {
onClick: toggleChat,
disabled: chatButtonDisabled
}
: undefined,
[chatButtonDisabled, mobileView, toggleChat, showChatButton]
);
const unreadChatMessagesCount = useUnreadMessagesTracker(chatProps.adapter, isChatOpen);
const customChatButton: CustomCallControlButtonCallback = useCallback(
(args: CustomCallControlButtonCallbackArgs) => ({
placement: mobileView ? 'primary' : 'secondary',
onRenderButton: () => (
<ChatButtonWithUnreadMessagesBadge
checked={isChatOpen}
showLabel={args.displayType !== 'compact'}
onClick={toggleChat}
disabled={chatButtonDisabled}
strings={chatButtonStrings}
styles={commonButtonStyles}
newMessageLabel={callWithChatStrings.chatButtonNewMessageNotificationLabel}
unreadChatMessagesCount={unreadChatMessagesCount}
// As chat is disabled when on hold, we don't want to show the unread badge when on hold
hideUnreadChatMessagesBadge={isOnHold}
/>
)
}),
[
callWithChatStrings.chatButtonNewMessageNotificationLabel,
chatButtonStrings,
commonButtonStyles,
isChatOpen,
chatButtonDisabled,
mobileView,
toggleChat,
unreadChatMessagesCount,
isOnHold
]
);
const callControlOptionsFromProps = useMemo(
() => ({
...(typeof props.callControls === 'object' ? props.callControls : {})
}),
[props.callControls]
);
const injectedCustomButtonsFromProps = useMemo(() => {
/* @conditional-compile-remove(control-bar-button-injection) */
return [...(callControlOptionsFromProps.onFetchCustomButtonProps ?? [])];
return [];
}, [callControlOptionsFromProps]);
const callCompositeOptions: CallCompositeOptions = useMemo(
() => ({
callControls:
props.callControls === false
? false
: ({
...callControlOptionsFromProps,
onFetchCustomButtonProps: [
...(showChatButton ? [customChatButton] : []),
/* @conditional-compile-remove(control-bar-button-injection) */
...injectedCustomButtonsFromProps
],
legacyControlBarExperience: false
} as _CallControlOptions),
/* @conditional-compile-remove(call-readiness) */
deviceChecks: props.deviceChecks,
/* @conditional-compile-remove(call-readiness) */
onNetworkingTroubleShootingClick: props.onNetworkingTroubleShootingClick,
/* @conditional-compile-remove(call-readiness) */
onPermissionsTroubleshootingClick: props.onPermissionsTroubleshootingClick,
/* @conditional-compile-remove(unsupported-browser) */
onEnvironmentInfoTroubleshootingClick: props.onEnvironmentInfoTroubleshootingClick
}),
[
props.callControls,
callControlOptionsFromProps,
showChatButton,
customChatButton,
injectedCustomButtonsFromProps,
/* @conditional-compile-remove(call-readiness) */
props.deviceChecks,
/* @conditional-compile-remove(unsupported-browser) */
props.onEnvironmentInfoTroubleshootingClick,
/* @conditional-compile-remove(call-readiness) */
props.onNetworkingTroubleShootingClick,
/* @conditional-compile-remove(call-readiness) */
props.onPermissionsTroubleshootingClick
]
);
const onRenderChatContent = useCallback(
(): JSX.Element => (
<ChatComposite
{...chatProps}
fluentTheme={theme}
options={{
topic: false,
/* @conditional-compile-remove(chat-composite-participant-pane) */
participantPane: false,
/* @conditional-compile-remove(file-sharing) */
fileSharing: props.fileSharing
}}
onFetchAvatarPersonaData={props.onFetchAvatarPersonaData}
/>
),
[
chatProps,
/* @conditional-compile-remove(file-sharing) */ props.fileSharing,
props.onFetchAvatarPersonaData,
theme
]
);
const sidePaneHeaderRenderer = useCallback(
() => (
<SidePaneHeader
headingText={callWithChatStrings.chatPaneTitle}
onClose={closeChat}
dismissSidePaneButtonAriaLabel={callWithChatStrings.dismissSidePaneButtonLabel ?? ''}
mobileView={mobileView}
/>
),
[callWithChatStrings.chatPaneTitle, callWithChatStrings.dismissSidePaneButtonLabel, closeChat, mobileView]
);
const sidePaneContentRenderer = useMemo(
() => (hasJoinedCall ? onRenderChatContent : undefined),
[hasJoinedCall, onRenderChatContent]
);
const sidePaneRenderer = useMemo(
() => ({
contentRenderer: sidePaneContentRenderer,
headerRenderer: sidePaneHeaderRenderer,
id: 'chat'
}),
[sidePaneContentRenderer, sidePaneHeaderRenderer]
);
const overrideSidePaneProps: InjectedSidePaneProps = useMemo(
() => ({
renderer: sidePaneRenderer,
isActive: isChatOpen,
persistRenderingWhenClosed: true
}),
[isChatOpen, sidePaneRenderer]
);
const onSidePaneIdChange = useCallback(
(sidePaneId) => {
// If the pane is switched to something other than chat, removing rendering chat.
if (sidePaneId && sidePaneId !== 'chat') {
closeChat();
}
},
[closeChat]
);
return (
<div ref={containerRef} className={mergeStyles(containerDivStyles)}>
<Stack verticalFill grow styles={compositeOuterContainerStyles} id={compositeParentDivId}>
<Stack horizontal grow>
<Stack.Item grow styles={callCompositeContainerStyles(mobileView)}>
<CallCompositeInner
{...props}
formFactor={formFactor}
options={callCompositeOptions}
adapter={callAdapter}
fluentTheme={fluentTheme}
callInvitationUrl={props.joinInvitationURL}
overrideSidePane={overrideSidePaneProps}
onSidePaneIdChange={onSidePaneIdChange}
mobileChatTabHeader={chatTabHeaderProps}
/>
</Stack.Item>
</Stack>
</Stack>
</div>
);
};
/**
* CallWithChatComposite brings together key components to provide a full call with chat experience out of the box.
*
* @public
*/
export const CallWithChatComposite = (props: CallWithChatCompositeProps): JSX.Element => {
const { adapter, fluentTheme, rtl, formFactor, joinInvitationURL, options } = props;
return (
<BaseProvider fluentTheme={fluentTheme} rtl={rtl} locale={props.locale} icons={props.icons}>
<CallWithChatScreen
{...props}
/* @conditional-compile-remove(call-readiness) */
deviceChecks={options?.deviceChecks}
callWithChatAdapter={adapter}
formFactor={formFactor}
callControls={options?.callControls}
joinInvitationURL={joinInvitationURL}
fluentTheme={fluentTheme}
/* @conditional-compile-remove(file-sharing) */
fileSharing={options?.fileSharing}
/>
</BaseProvider>
);
};
const hasJoinedCallFn = (page: CallCompositePage, callStatus: CallState): boolean => {
/* @conditional-compile-remove(one-to-n-calling) */ /* @conditional-compile-remove(one-to-n-calling) */
return (
(page === 'call' &&
(callStatus === 'Connected' || callStatus === 'RemoteHold' || callStatus === 'Disconnecting')) ||
(page === 'hold' && (callStatus === 'LocalHold' || callStatus === 'Disconnecting'))
);
return page === 'call' && (callStatus === 'Connected' || callStatus === 'Disconnecting');
};
const checkShowChatButton = (callControls?: boolean | CallWithChatControlOptions): boolean => {
if (callControls === undefined || callControls === true) {
return true;
}
if (callControls === false) {
return false;
}
return callControls.chatButton !== false;
};
const checkChatButtonIsDisabled = (callControls?: boolean | CallWithChatControlOptions): boolean => {
return typeof callControls === 'object' && isDisabled(callControls?.chatButton);
};
const isOnHoldTrampoline = (page: CallCompositePage | undefined): boolean => {
/* @conditional-compile-remove(PSTN-calls) */ /* @conditional-compile-remove(one-to-n-calling) */
return page === 'hold';
return false;
};