|
| 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, { FC, forwardRef, useCallback, useContext, useEffect, useMemo, useState } from "react"; |
| 18 | + |
| 19 | +import type { MatrixEvent } from "matrix-js-sdk/src/models/event"; |
| 20 | +import type { RoomMember } from "matrix-js-sdk/src/models/room-member"; |
| 21 | +import { Call, ConnectionState } from "../../../models/Call"; |
| 22 | +import { _t } from "../../../languageHandler"; |
| 23 | +import { useCall, useConnectionState, useParticipants } from "../../../hooks/useCall"; |
| 24 | +import defaultDispatcher from "../../../dispatcher/dispatcher"; |
| 25 | +import type { ViewRoomPayload } from "../../../dispatcher/payloads/ViewRoomPayload"; |
| 26 | +import { Action } from "../../../dispatcher/actions"; |
| 27 | +import type { ButtonEvent } from "../elements/AccessibleButton"; |
| 28 | +import MemberAvatar from "../avatars/MemberAvatar"; |
| 29 | +import { LiveContentSummary, LiveContentType } from "../rooms/LiveContentSummary"; |
| 30 | +import FacePile from "../elements/FacePile"; |
| 31 | +import { formatCallTime } from "../../../DateUtils"; |
| 32 | +import AccessibleButton from "../elements/AccessibleButton"; |
| 33 | +import MatrixClientContext from "../../../contexts/MatrixClientContext"; |
| 34 | + |
| 35 | +interface CallEventDurationProps { |
| 36 | + delta: number; |
| 37 | +} |
| 38 | + |
| 39 | +const CallEventDuration: FC<CallEventDurationProps> = ({ delta }) => { |
| 40 | + // Clock desync could lead to a negative duration, so just hide it if that happens |
| 41 | + if (delta <= 0) return null; |
| 42 | + return <div className="mx_CallEvent_duration">{ formatCallTime(new Date(delta)) }</div>; |
| 43 | +}; |
| 44 | + |
| 45 | +const MAX_FACES = 8; |
| 46 | + |
| 47 | +interface ActiveCallEventProps { |
| 48 | + mxEvent: MatrixEvent; |
| 49 | + participants: Set<RoomMember>; |
| 50 | + buttonText: string; |
| 51 | + buttonKind: string; |
| 52 | + onButtonClick: ((ev: ButtonEvent) => void) | null; |
| 53 | +} |
| 54 | + |
| 55 | +const ActiveCallEvent = forwardRef<any, ActiveCallEventProps>( |
| 56 | + ( |
| 57 | + { |
| 58 | + mxEvent, |
| 59 | + participants, |
| 60 | + buttonText, |
| 61 | + buttonKind, |
| 62 | + onButtonClick, |
| 63 | + }, |
| 64 | + ref, |
| 65 | + ) => { |
| 66 | + const senderName = useMemo(() => mxEvent.sender?.name ?? mxEvent.getSender(), [mxEvent]); |
| 67 | + |
| 68 | + const facePileMembers = useMemo(() => [...participants].slice(0, MAX_FACES), [participants]); |
| 69 | + const facePileOverflow = participants.size > facePileMembers.length; |
| 70 | + |
| 71 | + const [now, setNow] = useState(() => Date.now()); |
| 72 | + useEffect(() => { |
| 73 | + const timer = setInterval(() => setNow(Date.now()), 1000); |
| 74 | + return () => clearInterval(timer); |
| 75 | + }, []); |
| 76 | + |
| 77 | + return <div className="mx_CallEvent_wrapper" ref={ref}> |
| 78 | + <div className="mx_CallEvent mx_CallEvent_active"> |
| 79 | + <MemberAvatar |
| 80 | + member={mxEvent.sender} |
| 81 | + fallbackUserId={mxEvent.getSender()} |
| 82 | + viewUserOnClick |
| 83 | + width={24} |
| 84 | + height={24} |
| 85 | + /> |
| 86 | + <div className="mx_CallEvent_infoRows"> |
| 87 | + <span className="mx_CallEvent_title"> |
| 88 | + { _t("%(name)s started a video call", { name: senderName }) } |
| 89 | + </span> |
| 90 | + <LiveContentSummary |
| 91 | + type={LiveContentType.Video} |
| 92 | + text={_t("Video call")} |
| 93 | + active={false} |
| 94 | + participantCount={participants.size} |
| 95 | + /> |
| 96 | + <FacePile members={facePileMembers} faceSize={24} overflow={facePileOverflow} /> |
| 97 | + </div> |
| 98 | + <CallEventDuration delta={now - mxEvent.getTs()} /> |
| 99 | + <AccessibleButton |
| 100 | + className="mx_CallEvent_button" |
| 101 | + kind={buttonKind} |
| 102 | + disabled={onButtonClick === null} |
| 103 | + onClick={onButtonClick} |
| 104 | + > |
| 105 | + { buttonText } |
| 106 | + </AccessibleButton> |
| 107 | + </div> |
| 108 | + </div>; |
| 109 | + }, |
| 110 | +); |
| 111 | + |
| 112 | +interface ActiveLoadedCallEventProps { |
| 113 | + mxEvent: MatrixEvent; |
| 114 | + call: Call; |
| 115 | +} |
| 116 | + |
| 117 | +const ActiveLoadedCallEvent = forwardRef<any, ActiveLoadedCallEventProps>(({ mxEvent, call }, ref) => { |
| 118 | + const connectionState = useConnectionState(call); |
| 119 | + const participants = useParticipants(call); |
| 120 | + |
| 121 | + const connect = useCallback((ev: ButtonEvent) => { |
| 122 | + ev.preventDefault(); |
| 123 | + defaultDispatcher.dispatch<ViewRoomPayload>({ |
| 124 | + action: Action.ViewRoom, |
| 125 | + room_id: mxEvent.getRoomId()!, |
| 126 | + view_call: true, |
| 127 | + metricsTrigger: undefined, |
| 128 | + }); |
| 129 | + }, [mxEvent]); |
| 130 | + |
| 131 | + const disconnect = useCallback((ev: ButtonEvent) => { |
| 132 | + ev.preventDefault(); |
| 133 | + call.disconnect(); |
| 134 | + }, [call]); |
| 135 | + |
| 136 | + const [buttonText, buttonKind, onButtonClick] = useMemo(() => { |
| 137 | + switch (connectionState) { |
| 138 | + case ConnectionState.Disconnected: return [_t("Join"), "primary", connect]; |
| 139 | + case ConnectionState.Connecting: return [_t("Join"), "primary", null]; |
| 140 | + case ConnectionState.Connected: return [_t("Leave"), "danger", disconnect]; |
| 141 | + case ConnectionState.Disconnecting: return [_t("Leave"), "danger", null]; |
| 142 | + } |
| 143 | + }, [connectionState, connect, disconnect]); |
| 144 | + |
| 145 | + return <ActiveCallEvent |
| 146 | + ref={ref} |
| 147 | + mxEvent={mxEvent} |
| 148 | + participants={participants} |
| 149 | + buttonText={buttonText} |
| 150 | + buttonKind={buttonKind} |
| 151 | + onButtonClick={onButtonClick} |
| 152 | + />; |
| 153 | +}); |
| 154 | + |
| 155 | +interface CallEventProps { |
| 156 | + mxEvent: MatrixEvent; |
| 157 | +} |
| 158 | + |
| 159 | +/** |
| 160 | + * An event tile representing an active or historical Element call. |
| 161 | + */ |
| 162 | +export const CallEvent = forwardRef<any, CallEventProps>(({ mxEvent }, ref) => { |
| 163 | + const noParticipants = useMemo(() => new Set<RoomMember>(), []); |
| 164 | + const client = useContext(MatrixClientContext); |
| 165 | + const call = useCall(mxEvent.getRoomId()!); |
| 166 | + const latestEvent = client.getRoom(mxEvent.getRoomId())!.currentState |
| 167 | + .getStateEvents(mxEvent.getType(), mxEvent.getStateKey()!); |
| 168 | + |
| 169 | + if ("m.terminated" in latestEvent.getContent()) { |
| 170 | + // The call is terminated |
| 171 | + return <div className="mx_CallEvent_wrapper" ref={ref}> |
| 172 | + <div className="mx_CallEvent mx_CallEvent_inactive"> |
| 173 | + <span className="mx_CallEvent_title">{ _t("Video call ended") }</span> |
| 174 | + <CallEventDuration delta={latestEvent.getTs() - mxEvent.getTs()} /> |
| 175 | + </div> |
| 176 | + </div>; |
| 177 | + } |
| 178 | + |
| 179 | + if (call === null) { |
| 180 | + // There should be a call, but it hasn't loaded yet |
| 181 | + return <ActiveCallEvent |
| 182 | + ref={ref} |
| 183 | + mxEvent={mxEvent} |
| 184 | + participants={noParticipants} |
| 185 | + buttonText={_t("Join")} |
| 186 | + buttonKind="primary" |
| 187 | + onButtonClick={null} |
| 188 | + />; |
| 189 | + } |
| 190 | + |
| 191 | + return <ActiveLoadedCallEvent mxEvent={mxEvent} call={call} ref={ref} />; |
| 192 | +}); |
0 commit comments