Files
ThreadNet-Web/src/components/views/voip/IncomingCallBox.tsx
T

166 lines
5.5 KiB
TypeScript
Raw Normal View History

2020-07-06 22:42:46 +01:00
/*
Copyright 2015, 2016 OpenMarket Ltd
Copyright 2018 New Vector Ltd
Copyright 2019, 2020 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.
*/
2020-07-07 15:11:08 +01:00
2020-07-06 22:42:46 +01:00
import React from 'react';
import {MatrixClientPeg} from '../../../MatrixClientPeg';
import dis from '../../../dispatcher/dispatcher';
import { _t } from '../../../languageHandler';
import { ActionPayload } from '../../../dispatcher/payloads';
2021-05-22 20:24:36 +02:00
import CallHandler, { AudioID } from '../../../CallHandler';
2020-07-06 22:42:46 +01:00
import RoomAvatar from '../avatars/RoomAvatar';
import AccesibleButton from '../elements/AccessibleButton';
2020-12-17 17:16:00 +00:00
import { CallState } from 'matrix-js-sdk/src/webrtc/call';
import {replaceableComponent} from "../../../utils/replaceableComponent";
2021-05-22 20:24:36 +02:00
import AccessibleTooltipButton from '../elements/AccessibleTooltipButton';
import classNames from 'classnames';
2020-07-06 22:42:46 +01:00
interface IProps {
}
interface IState {
incomingCall: any;
2021-05-22 20:24:36 +02:00
silenced: boolean;
2020-07-06 22:42:46 +01:00
}
@replaceableComponent("views.voip.IncomingCallBox")
2020-07-17 15:56:58 -06:00
export default class IncomingCallBox extends React.Component<IProps, IState> {
2020-07-06 22:42:46 +01:00
private dispatcherRef: string;
constructor(props: IProps) {
super(props);
2020-07-07 15:40:05 +01:00
this.dispatcherRef = dis.register(this.onAction);
2020-07-06 22:42:46 +01:00
this.state = {
incomingCall: null,
2021-05-22 20:24:36 +02:00
silenced: false,
2020-07-07 15:40:05 +01:00
};
2020-07-06 22:42:46 +01:00
}
public componentWillUnmount() {
dis.unregister(this.dispatcherRef);
}
private onAction = (payload: ActionPayload) => {
switch (payload.action) {
case 'call_state': {
2020-09-24 16:16:20 +01:00
const call = CallHandler.sharedInstance().getCallForRoom(payload.room_id);
2020-10-09 18:56:07 +01:00
if (call && call.state === CallState.Ringing) {
2020-07-06 22:42:46 +01:00
this.setState({
incomingCall: call,
2021-05-22 20:24:36 +02:00
silenced: false, // Reset silenced state for new call
2020-07-06 22:42:46 +01:00
});
} else {
this.setState({
incomingCall: null,
});
}
}
2020-07-06 22:42:46 +01:00
}
2020-07-07 15:40:05 +01:00
};
2020-07-06 22:42:46 +01:00
private onAnswerClick: React.MouseEventHandler = (e) => {
e.stopPropagation();
dis.dispatch({
action: 'answer',
2021-04-19 20:30:51 +01:00
room_id: CallHandler.sharedInstance().roomIdForCall(this.state.incomingCall),
2020-07-06 22:42:46 +01:00
});
2020-07-07 15:40:05 +01:00
};
2020-07-06 22:42:46 +01:00
private onRejectClick: React.MouseEventHandler = (e) => {
e.stopPropagation();
dis.dispatch({
2020-10-15 14:54:03 +01:00
action: 'reject',
2021-04-19 20:30:51 +01:00
room_id: CallHandler.sharedInstance().roomIdForCall(this.state.incomingCall),
2020-07-06 22:42:46 +01:00
});
2020-07-07 15:40:05 +01:00
};
2020-07-06 22:42:46 +01:00
2021-05-22 20:24:36 +02:00
private onSilenceClick: React.MouseEventHandler = (e) => {
e.stopPropagation();
const newState = !this.state.silenced
this.setState({silenced: newState});
newState ? CallHandler.sharedInstance().pause(AudioID.Ring) : CallHandler.sharedInstance().play(AudioID.Ring);
}
2020-07-06 22:42:46 +01:00
public render() {
if (!this.state.incomingCall) {
return null;
}
let room = null;
if (this.state.incomingCall) {
2021-04-19 20:30:51 +01:00
room = MatrixClientPeg.get().getRoom(CallHandler.sharedInstance().roomIdForCall(this.state.incomingCall));
2020-07-06 22:42:46 +01:00
}
const caller = room ? room.name : _t("Unknown caller");
let incomingCallText = null;
if (this.state.incomingCall) {
if (this.state.incomingCall.type === "voice") {
incomingCallText = _t("Incoming voice call");
} else if (this.state.incomingCall.type === "video") {
incomingCallText = _t("Incoming video call");
} else {
incomingCallText = _t("Incoming call");
}
}
2021-05-22 20:24:36 +02:00
const silenceClass = classNames({
"mx_IncomingCallBox_iconButton": true,
"mx_IncomingCallBox_unSilence": this.state.silenced,
"mx_IncomingCallBox_silence": !this.state.silenced,
});
2020-07-17 15:56:58 -06:00
return <div className="mx_IncomingCallBox">
<div className="mx_IncomingCallBox_CallerInfo">
2020-11-12 18:09:56 +00:00
<RoomAvatar
room={room}
height={32}
width={32}
/>
2020-07-06 22:42:46 +01:00
<div>
<h1>{caller}</h1>
<p>{incomingCallText}</p>
</div>
2021-05-22 20:24:36 +02:00
<AccessibleTooltipButton
className={silenceClass}
onClick={this.onSilenceClick}
title={this.state.silenced ? _t("Sound on"): _t("Silence call")}
/>
2020-07-06 22:42:46 +01:00
</div>
2020-07-17 15:56:58 -06:00
<div className="mx_IncomingCallBox_buttons">
<AccesibleButton
2020-07-17 15:56:58 -06:00
className={"mx_IncomingCallBox_decline"}
2020-07-06 22:42:46 +01:00
onClick={this.onRejectClick}
kind="danger"
>
{_t("Decline")}
</AccesibleButton>
2020-07-17 15:56:58 -06:00
<div className="mx_IncomingCallBox_spacer" />
<AccesibleButton
2020-07-17 15:56:58 -06:00
className={"mx_IncomingCallBox_accept"}
2020-07-06 22:42:46 +01:00
onClick={this.onAnswerClick}
kind="primary"
>
{_t("Accept")}
</AccesibleButton>
2020-07-06 22:42:46 +01:00
</div>
</div>;
}
}