|
| 1 | +/* |
| 2 | +Copyright 2022 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import React, { forwardRef, useCallback, useContext, useEffect, useMemo, useState } from "react"; |
| 18 | + |
| 19 | +import type { MatrixEvent } from "matrix-js-sdk/src/models/event"; |
| 20 | +import { Call, ConnectionState } from "../../../models/Call"; |
| 21 | +import { _t } from "../../../languageHandler"; |
| 22 | +import { useCall, useConnectionState, useParticipants } from "../../../hooks/useCall"; |
| 23 | +import defaultDispatcher from "../../../dispatcher/dispatcher"; |
| 24 | +import type { ViewRoomPayload } from "../../../dispatcher/payloads/ViewRoomPayload"; |
| 25 | +import { Action } from "../../../dispatcher/actions"; |
| 26 | +import type { ButtonEvent } from "../elements/AccessibleButton"; |
| 27 | +import MemberAvatar from "../avatars/MemberAvatar"; |
| 28 | +import { LiveContentSummary, LiveContentType } from "../rooms/LiveContentSummary"; |
| 29 | +import FacePile from "../elements/FacePile"; |
| 30 | +import { formatCallTime } from "../../../DateUtils"; |
| 31 | +import AccessibleButton from "../elements/AccessibleButton"; |
| 32 | +import MatrixClientContext from "../../../contexts/MatrixClientContext"; |
| 33 | + |
| 34 | +interface ActiveCallEventProps { |
| 35 | + mxEvent: MatrixEvent; |
| 36 | + call: Call; |
| 37 | +} |
| 38 | + |
| 39 | +const MAX_FACES = 8; |
| 40 | + |
| 41 | +const ActiveCallEvent = forwardRef<any, ActiveCallEventProps>(({ mxEvent, call }, ref) => { |
| 42 | + const connectionState = useConnectionState(call); |
| 43 | + const participants = useParticipants(call); |
| 44 | + |
| 45 | + const connect = useCallback((ev: ButtonEvent) => { |
| 46 | + ev.preventDefault(); |
| 47 | + defaultDispatcher.dispatch<ViewRoomPayload>({ |
| 48 | + action: Action.ViewRoom, |
| 49 | + room_id: mxEvent.getRoomId()!, |
| 50 | + view_call: true, |
| 51 | + metricsTrigger: undefined, |
| 52 | + }); |
| 53 | + }, [mxEvent]); |
| 54 | + |
| 55 | + const disconnect = useCallback((ev: ButtonEvent) => { |
| 56 | + ev.preventDefault(); |
| 57 | + call.disconnect(); |
| 58 | + }, [call]); |
| 59 | + |
| 60 | + const [buttonText, buttonKind, buttonDisabled, onButtonClick] = useMemo(() => { |
| 61 | + switch (connectionState) { |
| 62 | + case ConnectionState.Disconnected: return [_t("Join"), "primary", false, connect]; |
| 63 | + case ConnectionState.Connecting: return [_t("Join"), "primary", true, connect]; |
| 64 | + case ConnectionState.Connected: return [_t("Leave"), "danger", false, disconnect]; |
| 65 | + case ConnectionState.Disconnecting: return [_t("Leave"), "danger", true, disconnect]; |
| 66 | + } |
| 67 | + }, [connectionState, connect, disconnect]); |
| 68 | + |
| 69 | + const [now, setNow] = useState(() => Date.now()); |
| 70 | + const duration = now - mxEvent.getTs(); |
| 71 | + |
| 72 | + useEffect(() => { |
| 73 | + const timer = setInterval(() => setNow(Date.now()), 1000); |
| 74 | + return () => clearInterval(timer); |
| 75 | + }, []); |
| 76 | + |
| 77 | + const facePileMembers = useMemo(() => [...participants].slice(0, MAX_FACES), [participants]); |
| 78 | + const facePileOverflow = participants.size > facePileMembers.length; |
| 79 | + |
| 80 | + const senderName = mxEvent.sender?.name ?? mxEvent.getSender(); |
| 81 | + |
| 82 | + return <div className="mx_CallEvent_wrapper" ref={ref}> |
| 83 | + <div className="mx_CallEvent mx_CallEvent_active"> |
| 84 | + <MemberAvatar |
| 85 | + member={mxEvent.sender} |
| 86 | + fallbackUserId={mxEvent.getSender()} |
| 87 | + viewUserOnClick |
| 88 | + width={24} |
| 89 | + height={24} |
| 90 | + /> |
| 91 | + <div className="mx_CallEvent_infoRows"> |
| 92 | + <span className="mx_CallEvent_title"> |
| 93 | + { _t("%(name)s started a video call", { name: senderName }) } |
| 94 | + </span> |
| 95 | + <LiveContentSummary |
| 96 | + type={LiveContentType.Video} |
| 97 | + text={_t("Video call")} |
| 98 | + active={false} |
| 99 | + participantCount={participants.size} |
| 100 | + /> |
| 101 | + <FacePile members={facePileMembers} faceSize={24} overflow={facePileOverflow} /> |
| 102 | + </div> |
| 103 | + { /* Clock desync could lead to a negative duration, so just hide it if that happens */ } |
| 104 | + { duration > 0 && <div className="mx_CallEvent_duration"> |
| 105 | + { formatCallTime(new Date(duration)) } |
| 106 | + </div> } |
| 107 | + <AccessibleButton |
| 108 | + className="mx_CallEvent_button" |
| 109 | + kind={buttonKind} |
| 110 | + disabled={buttonDisabled} |
| 111 | + onClick={onButtonClick} |
| 112 | + > |
| 113 | + { buttonText } |
| 114 | + </AccessibleButton> |
| 115 | + </div> |
| 116 | + </div>; |
| 117 | +}); |
| 118 | + |
| 119 | +interface CallEventProps { |
| 120 | + mxEvent: MatrixEvent; |
| 121 | +} |
| 122 | + |
| 123 | +/** |
| 124 | + * An event tile representing an active or historical Element call. |
| 125 | + */ |
| 126 | +export const CallEvent = forwardRef<any, CallEventProps>(({ mxEvent }, ref) => { |
| 127 | + const client = useContext(MatrixClientContext); |
| 128 | + const call = useCall(mxEvent.getRoomId()!); |
| 129 | + const latestEvent = client.getRoom(mxEvent.getRoomId())!.currentState |
| 130 | + .getStateEvents(mxEvent.getType(), mxEvent.getStateKey()!); |
| 131 | + |
| 132 | + if ("m.terminated" in latestEvent.getContent()) { |
| 133 | + const duration = latestEvent.getTs() - mxEvent.getTs(); |
| 134 | + |
| 135 | + // The call is terminated |
| 136 | + return <div className="mx_CallEvent_wrapper" ref={ref}> |
| 137 | + <div className="mx_CallEvent mx_CallEvent_inactive"> |
| 138 | + <span className="mx_CallEvent_title">{ _t("Video call ended") }</span> |
| 139 | + { /* Clock desync could lead to a negative duration, so just hide it if that happens */ } |
| 140 | + { duration > 0 && <div className="mx_CallEvent_duration"> |
| 141 | + { formatCallTime(new Date(duration)) } |
| 142 | + </div> } |
| 143 | + </div> |
| 144 | + </div>; |
| 145 | + } |
| 146 | + |
| 147 | + if (call === null) { |
| 148 | + const senderName = mxEvent.sender?.name ?? mxEvent.getSender(); |
| 149 | + |
| 150 | + // There should be a call, but it hasn't loaded yet |
| 151 | + return <div className="mx_CallEvent_wrapper" ref={ref}> |
| 152 | + <div className="mx_CallEvent mx_CallEvent_active"> |
| 153 | + <MemberAvatar |
| 154 | + member={mxEvent.sender} |
| 155 | + fallbackUserId={mxEvent.getSender()} |
| 156 | + viewUserOnClick |
| 157 | + width={24} |
| 158 | + height={24} |
| 159 | + /> |
| 160 | + <div className="mx_CallEvent_infoRows"> |
| 161 | + <span className="mx_CallEvent_title"> |
| 162 | + { _t("%(name)s started a video call", { name: senderName }) } |
| 163 | + </span> |
| 164 | + <LiveContentSummary |
| 165 | + type={LiveContentType.Video} |
| 166 | + text={_t("Video call")} |
| 167 | + active={false} |
| 168 | + participantCount={0} |
| 169 | + /> |
| 170 | + </div> |
| 171 | + <AccessibleButton |
| 172 | + className="mx_CallEvent_button" |
| 173 | + kind="primary" |
| 174 | + disabled={true} |
| 175 | + onClick={() => {}} |
| 176 | + > |
| 177 | + { _t("Join") } |
| 178 | + </AccessibleButton> |
| 179 | + </div> |
| 180 | + </div>; |
| 181 | + } |
| 182 | + |
| 183 | + return <ActiveCallEvent mxEvent={mxEvent} call={call} ref={ref} />; |
| 184 | +}); |
0 commit comments