simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 Savoir-faire Linux Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Affero General Public License as |
| 6 | * published by the Free Software Foundation; either version 3 of the |
| 7 | * License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Affero General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Affero General Public |
| 15 | * License along with this program. If not, see |
| 16 | * <https://www.gnu.org/licenses/>. |
| 17 | */ |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 18 | import { CallBegin, ConversationInfos, WebSocketMessageType } from 'jami-web-common'; |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 19 | import { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react'; |
Michelle Sepkap Sime | c050d9c | 2022-12-05 09:39:34 -0500 | [diff] [blame] | 20 | import { useTranslation } from 'react-i18next'; |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 21 | import { useNavigate } from 'react-router-dom'; |
| 22 | |
simon | daae910 | 2022-12-02 16:51:31 -0500 | [diff] [blame] | 23 | import { RemoteVideoOverlay } from '../components/VideoOverlay'; |
| 24 | import { useUrlParams } from '../hooks/useUrlParams'; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 25 | import { ConversationMember } from '../models/conversation-member'; |
simon | daae910 | 2022-12-02 16:51:31 -0500 | [diff] [blame] | 26 | import { ConversationRouteParams } from '../router'; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 27 | import { useConversationInfosQuery, useMembersQuery } from '../services/conversationQueries'; |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 28 | import { SetState, WithChildren } from '../utils/utils'; |
Ziwei Wang | 49765ec | 2023-02-13 16:35:32 -0500 | [diff] [blame] | 29 | import { AlertSnackbarContext } from './AlertSnackbarProvider'; |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 30 | import CallProvider, { CallRole } from './CallProvider'; |
idillon | a4b96ab | 2023-02-01 15:30:12 -0500 | [diff] [blame] | 31 | import { useWebSocketContext } from './WebSocketProvider'; |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 32 | |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 33 | export type CallData = { |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 34 | conversationId: string; |
| 35 | role: CallRole; |
| 36 | withVideoOn?: boolean; |
| 37 | }; |
| 38 | |
| 39 | type ICallManagerContext = { |
| 40 | callData: CallData | undefined; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 41 | callConversationInfos: ConversationInfos | undefined; |
| 42 | callMembers: ConversationMember[] | undefined; |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 43 | startCall: SetState<CallData | undefined>; |
| 44 | exitCall: () => void; |
| 45 | }; |
| 46 | |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 47 | const defaultCallManagerContext: ICallManagerContext = { |
| 48 | callData: undefined, |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 49 | callConversationInfos: undefined, |
| 50 | callMembers: undefined, |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 51 | startCall: () => {}, |
| 52 | exitCall: () => {}, |
| 53 | }; |
| 54 | |
| 55 | export const CallManagerContext = createContext<ICallManagerContext>(defaultCallManagerContext); |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 56 | CallManagerContext.displayName = 'CallManagerContext'; |
| 57 | |
| 58 | export default ({ children }: WithChildren) => { |
| 59 | const [callData, setCallData] = useState<CallData>(); |
Ziwei Wang | 49765ec | 2023-02-13 16:35:32 -0500 | [diff] [blame] | 60 | const { setAlertContent } = useContext(AlertSnackbarContext); |
idillon | a4b96ab | 2023-02-01 15:30:12 -0500 | [diff] [blame] | 61 | const webSocket = useWebSocketContext(); |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 62 | const navigate = useNavigate(); |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 63 | const { data: conversationInfos } = useConversationInfosQuery(callData?.conversationId); |
| 64 | const { data: members } = useMembersQuery(callData?.conversationId); |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 65 | const { urlParams } = useUrlParams<ConversationRouteParams>(); |
Michelle Sepkap Sime | c050d9c | 2022-12-05 09:39:34 -0500 | [diff] [blame] | 66 | const { t } = useTranslation(); |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 67 | |
| 68 | const failStartCall = useCallback(() => { |
| 69 | throw new Error('Cannot start call: Already in a call'); |
| 70 | }, []); |
| 71 | |
| 72 | const startCall = !callData ? setCallData : failStartCall; |
| 73 | |
| 74 | const exitCall = useCallback(() => { |
| 75 | if (!callData) { |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | setCallData(undefined); |
| 80 | // TODO: write in chat that the call ended |
| 81 | }, [callData]); |
| 82 | |
| 83 | useEffect(() => { |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 84 | const callBeginListener = ({ conversationId, withVideoOn }: CallBegin) => { |
Michelle Sepkap Sime | c050d9c | 2022-12-05 09:39:34 -0500 | [diff] [blame] | 85 | if (callData) { |
| 86 | // TODO: Currently, we display a notification if already in a call. |
| 87 | // In the future, we should handle receiving a call while already in another. |
Ziwei Wang | 49765ec | 2023-02-13 16:35:32 -0500 | [diff] [blame] | 88 | setAlertContent({ |
| 89 | messageI18nKey: 'missed_incoming_call', |
| 90 | messageI18nContext: { conversationId }, |
| 91 | severity: 'info', |
| 92 | alertOpen: true, |
| 93 | }); |
Michelle Sepkap Sime | c050d9c | 2022-12-05 09:39:34 -0500 | [diff] [blame] | 94 | return; |
| 95 | } |
| 96 | |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 97 | startCall({ conversationId: conversationId, role: 'receiver', withVideoOn }); |
| 98 | navigate(`/conversation/${conversationId}`); |
| 99 | }; |
| 100 | |
| 101 | webSocket.bind(WebSocketMessageType.CallBegin, callBeginListener); |
| 102 | |
| 103 | return () => { |
| 104 | webSocket.unbind(WebSocketMessageType.CallBegin, callBeginListener); |
| 105 | }; |
Ziwei Wang | 49765ec | 2023-02-13 16:35:32 -0500 | [diff] [blame] | 106 | }, [webSocket, navigate, startCall, callData, setAlertContent, t]); |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 107 | |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 108 | const value = useMemo( |
| 109 | () => ({ |
| 110 | startCall, |
| 111 | callData, |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 112 | callConversationInfos: conversationInfos, |
| 113 | callMembers: members, |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 114 | exitCall, |
| 115 | }), |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 116 | [startCall, callData, conversationInfos, members, exitCall] |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 117 | ); |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 118 | |
| 119 | return ( |
Michelle Sepkap Sime | c050d9c | 2022-12-05 09:39:34 -0500 | [diff] [blame] | 120 | <> |
Michelle Sepkap Sime | c050d9c | 2022-12-05 09:39:34 -0500 | [diff] [blame] | 121 | <CallManagerContext.Provider value={value}> |
idillon | cf42c65 | 2023-01-31 18:56:17 -0500 | [diff] [blame] | 122 | <CallProvider> |
| 123 | {callData && callData.conversationId !== urlParams.conversationId && ( |
| 124 | <RemoteVideoOverlay callConversationId={callData.conversationId} /> |
| 125 | )} |
| 126 | {children} |
| 127 | </CallProvider> |
Michelle Sepkap Sime | c050d9c | 2022-12-05 09:39:34 -0500 | [diff] [blame] | 128 | </CallManagerContext.Provider> |
| 129 | </> |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 130 | ); |
| 131 | }; |