-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathCallScreen.tsx
More file actions
212 lines (193 loc) · 6.83 KB
/
CallScreen.tsx
File metadata and controls
212 lines (193 loc) · 6.83 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { TeamsMeetingLinkLocator } from '@azure/communication-calling';
import { CommunicationUserIdentifier } from '@azure/communication-common';
import {
toFlatCommunicationIdentifier,
useAzureCommunicationCallWithChatAdapter,
CallAndChatLocator,
CallWithChatAdapterState,
CallWithChatComposite,
CallWithChatAdapter,
CallWithChatCompositeOptions
} from '@azure/communication-react';
/* @conditional-compile-remove(video-background-effects) */
import { onResolveVideoEffectDependencyLazy, AzureCommunicationCallAdapterOptions } from '@azure/communication-react';
import { Spinner } from '@fluentui/react';
import React, { useCallback, useEffect, useMemo, useRef } from 'react';
import { useSwitchableFluentTheme } from '../theming/SwitchableFluentThemeProvider';
import { createAutoRefreshingCredential } from '../utils/credential';
import { WEB_APP_TITLE } from '../utils/constants';
import { useIsMobile } from '../utils/useIsMobile';
import { isIOS } from '../utils/utils';
export interface CallScreenProps {
token: string;
userId: CommunicationUserIdentifier;
displayName: string;
endpoint: string;
locator: CallAndChatLocator | TeamsMeetingLinkLocator;
/* @conditional-compile-remove(PSTN-calls) */ alternateCallerId?: string;
}
export const CallScreen = (props: CallScreenProps): JSX.Element => {
const {
token,
userId,
displayName,
endpoint,
locator,
/* @conditional-compile-remove(PSTN-calls) */ alternateCallerId
} = props;
/* @conditional-compile-remove(video-background-effects) */
const callAdapterOptions: AzureCommunicationCallAdapterOptions = useMemo(() => {
const videoBackgroundImages = [
{
key: 'ab1',
url: '/assets/backgrounds/contoso.png',
tooltipText: 'Custom Background'
},
{
key: 'ab2',
url: '/assets/backgrounds/abstract2.jpg',
tooltipText: 'Custom Background'
},
{
key: 'ab3',
url: '/assets/backgrounds/abstract3.jpg',
tooltipText: 'Custom Background'
},
{
key: 'ab4',
url: '/assets/backgrounds/room1.jpg',
tooltipText: 'Custom Background'
},
{
key: 'ab5',
url: '/assets/backgrounds/room2.jpg',
tooltipText: 'Custom Background'
},
{
key: 'ab6',
url: '/assets/backgrounds/room3.jpg',
tooltipText: 'Custom Background'
},
{
key: 'ab7',
url: '/assets/backgrounds/room4.jpg',
tooltipText: 'Custom Background'
}
];
return {
videoBackgroundOptions: {
videoBackgroundImages,
/* @conditional-compile-remove(video-background-effects) */
onResolveDependency: onResolveVideoEffectDependencyLazy
}
};
}, []);
// Disables pull down to refresh. Prevents accidental page refresh when scrolling through chat messages
// Another alternative: set body style touch-action to 'none'. Achieves same result.
useEffect(() => {
document.body.style.overflow = 'hidden';
return () => {
document.body.style.overflow = 'null';
};
}, []);
const callIdRef = useRef<string>();
const { currentTheme, currentRtl } = useSwitchableFluentTheme();
const isMobileSession = useIsMobile();
const credential = useMemo(
() => createAutoRefreshingCredential(toFlatCommunicationIdentifier(userId), token),
[userId, token]
);
const afterAdapterCreate = useCallback(
async (adapter: CallWithChatAdapter): Promise<CallWithChatAdapter> => {
adapter.on('callError', (e) => {
// Error is already acted upon by the Call composite, but the surrounding application could
// add top-level error handling logic here (e.g. reporting telemetry).
console.log('Adapter error event:', e);
});
adapter.on('chatError', (e) => {
// Error is already acted upon by the Chat composite, but the surrounding application could
// add top-level error handling logic here (e.g. reporting telemetry).
console.log('Adapter error event:', e);
});
adapter.onStateChange((state: CallWithChatAdapterState) => {
const pageTitle = convertPageStateToString(state);
document.title = `${pageTitle} - ${WEB_APP_TITLE}`;
if (state?.call?.id && callIdRef.current !== state?.call?.id) {
callIdRef.current = state?.call?.id;
console.log(`Call Id: ${callIdRef.current}`);
}
});
return adapter;
},
[callIdRef]
);
const adapter = useAzureCommunicationCallWithChatAdapter(
{
userId,
displayName,
credential,
endpoint,
locator,
/* @conditional-compile-remove(PSTN-calls) */ alternateCallerId,
/* @conditional-compile-remove(video-background-effects) */ callAdapterOptions
},
afterAdapterCreate
);
const shouldHideScreenShare = isMobileSession || isIOS();
const options: CallWithChatCompositeOptions = useMemo(
() => ({
callControls: {
screenShareButton: shouldHideScreenShare ? false : undefined
}
}),
[shouldHideScreenShare]
);
// Dispose of the adapter in the window's before unload event.
// This ensures the service knows the user intentionally left the call if the user
// closed the browser tab during an active call.
useEffect(() => {
const disposeAdapter = (): void => adapter?.dispose();
window.addEventListener('beforeunload', disposeAdapter);
return () => window.removeEventListener('beforeunload', disposeAdapter);
}, [adapter]);
if (!adapter) {
return <Spinner label={'Creating adapter'} ariaLive="assertive" labelPosition="top" />;
}
let callInvitationUrl: string | undefined = window.location.href;
// Only show the call invitation url if the call is a group call or Teams call, do not show for Rooms, 1:1 or 1:N calls
if (!isGroupCallLocator(locator) && !isTeamsMeetingLinkLocator(locator)) {
callInvitationUrl = undefined;
}
return (
<CallWithChatComposite
adapter={adapter}
fluentTheme={currentTheme.theme}
rtl={currentRtl}
joinInvitationURL={callInvitationUrl}
options={options}
formFactor={isMobileSession ? 'mobile' : 'desktop'}
/>
);
};
const convertPageStateToString = (state: CallWithChatAdapterState): string => {
switch (state.page) {
case 'accessDeniedTeamsMeeting':
return 'error';
case 'leftCall':
return 'end call';
case 'removedFromCall':
return 'end call';
default:
return `${state.page}`;
}
};
const isTeamsMeetingLinkLocator = (
locator: TeamsMeetingLinkLocator | CallAndChatLocator
): locator is TeamsMeetingLinkLocator => {
return 'meetingLink' in locator;
};
const isGroupCallLocator = (locator: TeamsMeetingLinkLocator | CallAndChatLocator): boolean => {
return 'callLocator' in locator && 'groupId' in locator.callLocator;
};