|
| 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, { useCallback, useEffect } from 'react'; |
| 18 | +import { MatrixEvent } from "matrix-js-sdk/src/models/event"; |
| 19 | + |
| 20 | +import { _t } from '../languageHandler'; |
| 21 | +import RoomAvatar from '../components/views/avatars/RoomAvatar'; |
| 22 | +import AccessibleButton from '../components/views/elements/AccessibleButton'; |
| 23 | +import { MatrixClientPeg } from "../MatrixClientPeg"; |
| 24 | +import defaultDispatcher from "../dispatcher/dispatcher"; |
| 25 | +import { ViewRoomPayload } from "../dispatcher/payloads/ViewRoomPayload"; |
| 26 | +import { Action } from "../dispatcher/actions"; |
| 27 | +import ToastStore from "../stores/ToastStore"; |
| 28 | +import AccessibleTooltipButton from "../components/views/elements/AccessibleTooltipButton"; |
| 29 | +import { |
| 30 | + LiveContentSummary, |
| 31 | + LiveContentSummaryWithCall, |
| 32 | + LiveContentType, |
| 33 | +} from "../components/views/rooms/LiveContentSummary"; |
| 34 | +import { useCall } from "../hooks/useCall"; |
| 35 | +import { useRoomState } from "../hooks/useRoomState"; |
| 36 | +import { ButtonEvent } from "../components/views/elements/AccessibleButton"; |
| 37 | + |
| 38 | +export const getIncomingCallToastKey = (stateKey: string) => `call_${stateKey}`; |
| 39 | + |
| 40 | +interface Props { |
| 41 | + callEvent: MatrixEvent; |
| 42 | +} |
| 43 | + |
| 44 | +export function IncomingCallToast({ callEvent }: Props) { |
| 45 | + const roomId = callEvent.getRoomId()!; |
| 46 | + const room = MatrixClientPeg.get().getRoom(roomId); |
| 47 | + const call = useCall(roomId); |
| 48 | + |
| 49 | + const dismissToast = useCallback((): void => { |
| 50 | + ToastStore.sharedInstance().dismissToast(getIncomingCallToastKey(callEvent.getStateKey()!)); |
| 51 | + }, [callEvent]); |
| 52 | + |
| 53 | + const latestEvent = useRoomState(room, useCallback((state) => { |
| 54 | + return state.getStateEvents(callEvent.getType(), callEvent.getStateKey()!); |
| 55 | + }, [callEvent])); |
| 56 | + |
| 57 | + useEffect(() => { |
| 58 | + if ("m.terminated" in latestEvent.getContent()) { |
| 59 | + dismissToast(); |
| 60 | + } |
| 61 | + }, [latestEvent, dismissToast]); |
| 62 | + |
| 63 | + const onJoinClick = useCallback((e: ButtonEvent): void => { |
| 64 | + e.stopPropagation(); |
| 65 | + |
| 66 | + defaultDispatcher.dispatch<ViewRoomPayload>({ |
| 67 | + action: Action.ViewRoom, |
| 68 | + room_id: room.roomId, |
| 69 | + view_call: true, |
| 70 | + metricsTrigger: undefined, |
| 71 | + }); |
| 72 | + dismissToast(); |
| 73 | + }, [room, dismissToast]); |
| 74 | + |
| 75 | + const onCloseClick = useCallback((e: ButtonEvent): void => { |
| 76 | + e.stopPropagation(); |
| 77 | + |
| 78 | + dismissToast(); |
| 79 | + }, [dismissToast]); |
| 80 | + |
| 81 | + return <React.Fragment> |
| 82 | + <RoomAvatar |
| 83 | + room={room ?? undefined} |
| 84 | + height={24} |
| 85 | + width={24} |
| 86 | + /> |
| 87 | + <div className="mx_IncomingCallToast_content"> |
| 88 | + <div className="mx_IncomingCallToast_info"> |
| 89 | + <span className="mx_IncomingCallToast_room"> |
| 90 | + { room ? room.name : _t("Unknown room") } |
| 91 | + </span> |
| 92 | + <div className="mx_IncomingCallToast_message"> |
| 93 | + { _t("Video call started") } |
| 94 | + </div> |
| 95 | + { call |
| 96 | + ? <LiveContentSummaryWithCall call={call} /> |
| 97 | + : <LiveContentSummary |
| 98 | + type={LiveContentType.Video} |
| 99 | + text={_t("Video")} |
| 100 | + active={false} |
| 101 | + participantCount={0} |
| 102 | + /> |
| 103 | + } |
| 104 | + </div> |
| 105 | + <AccessibleButton |
| 106 | + className="mx_IncomingCallToast_joinButton" |
| 107 | + onClick={onJoinClick} |
| 108 | + kind="primary" |
| 109 | + > |
| 110 | + { _t("Join") } |
| 111 | + </AccessibleButton> |
| 112 | + </div> |
| 113 | + <AccessibleTooltipButton |
| 114 | + className="mx_IncomingCallToast_closeButton" |
| 115 | + onClick={onCloseClick} |
| 116 | + title={_t("Close")} |
| 117 | + /> |
| 118 | + </React.Fragment>; |
| 119 | +} |
0 commit comments