forked from element-hq/element-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIncomingCallToast.tsx
More file actions
339 lines (314 loc) · 14.1 KB
/
Copy pathIncomingCallToast.tsx
File metadata and controls
339 lines (314 loc) · 14.1 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
/*
Copyright 2024 New Vector Ltd.
Copyright 2022 The Matrix.org Foundation C.I.C.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE files in the repository root for full details.
*/
import React, { type JSX, useCallback, useEffect, useRef, useState } from "react";
import { type Room, type MatrixEvent, type RoomMember, RoomEvent, EventType } from "matrix-js-sdk/src/matrix";
import { Button, ToggleInput, Tooltip, TooltipProvider } from "@vector-im/compound-web";
import VideoCallIcon from "@vector-im/compound-design-tokens/assets/web/icons/video-call-solid";
import CheckIcon from "@vector-im/compound-design-tokens/assets/web/icons/check";
import CrossIcon from "@vector-im/compound-design-tokens/assets/web/icons/close";
import { logger } from "matrix-js-sdk/src/logger";
import { type IRTCNotificationContent } from "matrix-js-sdk/src/matrixrtc";
import { VoiceCallIcon } from "@vector-im/compound-design-tokens/assets/web/icons";
import { AvatarWithDetails } from "@element-hq/web-shared-components";
import { _t } from "../languageHandler";
import RoomAvatar from "../components/views/avatars/RoomAvatar";
import { MatrixClientPeg } from "../MatrixClientPeg";
import defaultDispatcher from "../dispatcher/dispatcher";
import { type ViewRoomPayload } from "../dispatcher/payloads/ViewRoomPayload";
import { Action } from "../dispatcher/actions";
import ToastStore from "../stores/ToastStore";
import { LiveContentSummary, LiveContentType } from "../components/views/rooms/LiveContentSummary";
import { useCall, useJoinCallButtonDisabledTooltip, useParticipantCount } from "../hooks/useCall";
import AccessibleButton, { type ButtonEvent } from "../components/views/elements/AccessibleButton";
import { useDispatcher } from "../hooks/useDispatcher";
import { type ActionPayload } from "../dispatcher/payloads";
import { type Call, CallEvent } from "../models/Call";
import LegacyCallHandler, { AudioID } from "../LegacyCallHandler";
import { useEventEmitter } from "../hooks/useEventEmitter";
import { CallStore, CallStoreEvent } from "../stores/CallStore";
import DMRoomMap from "../utils/DMRoomMap";
/**
* Get the key for the incoming call toast. A combination of the event ID and room ID.
* @param notificationEventId The ID of the notification event.
* @param roomId The ID of the room.
* @returns The key for the incoming call toast.
*/
export const getIncomingCallToastKey = (notificationEventId: string, roomId: string): string =>
`call_${notificationEventId}_${roomId}`;
/**
* Get the ts when the notification event was sent.
* This can be either the origin_server_ts or a ts the sender of this event claims as
* the time they sent it (sender_ts).
* The origin_server_ts is the fallback if sender_ts seems wrong.
* @param event The RTCNotification event.
* @returns The timestamp to use as the expect start time to apply the `lifetime` to.
*/
export const getNotificationEventSendTs = (event: MatrixEvent): number => {
const content = event.getContent() as Partial<IRTCNotificationContent>;
const sendTs = content.sender_ts;
if (sendTs && Math.abs(sendTs - event.getTs()) >= 15000) {
logger.warn(
"Received RTCNotification event. With large sender_ts origin_server_ts offset -> using origin_server_ts",
);
return event.getTs();
}
return sendTs ?? event.getTs();
};
const MAX_RING_TIME_MS = 90 * 1000;
interface JoinCallButtonWithCallProps {
onClick: (e: ButtonEvent) => void;
call: Call | null;
disabledTooltip: string | undefined;
isRinging: boolean;
}
function JoinCallButtonWithCall({
onClick,
call,
disabledTooltip,
isRinging,
}: JoinCallButtonWithCallProps): JSX.Element {
let disTooltip = disabledTooltip;
const disabledBecauseFullTooltip = useJoinCallButtonDisabledTooltip(call);
disTooltip = disabledTooltip ?? disabledBecauseFullTooltip ?? undefined;
return (
<Tooltip description={disTooltip ?? _t("voip|video_call")}>
<Button
className="mx_IncomingCallToast_actionButton"
onClick={onClick}
disabled={disTooltip != undefined}
kind="primary"
Icon={CheckIcon}
size="sm"
>
{isRinging ? _t("action|accept") : _t("action|join")}
</Button>
</Tooltip>
);
}
interface DeclineCallButtonWithNotificationEventProps {
onDeclined: (e: ButtonEvent) => void;
notificationEvent: MatrixEvent;
room?: Room;
}
function DeclineCallButtonWithNotificationEvent({
notificationEvent,
room,
onDeclined,
}: DeclineCallButtonWithNotificationEventProps): JSX.Element {
const [declining, setDeclining] = useState(false);
const onClick = useCallback(
async (e: ButtonEvent) => {
e.stopPropagation();
setDeclining(true);
await room?.client.sendRtcDecline(room.roomId, notificationEvent.getId() ?? "");
onDeclined(e);
},
[notificationEvent, onDeclined, room?.client, room?.roomId],
);
return (
<Tooltip description={_t("voip|decline_call")}>
<Button
className="mx_IncomingCallToast_actionButton"
onClick={onClick}
kind="primary"
destructive
disabled={declining}
Icon={CrossIcon}
size="sm"
>
{_t("action|decline")}
</Button>
</Tooltip>
);
}
interface Props {
notificationEvent: MatrixEvent;
}
export function IncomingCallToast({ notificationEvent }: Props): JSX.Element {
const roomId = notificationEvent.getRoomId()!;
// Use a partial type so ts still helps us to not miss any type checks.
const notificationContent = notificationEvent.getContent() as Partial<IRTCNotificationContent>;
const room = MatrixClientPeg.safeGet().getRoom(roomId) ?? undefined;
const call = useCall(roomId);
const [connectedCalls, setConnectedCalls] = useState<Call[]>(Array.from(CallStore.instance.connectedCalls));
useEventEmitter(CallStore.instance, CallStoreEvent.ConnectedCalls, () => {
setConnectedCalls(Array.from(CallStore.instance.connectedCalls));
});
const otherCallIsOngoing = connectedCalls.find((call) => call.roomId !== roomId);
const soundHasStarted = useRef<boolean>(false);
useEffect(() => {
// This section can race, so we use a ref to keep track of whether we have started trying to play.
// This is because `LegacyCallHandler.play` tries to load the sound and then play it asynchonously
// and `LegacyCallHandler.isPlaying` will not be `true` until the sound starts playing.
const isRingToast = notificationContent.notification_type === "ring";
if (isRingToast && !soundHasStarted.current && !LegacyCallHandler.instance.isPlaying(AudioID.Ring)) {
// Start ringing if not already.
soundHasStarted.current = true;
void LegacyCallHandler.instance.play(AudioID.Ring);
}
}, [notificationContent.notification_type, soundHasStarted]);
// Stop ringing on dismiss.
const dismissToast = useCallback((): void => {
const notificationId = notificationEvent.getId();
if (!notificationId) {
logger.warn("Could not get eventId for RTCNotification event");
return;
}
ToastStore.sharedInstance().dismissToast(getIncomingCallToastKey(notificationId, roomId));
LegacyCallHandler.instance.pause(AudioID.Ring);
}, [notificationEvent, roomId]);
// Dismiss if session got ended remotely.
const onCall = useCallback(
(call: Call, callRoomId: string): void => {
const roomId = notificationEvent.getRoomId();
if (!roomId && roomId !== callRoomId) return;
if (call === null || call.participants.size === 0) {
dismissToast();
}
},
[dismissToast, notificationEvent],
);
// Dismiss if session got declined remotely.
const onTimelineChange = useCallback(
(ev: MatrixEvent) => {
const userId = room?.client.getUserId();
if (
ev.getType() === EventType.RTCDecline &&
userId !== undefined &&
ev.getSender() === userId && // It is our decline not someone elses
ev.relationEventId === notificationEvent.getId() // The event declines this ringing toast.
) {
dismissToast();
}
},
[dismissToast, notificationEvent, room?.client],
);
// Dismiss if another device from this user joins.
const onParticipantChange = useCallback(
(participants: Map<RoomMember, Set<string>>, prevParticipants: Map<RoomMember, Set<string>>) => {
if (Array.from(participants.keys()).some((p) => p.userId == room?.client.getUserId())) {
dismissToast();
}
},
[dismissToast, room?.client],
);
// Dismiss on timeout.
useEffect(() => {
const lifetime = notificationContent.lifetime ?? MAX_RING_TIME_MS;
const timeout = setTimeout(dismissToast, getNotificationEventSendTs(notificationEvent) + lifetime - Date.now());
return () => clearTimeout(timeout);
});
// Dismiss on viewing call.
useDispatcher(
defaultDispatcher,
useCallback(
(payload: ActionPayload) => {
if (payload.action === Action.ViewRoom && payload.room_id === roomId && payload.view_call) {
dismissToast();
}
},
[roomId, dismissToast],
),
);
const [skipLobbyToggle, setSkipLobbyToggle] = useState(true);
// Dismiss on clicking join.
// If the skip lobby option is undefined, it will use to the shift key state to decide if the lobby is skipped.
const onJoinClick = useCallback(
(e: ButtonEvent): void => {
e.stopPropagation();
// The toast will be automatically dismissed by the dispatcher callback above
defaultDispatcher.dispatch<ViewRoomPayload>({
action: Action.ViewRoom,
room_id: room?.roomId,
view_call: true,
skipLobby: ("shiftKey" in e && e.shiftKey) || skipLobbyToggle,
voiceOnly: notificationContent["m.call.intent"] === "audio",
metricsTrigger: undefined,
});
},
[room, skipLobbyToggle, notificationContent],
);
// Dismiss on closing toast.
const onCloseClick = useCallback(
(e: ButtonEvent): void => {
e.stopPropagation();
dismissToast();
},
[dismissToast],
);
useEventEmitter(CallStore.instance, CallStoreEvent.Call, onCall);
useEventEmitter(call ?? undefined, CallEvent.Participants, onParticipantChange);
useEventEmitter(room, RoomEvent.Timeline, onTimelineChange);
const isVoice = notificationContent["m.call.intent"] === "audio";
const otherUserId = DMRoomMap.shared().getUserIdForRoomId(roomId);
const participantCount = useParticipantCount(call);
const detailsInformation =
notificationContent.notification_type === "ring" ? (
<span>{otherUserId}</span>
) : (
<LiveContentSummary
type={isVoice ? LiveContentType.Voice : LiveContentType.Video}
text={isVoice ? _t("common|voice") : _t("common|video")}
active={false}
participantCount={participantCount}
/>
);
return (
<TooltipProvider>
<>
<div className="mx_IncomingCallToast_content">
{isVoice ? (
<div className="mx_IncomingCallToast_message">
<VoiceCallIcon width="20px" height="20px" style={{ position: "relative", top: "4px" }} />{" "}
{_t("voip|voice_call_incoming")}
</div>
) : (
<div className="mx_IncomingCallToast_message">
<VideoCallIcon width="20px" height="20px" style={{ position: "relative", top: "4px" }} />{" "}
{notificationContent.notification_type === "ring"
? _t("voip|video_call_incoming")
: _t("voip|video_call_started")}
</div>
)}
<AvatarWithDetails
avatar={<RoomAvatar room={room ?? undefined} size="32px" />}
details={detailsInformation}
title={room ? room.name : _t("voip|call_toast_unknown_room")}
className="mx_IncomingCallToast_AvatarWithDetails"
/>
{!isVoice && (
<div className="mx_IncomingCallToast_toggleWithLabel">
<span>{_t("voip|skip_lobby_toggle_option")}</span>
<ToggleInput
onChange={(e) => setSkipLobbyToggle(e.target.checked)}
checked={skipLobbyToggle}
/>
</div>
)}
<div className="mx_IncomingCallToast_buttons">
<DeclineCallButtonWithNotificationEvent
notificationEvent={notificationEvent}
room={room}
onDeclined={onCloseClick}
/>
<JoinCallButtonWithCall
onClick={onJoinClick}
call={call}
isRinging={notificationContent.notification_type === "ring"}
disabledTooltip={otherCallIsOngoing ? "Ongoing call" : undefined}
/>
</div>
</div>
<AccessibleButton
className="mx_IncomingCallToast_closeButton"
onClick={onCloseClick}
title={_t("action|close")}
/>
</>
</TooltipProvider>
);
}