blob: 4261f0705c02a20bd64b4b77c285a3c8ff49a71f [file] [log] [blame]
simone35acc22022-12-02 16:51:12 -05001/*
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 */
18import { CallBegin, WebSocketMessageType } from 'jami-web-common';
19import { createContext, useCallback, useContext, useEffect, useState } from 'react';
20import { useNavigate } from 'react-router-dom';
21
22import { Conversation } from '../models/conversation';
23import { useConversationQuery } from '../services/conversationQueries';
24import { SetState, WithChildren } from '../utils/utils';
25import CallProvider, { CallRole } from './CallProvider';
26import WebRtcProvider from './WebRtcProvider';
27import { WebSocketContext } from './WebSocketProvider';
28
29type CallData = {
30 conversationId: string;
31 role: CallRole;
32 withVideoOn?: boolean;
33};
34
35type ICallManagerContext = {
36 callData: CallData | undefined;
37 callConversation: Conversation | undefined;
38
39 startCall: SetState<CallData | undefined>;
40 exitCall: () => void;
41};
42
43export const CallManagerContext = createContext<ICallManagerContext>(undefined!);
44CallManagerContext.displayName = 'CallManagerContext';
45
46export default ({ children }: WithChildren) => {
47 const [callData, setCallData] = useState<CallData>();
48 const webSocket = useContext(WebSocketContext);
49 const navigate = useNavigate();
50 const conversationId = callData?.conversationId;
51 const { conversation } = useConversationQuery(conversationId);
52
53 const failStartCall = useCallback(() => {
54 throw new Error('Cannot start call: Already in a call');
55 }, []);
56
57 const startCall = !callData ? setCallData : failStartCall;
58
59 const exitCall = useCallback(() => {
60 if (!callData) {
61 return;
62 }
63
64 setCallData(undefined);
65 // TODO: write in chat that the call ended
66 }, [callData]);
67
68 useEffect(() => {
69 if (callData) {
70 // TODO: Currently, we simply do not bind the CallBegin listener if already in a call.
71 // In the future, we should handle receiving a call while already in another.
72 return;
73 }
74 if (!webSocket) {
75 return;
76 }
77
78 const callBeginListener = ({ conversationId, withVideoOn }: CallBegin) => {
79 startCall({ conversationId: conversationId, role: 'receiver', withVideoOn });
80 navigate(`/conversation/${conversationId}`);
81 };
82
83 webSocket.bind(WebSocketMessageType.CallBegin, callBeginListener);
84
85 return () => {
86 webSocket.unbind(WebSocketMessageType.CallBegin, callBeginListener);
87 };
88 }, [webSocket, navigate, startCall, callData]);
89
90 return (
91 <CallManagerContext.Provider
92 value={{
93 startCall,
94 callData,
95 callConversation: conversation,
96 exitCall,
97 }}
98 >
99 <CallManagerProvider>{children}</CallManagerProvider>
100 </CallManagerContext.Provider>
101 );
102};
103
104const CallManagerProvider = ({ children }: WithChildren) => {
105 const { callData } = useContext(CallManagerContext);
106
107 if (!callData) {
108 return <>{children}</>;
109 }
110
111 return (
112 <WebRtcProvider>
113 <CallProvider>{children}</CallProvider>
114 </WebRtcProvider>
115 );
116};