2022-10-06 14:27:12 +00: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.
|
|
|
|
*/
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
import React, { useCallback, useEffect } from "react";
|
2023-08-07 08:24:58 +00:00
|
|
|
import { MatrixEvent, MatrixEventEvent } from "matrix-js-sdk/src/matrix";
|
2022-10-06 14:27:12 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
import { _t } from "../languageHandler";
|
|
|
|
import RoomAvatar from "../components/views/avatars/RoomAvatar";
|
2022-10-06 14:27:12 +00:00
|
|
|
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-28 21:37:32 +00:00
|
|
|
import { useCall, useJoinCallButtonDisabledTooltip } from "../hooks/useCall";
|
2022-10-06 14:27:12 +00:00
|
|
|
import { useRoomState } from "../hooks/useRoomState";
|
|
|
|
import { ButtonEvent } from "../components/views/elements/AccessibleButton";
|
2022-10-07 17:31:22 +00:00
|
|
|
import { useDispatcher } from "../hooks/useDispatcher";
|
|
|
|
import { ActionPayload } from "../dispatcher/payloads";
|
2022-10-07 20:16:35 +00:00
|
|
|
import { Call } from "../models/Call";
|
2023-03-28 13:16:30 +00:00
|
|
|
import { useTypedEventEmitter } from "../hooks/useEventEmitter";
|
2022-10-06 14:27:12 +00:00
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
export const getIncomingCallToastKey = (stateKey: string): string => `call_${stateKey}`;
|
2022-10-06 14:27:12 +00:00
|
|
|
|
2022-10-07 20:16:35 +00:00
|
|
|
interface JoinCallButtonWithCallProps {
|
|
|
|
onClick: (e: ButtonEvent) => void;
|
|
|
|
call: Call;
|
|
|
|
}
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
function JoinCallButtonWithCall({ onClick, call }: JoinCallButtonWithCallProps): JSX.Element {
|
2022-11-28 21:37:32 +00:00
|
|
|
const disabledTooltip = useJoinCallButtonDisabledTooltip(call);
|
2022-10-07 20:16:35 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
return (
|
|
|
|
<AccessibleTooltipButton
|
|
|
|
className="mx_IncomingCallToast_joinButton"
|
|
|
|
onClick={onClick}
|
|
|
|
disabled={disabledTooltip !== null}
|
|
|
|
tooltip={disabledTooltip}
|
|
|
|
kind="primary"
|
|
|
|
>
|
|
|
|
{_t("Join")}
|
|
|
|
</AccessibleTooltipButton>
|
|
|
|
);
|
2022-10-07 20:16:35 +00:00
|
|
|
}
|
|
|
|
|
2022-10-06 14:27:12 +00:00
|
|
|
interface Props {
|
|
|
|
callEvent: MatrixEvent;
|
|
|
|
}
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
export function IncomingCallToast({ callEvent }: Props): JSX.Element {
|
2022-10-06 14:27:12 +00:00
|
|
|
const roomId = callEvent.getRoomId()!;
|
2023-06-21 16:29:44 +00:00
|
|
|
const room = MatrixClientPeg.safeGet().getRoom(roomId) ?? undefined;
|
2022-10-06 14:27:12 +00:00
|
|
|
const call = useCall(roomId);
|
|
|
|
|
|
|
|
const dismissToast = useCallback((): void => {
|
|
|
|
ToastStore.sharedInstance().dismissToast(getIncomingCallToastKey(callEvent.getStateKey()!));
|
|
|
|
}, [callEvent]);
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
const latestEvent = useRoomState(
|
|
|
|
room,
|
|
|
|
useCallback(
|
|
|
|
(state) => {
|
|
|
|
return state.getStateEvents(callEvent.getType(), callEvent.getStateKey()!);
|
|
|
|
},
|
|
|
|
[callEvent],
|
|
|
|
),
|
|
|
|
);
|
2022-10-06 14:27:12 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if ("m.terminated" in latestEvent.getContent()) {
|
|
|
|
dismissToast();
|
|
|
|
}
|
|
|
|
}, [latestEvent, dismissToast]);
|
|
|
|
|
2023-03-28 13:16:30 +00:00
|
|
|
useTypedEventEmitter(latestEvent, MatrixEventEvent.BeforeRedaction, dismissToast);
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
useDispatcher(
|
|
|
|
defaultDispatcher,
|
|
|
|
useCallback(
|
|
|
|
(payload: ActionPayload) => {
|
|
|
|
if (payload.action === Action.ViewRoom && payload.room_id === roomId && payload.view_call) {
|
|
|
|
dismissToast();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[roomId, dismissToast],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
const onJoinClick = useCallback(
|
|
|
|
(e: ButtonEvent): void => {
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
defaultDispatcher.dispatch<ViewRoomPayload>({
|
|
|
|
action: Action.ViewRoom,
|
2023-02-15 13:36:22 +00:00
|
|
|
room_id: room?.roomId,
|
2022-12-12 11:24:14 +00:00
|
|
|
view_call: true,
|
|
|
|
metricsTrigger: undefined,
|
|
|
|
});
|
2022-10-07 17:31:22 +00:00
|
|
|
dismissToast();
|
2022-12-12 11:24:14 +00:00
|
|
|
},
|
|
|
|
[room, dismissToast],
|
|
|
|
);
|
|
|
|
|
|
|
|
const onCloseClick = useCallback(
|
|
|
|
(e: ButtonEvent): void => {
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
dismissToast();
|
|
|
|
},
|
|
|
|
[dismissToast],
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<React.Fragment>
|
|
|
|
<RoomAvatar room={room ?? undefined} height={24} width={24} />
|
|
|
|
<div className="mx_IncomingCallToast_content">
|
|
|
|
<div className="mx_IncomingCallToast_info">
|
|
|
|
<span className="mx_IncomingCallToast_room">{room ? room.name : _t("Unknown room")}</span>
|
|
|
|
<div className="mx_IncomingCallToast_message">{_t("Video call started")}</div>
|
|
|
|
{call ? (
|
|
|
|
<LiveContentSummaryWithCall call={call} />
|
|
|
|
) : (
|
|
|
|
<LiveContentSummary
|
|
|
|
type={LiveContentType.Video}
|
|
|
|
text={_t("Video")}
|
|
|
|
active={false}
|
|
|
|
participantCount={0}
|
|
|
|
/>
|
|
|
|
)}
|
2022-10-06 14:27:12 +00:00
|
|
|
</div>
|
2022-12-12 11:24:14 +00:00
|
|
|
{call ? (
|
|
|
|
<JoinCallButtonWithCall onClick={onJoinClick} call={call} />
|
|
|
|
) : (
|
|
|
|
<AccessibleTooltipButton
|
|
|
|
className="mx_IncomingCallToast_joinButton"
|
|
|
|
onClick={onJoinClick}
|
|
|
|
kind="primary"
|
|
|
|
>
|
|
|
|
{_t("Join")}
|
|
|
|
</AccessibleTooltipButton>
|
|
|
|
)}
|
2022-10-06 14:27:12 +00:00
|
|
|
</div>
|
2022-12-12 11:24:14 +00:00
|
|
|
<AccessibleTooltipButton
|
|
|
|
className="mx_IncomingCallToast_closeButton"
|
|
|
|
onClick={onCloseClick}
|
|
|
|
title={_t("Close")}
|
|
|
|
/>
|
|
|
|
</React.Fragment>
|
|
|
|
);
|
2022-10-06 14:27:12 +00:00
|
|
|
}
|