|
| 1 | +import { useEffect, useState } from 'react'; |
| 2 | +import AgoraRTC from 'agora-rtc-sdk-ng'; |
| 3 | +import { useJoin, usePublish, useRemoteUsers, useRTCClient, LocalUser, RemoteUser } from 'agora-rtc-react'; |
| 4 | + |
| 5 | +import { CallStatus } from 'shared/constants'; |
| 6 | + |
| 7 | +import Alert from '../Components/Alert'; |
| 8 | +import ApiService from '../ApiService'; |
| 9 | +import MicrophoneButton from './MicrophoneButton'; |
| 10 | +import CameraButton from './CameraButton'; |
| 11 | +import Ringdown from '../Models/Ringdown'; |
| 12 | +import RingdownDetails from '../Components/RingdownDetails'; |
| 13 | +import Spinner from '../Components/Spinner'; |
| 14 | + |
| 15 | +import './CallInterface.scss'; |
| 16 | +import EndCallButton from './EndCallButton'; |
| 17 | + |
| 18 | +export default function CallInterface({ call, setCall }) { |
| 19 | + const channel = call?.userId; |
| 20 | + const client = useRTCClient(); |
| 21 | + const { isConnected } = useJoin(async () => { |
| 22 | + const response = await ApiService.agora.getRtcToken(channel); |
| 23 | + const { token } = response.data; |
| 24 | + return { |
| 25 | + appid: window.env.REACT_APP_AGORA_APP_ID, |
| 26 | + channel, |
| 27 | + token, |
| 28 | + }; |
| 29 | + }, !!channel); |
| 30 | + |
| 31 | + const [isMicOn, setMicOn] = useState(true); |
| 32 | + const [isCameraOn, setCameraOn] = useState(false); |
| 33 | + |
| 34 | + const [localMicrophoneTrack, setLocalMicrophoneTrack] = useState(); |
| 35 | + useEffect(() => { |
| 36 | + if (isConnected) { |
| 37 | + if (!localMicrophoneTrack && isMicOn) { |
| 38 | + AgoraRTC.createMicrophoneAudioTrack() |
| 39 | + .then((result) => { |
| 40 | + setLocalMicrophoneTrack(result); |
| 41 | + }) |
| 42 | + .catch((err) => { |
| 43 | + console.error(err); |
| 44 | + }); |
| 45 | + } |
| 46 | + } |
| 47 | + }, [client, isConnected, localMicrophoneTrack, isMicOn]); |
| 48 | + const [localCameraTrack, setLocalCameraTrack] = useState(); |
| 49 | + useEffect(() => { |
| 50 | + if (isConnected) { |
| 51 | + if (!localCameraTrack && isCameraOn) { |
| 52 | + AgoraRTC.createCameraVideoTrack() |
| 53 | + .then((result) => { |
| 54 | + setLocalCameraTrack(result); |
| 55 | + }) |
| 56 | + .catch((err) => { |
| 57 | + console.error(err); |
| 58 | + }); |
| 59 | + } |
| 60 | + } |
| 61 | + }, [client, isConnected, localCameraTrack, isCameraOn]); |
| 62 | + usePublish([localMicrophoneTrack, localCameraTrack]); |
| 63 | + |
| 64 | + const remoteUsers = useRemoteUsers(); |
| 65 | + const [activeUser, setActiveUser] = useState(); |
| 66 | + useEffect(() => { |
| 67 | + if (remoteUsers?.length > 0) { |
| 68 | + setActiveUser((prevActiveUser) => { |
| 69 | + if (!prevActiveUser && call?.status === CallStatus.RINGING) { |
| 70 | + setCall({ ...call, status: CallStatus.ANSWERED, answeredAt: new Date().toISOString() }); |
| 71 | + } |
| 72 | + return remoteUsers[0]; |
| 73 | + }); |
| 74 | + } else { |
| 75 | + setActiveUser((prevActiveUser) => { |
| 76 | + if (prevActiveUser) { |
| 77 | + setEnded(true); |
| 78 | + } |
| 79 | + }); |
| 80 | + } |
| 81 | + }, [remoteUsers, call, setCall]); |
| 82 | + |
| 83 | + const [isShowingConfirmEnd, setShowingConfirmEnd] = useState(false); |
| 84 | + const [isEnded, setEnded] = useState(false); |
| 85 | + |
| 86 | + return ( |
| 87 | + <div className="grid-row call-interface"> |
| 88 | + <div className="tablet:grid-col-9"> |
| 89 | + <div className="call-interface-content"> |
| 90 | + <div className="call-interface-content__video"> |
| 91 | + {activeUser && <RemoteUser user={activeUser} videoPlayerConfig={{ fit: 'contain' }} cover="/img/user.png" />} |
| 92 | + {isConnected && ( |
| 93 | + <div className="call-interface-content__local-user"> |
| 94 | + <LocalUser |
| 95 | + audioTrack={localMicrophoneTrack} |
| 96 | + cameraOn={isCameraOn} |
| 97 | + cover="/img/user.png" |
| 98 | + micOn={isMicOn} |
| 99 | + playAudio={false} |
| 100 | + playVideo={isCameraOn} |
| 101 | + videoTrack={localCameraTrack} |
| 102 | + /> |
| 103 | + </div> |
| 104 | + )} |
| 105 | + </div> |
| 106 | + {(!isConnected || !CallStatus.is(call?.status, CallStatus.ANSWERED)) && ( |
| 107 | + <div className="call-interface-content__status"> |
| 108 | + <Spinner /> |
| 109 | + {!isConnected && 'Connecting...'} |
| 110 | + {isConnected && call?.status === CallStatus.RINGING && 'Ringing...'} |
| 111 | + </div> |
| 112 | + )} |
| 113 | + <div className="call-interface-content__controls"> |
| 114 | + <MicrophoneButton disabled={!isConnected} isMicOn={isMicOn} onClick={() => setMicOn((prev) => !prev)} /> |
| 115 | + <CameraButton disabled={!isConnected} isCameraOn={isCameraOn} onClick={() => setCameraOn((prev) => !prev)} /> |
| 116 | + <EndCallButton disabled={!isConnected} onClick={() => setShowingConfirmEnd(true)} /> |
| 117 | + </div> |
| 118 | + </div> |
| 119 | + </div> |
| 120 | + <div className="tablet:grid-col-3 call-interface-sidebar"> |
| 121 | + <div className="usa-accordion"> |
| 122 | + <div className="usa-accordion__content"> |
| 123 | + <RingdownDetails ringdown={new Ringdown(call.ringdown)} /> |
| 124 | + </div> |
| 125 | + </div> |
| 126 | + </div> |
| 127 | + {call?.status === CallStatus.DECLINED && ( |
| 128 | + <Alert |
| 129 | + type="error" |
| 130 | + title="Call Declined" |
| 131 | + cancel="No" |
| 132 | + primary="Yes" |
| 133 | + onCancel={() => window.close()} |
| 134 | + onPrimary={() => window.location.reload()} |
| 135 | + > |
| 136 | + The call was declined on the receiving end. Ring again? |
| 137 | + </Alert> |
| 138 | + )} |
| 139 | + {isShowingConfirmEnd && ( |
| 140 | + <Alert |
| 141 | + type="info" |
| 142 | + title="End Call?" |
| 143 | + cancel="No" |
| 144 | + primary="Yes" |
| 145 | + onCancel={() => setShowingConfirmEnd(false)} |
| 146 | + onPrimary={() => window.close()} |
| 147 | + > |
| 148 | + Are you sure you want to end the call? |
| 149 | + </Alert> |
| 150 | + )} |
| 151 | + {isEnded && ( |
| 152 | + <Alert type="info" title="Call Ended" primary="OK" onPrimary={() => window.close()}> |
| 153 | + The call was ended by the other side. |
| 154 | + </Alert> |
| 155 | + )} |
| 156 | + </div> |
| 157 | + ); |
| 158 | +} |
0 commit comments