2022-10-06 17:27:12 +03:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2023-11-21 20:12:08 +03:00
|
|
|
import React, { useCallback, useEffect, useMemo } from "react";
|
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/matrix";
|
|
|
|
// eslint-disable-next-line no-restricted-imports
|
|
|
|
import { MatrixRTCSessionManagerEvents } from "matrix-js-sdk/src/matrixrtc/MatrixRTCSessionManager";
|
|
|
|
// eslint-disable-next-line no-restricted-imports
|
|
|
|
import { MatrixRTCSession } from "matrix-js-sdk/src/matrixrtc/MatrixRTCSession";
|
2022-10-06 17:27:12 +03:00
|
|
|
|
|
|
|
import { _t } from "../languageHandler";
|
|
|
|
import RoomAvatar from "../components/views/avatars/RoomAvatar";
|
|
|
|
import { MatrixClientPeg } from "../MatrixClientPeg";
|
|
|
|
import defaultDispatcher from "../dispatcher/dispatcher";
|
|
|
|
import { ViewRoomPayload } from "../dispatcher/payloads/ViewRoomPayload";
|
|
|
|
import { Action } from "../dispatcher/actions";
|
|
|
|
import ToastStore from "../stores/ToastStore";
|
|
|
|
import AccessibleTooltipButton from "../components/views/elements/AccessibleTooltipButton";
|
|
|
|
import {
|
|
|
|
LiveContentSummary,
|
|
|
|
LiveContentSummaryWithCall,
|
|
|
|
LiveContentType,
|
|
|
|
} from "../components/views/rooms/LiveContentSummary";
|
2022-11-29 00:37:32 +03:00
|
|
|
import { useCall, useJoinCallButtonDisabledTooltip } from "../hooks/useCall";
|
2022-10-06 17:27:12 +03:00
|
|
|
import { ButtonEvent } from "../components/views/elements/AccessibleButton";
|
2022-10-07 20:31:22 +03:00
|
|
|
import { useDispatcher } from "../hooks/useDispatcher";
|
|
|
|
import { ActionPayload } from "../dispatcher/payloads";
|
2022-10-07 23:16:35 +03:00
|
|
|
import { Call } from "../models/Call";
|
2023-11-21 20:12:08 +03:00
|
|
|
import { AudioID } from "../LegacyCallHandler";
|
2023-03-28 16:16:30 +03:00
|
|
|
import { useTypedEventEmitter } from "../hooks/useEventEmitter";
|
2022-10-06 17:27:12 +03:00
|
|
|
|
2023-11-21 20:12:08 +03:00
|
|
|
export const getIncomingCallToastKey = (callId: string, roomId: string): string => `call_${callId}_${roomId}`;
|
|
|
|
const MAX_RING_TIME_MS = 10 * 1000;
|
2022-10-06 17:27:12 +03:00
|
|
|
|
2022-10-07 23:16:35 +03:00
|
|
|
interface JoinCallButtonWithCallProps {
|
|
|
|
onClick: (e: ButtonEvent) => void;
|
|
|
|
call: Call;
|
|
|
|
}
|
|
|
|
|
2023-01-12 16:25:14 +03:00
|
|
|
function JoinCallButtonWithCall({ onClick, call }: JoinCallButtonWithCallProps): JSX.Element {
|
2022-11-29 00:37:32 +03:00
|
|
|
const disabledTooltip = useJoinCallButtonDisabledTooltip(call);
|
2022-10-07 23:16:35 +03:00
|
|
|
|
|
|
|
return (
|
|
|
|
<AccessibleTooltipButton
|
|
|
|
className="mx_IncomingCallToast_joinButton"
|
|
|
|
onClick={onClick}
|
2022-11-29 00:37:32 +03:00
|
|
|
disabled={disabledTooltip !== null}
|
2023-12-19 20:19:54 +03:00
|
|
|
tooltip={disabledTooltip ?? undefined}
|
2022-10-07 23:16:35 +03:00
|
|
|
kind="primary"
|
|
|
|
>
|
2023-08-23 13:57:22 +03:00
|
|
|
{_t("action|join")}
|
2022-10-07 23:16:35 +03:00
|
|
|
</AccessibleTooltipButton>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-10-06 17:27:12 +03:00
|
|
|
interface Props {
|
2023-11-21 20:12:08 +03:00
|
|
|
notifyEvent: MatrixEvent;
|
2022-10-06 17:27:12 +03:00
|
|
|
}
|
|
|
|
|
2023-11-21 20:12:08 +03:00
|
|
|
export function IncomingCallToast({ notifyEvent }: Props): JSX.Element {
|
|
|
|
const roomId = notifyEvent.getRoomId()!;
|
2023-06-21 19:29:44 +03:00
|
|
|
const room = MatrixClientPeg.safeGet().getRoom(roomId) ?? undefined;
|
2022-10-06 17:27:12 +03:00
|
|
|
const call = useCall(roomId);
|
2023-11-21 20:12:08 +03:00
|
|
|
const audio = useMemo(() => document.getElementById(AudioID.Ring) as HTMLMediaElement, []);
|
2022-10-06 17:27:12 +03:00
|
|
|
|
2023-11-21 20:12:08 +03:00
|
|
|
// Start ringing if not already.
|
|
|
|
useEffect(() => {
|
|
|
|
const isRingToast = (notifyEvent.getContent() as unknown as { notify_type: string })["notify_type"] == "ring";
|
|
|
|
if (isRingToast && audio.paused) {
|
|
|
|
audio.play();
|
|
|
|
}
|
|
|
|
}, [audio, notifyEvent]);
|
2022-10-06 17:27:12 +03:00
|
|
|
|
2023-11-21 20:12:08 +03:00
|
|
|
// Stop ringing on dismiss.
|
|
|
|
const dismissToast = useCallback((): void => {
|
|
|
|
ToastStore.sharedInstance().dismissToast(
|
|
|
|
getIncomingCallToastKey(notifyEvent.getContent().call_id ?? "", roomId),
|
|
|
|
);
|
|
|
|
audio.pause();
|
|
|
|
}, [audio, notifyEvent, roomId]);
|
|
|
|
|
|
|
|
// Dismiss if session got ended remotely.
|
|
|
|
const onSessionEnded = useCallback(
|
|
|
|
(endedSessionRoomId: string, session: MatrixRTCSession): void => {
|
|
|
|
if (roomId == endedSessionRoomId && session.callId == notifyEvent.getContent().call_id) {
|
|
|
|
dismissToast();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[dismissToast, notifyEvent, roomId],
|
2022-10-06 17:27:12 +03:00
|
|
|
);
|
|
|
|
|
2023-11-21 20:12:08 +03:00
|
|
|
// Dismiss on timeout.
|
2022-10-06 17:27:12 +03:00
|
|
|
useEffect(() => {
|
2023-11-21 20:12:08 +03:00
|
|
|
const timeout = setTimeout(dismissToast, MAX_RING_TIME_MS);
|
|
|
|
return () => clearTimeout(timeout);
|
|
|
|
});
|
2023-03-28 16:16:30 +03:00
|
|
|
|
2023-11-21 20:12:08 +03:00
|
|
|
// Dismiss on viewing call.
|
2022-10-07 20:31:22 +03:00
|
|
|
useDispatcher(
|
|
|
|
defaultDispatcher,
|
|
|
|
useCallback(
|
|
|
|
(payload: ActionPayload) => {
|
2022-10-06 17:27:12 +03:00
|
|
|
if (payload.action === Action.ViewRoom && payload.room_id === roomId && payload.view_call) {
|
|
|
|
dismissToast();
|
2022-12-12 14:24:14 +03:00
|
|
|
}
|
|
|
|
},
|
2022-10-07 20:31:22 +03:00
|
|
|
[roomId, dismissToast],
|
2022-12-12 14:24:14 +03:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2023-11-21 20:12:08 +03:00
|
|
|
// Dismiss on clicking join.
|
2022-10-06 17:27:12 +03:00
|
|
|
const onJoinClick = useCallback(
|
|
|
|
(e: ButtonEvent): void => {
|
|
|
|
e.stopPropagation();
|
2022-12-12 14:24:14 +03:00
|
|
|
|
2023-11-21 20:12:08 +03:00
|
|
|
// The toast will be automatically dismissed by the dispatcher callback above
|
2022-10-07 20:31:22 +03:00
|
|
|
defaultDispatcher.dispatch<ViewRoomPayload>({
|
|
|
|
action: Action.ViewRoom,
|
2023-02-15 16:36:22 +03:00
|
|
|
room_id: room?.roomId,
|
2022-10-07 20:31:22 +03:00
|
|
|
view_call: true,
|
2024-01-29 19:06:12 +03:00
|
|
|
skipLobby: "shiftKey" in e ? e.shiftKey : false,
|
2022-10-06 17:27:12 +03:00
|
|
|
metricsTrigger: undefined,
|
2022-12-12 14:24:14 +03:00
|
|
|
});
|
2022-10-07 20:31:22 +03:00
|
|
|
},
|
2023-11-21 20:12:08 +03:00
|
|
|
[room],
|
2022-10-07 20:31:22 +03:00
|
|
|
);
|
2022-12-12 14:24:14 +03:00
|
|
|
|
2023-11-21 20:12:08 +03:00
|
|
|
// Dismiss on closing toast.
|
2022-10-06 17:27:12 +03:00
|
|
|
const onCloseClick = useCallback(
|
|
|
|
(e: ButtonEvent): void => {
|
|
|
|
e.stopPropagation();
|
2022-12-12 14:24:14 +03:00
|
|
|
|
2022-10-06 17:27:12 +03:00
|
|
|
dismissToast();
|
|
|
|
},
|
|
|
|
[dismissToast],
|
|
|
|
);
|
2022-12-12 14:24:14 +03:00
|
|
|
|
2023-11-21 20:12:08 +03:00
|
|
|
useTypedEventEmitter(
|
|
|
|
MatrixClientPeg.safeGet().matrixRTC,
|
|
|
|
MatrixRTCSessionManagerEvents.SessionEnded,
|
|
|
|
onSessionEnded,
|
|
|
|
);
|
|
|
|
|
2022-10-06 17:27:12 +03:00
|
|
|
return (
|
|
|
|
<React.Fragment>
|
2023-11-21 20:12:08 +03:00
|
|
|
<div>
|
|
|
|
<RoomAvatar room={room ?? undefined} size="24px" />
|
|
|
|
</div>
|
2022-10-06 17:27:12 +03:00
|
|
|
<div className="mx_IncomingCallToast_content">
|
|
|
|
<div className="mx_IncomingCallToast_info">
|
2023-09-25 20:12:41 +03:00
|
|
|
<span className="mx_IncomingCallToast_room">
|
|
|
|
{room ? room.name : _t("voip|call_toast_unknown_room")}
|
|
|
|
</span>
|
2023-09-05 19:52:06 +03:00
|
|
|
<div className="mx_IncomingCallToast_message">{_t("voip|video_call_started")}</div>
|
2022-10-06 17:27:12 +03:00
|
|
|
{call ? (
|
|
|
|
<LiveContentSummaryWithCall call={call} />
|
|
|
|
) : (
|
|
|
|
<LiveContentSummary
|
|
|
|
type={LiveContentType.Video}
|
2023-08-22 20:47:33 +03:00
|
|
|
text={_t("common|video")}
|
2022-10-06 17:27:12 +03:00
|
|
|
active={false}
|
|
|
|
participantCount={0}
|
2022-12-12 14:24:14 +03:00
|
|
|
/>
|
|
|
|
)}
|
2022-10-06 17:27:12 +03:00
|
|
|
</div>
|
|
|
|
{call ? (
|
|
|
|
<JoinCallButtonWithCall onClick={onJoinClick} call={call} />
|
|
|
|
) : (
|
|
|
|
<AccessibleTooltipButton
|
|
|
|
className="mx_IncomingCallToast_joinButton"
|
|
|
|
onClick={onJoinClick}
|
2022-10-07 23:16:35 +03:00
|
|
|
kind="primary"
|
2022-12-12 14:24:14 +03:00
|
|
|
>
|
2023-08-23 13:57:22 +03:00
|
|
|
{_t("action|join")}
|
2022-10-06 17:27:12 +03:00
|
|
|
</AccessibleTooltipButton>
|
|
|
|
)}
|
|
|
|
</div>
|
2022-10-07 23:16:35 +03:00
|
|
|
<AccessibleTooltipButton
|
2022-10-06 17:27:12 +03:00
|
|
|
className="mx_IncomingCallToast_closeButton"
|
|
|
|
onClick={onCloseClick}
|
2023-08-23 13:57:22 +03:00
|
|
|
title={_t("action|close")}
|
2022-10-06 17:27:12 +03:00
|
|
|
/>
|
|
|
|
</React.Fragment>
|
|
|
|
);
|
|
|
|
}
|