blob: c04010f406eda2bdab5b70c4f0e5c1f1a1210a73 [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';
simon5c677962022-12-02 16:51:54 -050019import { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react';
Michelle Sepkap Simec050d9c2022-12-05 09:39:34 -050020import { useTranslation } from 'react-i18next';
simone35acc22022-12-02 16:51:12 -050021import { useNavigate } from 'react-router-dom';
22
Michelle Sepkap Simec050d9c2022-12-05 09:39:34 -050023import { AlertSnackbar } from '../components/AlertSnackbar';
simondaae9102022-12-02 16:51:31 -050024import { RemoteVideoOverlay } from '../components/VideoOverlay';
25import { useUrlParams } from '../hooks/useUrlParams';
simone35acc22022-12-02 16:51:12 -050026import { Conversation } from '../models/conversation';
simondaae9102022-12-02 16:51:31 -050027import { ConversationRouteParams } from '../router';
simone35acc22022-12-02 16:51:12 -050028import { useConversationQuery } from '../services/conversationQueries';
29import { SetState, WithChildren } from '../utils/utils';
30import CallProvider, { CallRole } from './CallProvider';
31import WebRtcProvider from './WebRtcProvider';
32import { WebSocketContext } from './WebSocketProvider';
33
simon5c677962022-12-02 16:51:54 -050034export type CallData = {
simone35acc22022-12-02 16:51:12 -050035 conversationId: string;
36 role: CallRole;
37 withVideoOn?: boolean;
38};
39
40type ICallManagerContext = {
41 callData: CallData | undefined;
42 callConversation: Conversation | undefined;
43
44 startCall: SetState<CallData | undefined>;
45 exitCall: () => void;
46};
47
simon5c677962022-12-02 16:51:54 -050048const defaultCallManagerContext: ICallManagerContext = {
49 callData: undefined,
50 callConversation: undefined,
51
52 startCall: () => {},
53 exitCall: () => {},
54};
55
56export const CallManagerContext = createContext<ICallManagerContext>(defaultCallManagerContext);
simone35acc22022-12-02 16:51:12 -050057CallManagerContext.displayName = 'CallManagerContext';
58
59export default ({ children }: WithChildren) => {
60 const [callData, setCallData] = useState<CallData>();
61 const webSocket = useContext(WebSocketContext);
62 const navigate = useNavigate();
simon5c677962022-12-02 16:51:54 -050063 const { conversation } = useConversationQuery(callData?.conversationId);
64 const { urlParams } = useUrlParams<ConversationRouteParams>();
Michelle Sepkap Simec050d9c2022-12-05 09:39:34 -050065 const [missedCallConversationId, setMissedCallConversationId] = useState<string>();
66 const { t } = useTranslation();
simone35acc22022-12-02 16:51:12 -050067
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(() => {
simone35acc22022-12-02 16:51:12 -050084 if (!webSocket) {
85 return;
86 }
87
88 const callBeginListener = ({ conversationId, withVideoOn }: CallBegin) => {
Michelle Sepkap Simec050d9c2022-12-05 09:39:34 -050089 if (callData) {
90 // TODO: Currently, we display a notification if already in a call.
91 // In the future, we should handle receiving a call while already in another.
92 setMissedCallConversationId(conversationId);
93 return;
94 }
95
simone35acc22022-12-02 16:51:12 -050096 startCall({ conversationId: conversationId, role: 'receiver', withVideoOn });
97 navigate(`/conversation/${conversationId}`);
98 };
99
100 webSocket.bind(WebSocketMessageType.CallBegin, callBeginListener);
101
102 return () => {
103 webSocket.unbind(WebSocketMessageType.CallBegin, callBeginListener);
104 };
105 }, [webSocket, navigate, startCall, callData]);
106
simon5c677962022-12-02 16:51:54 -0500107 const value = useMemo(
108 () => ({
109 startCall,
110 callData,
111 callConversation: conversation,
112 exitCall,
113 }),
114 [startCall, callData, conversation, exitCall]
simone35acc22022-12-02 16:51:12 -0500115 );
simone35acc22022-12-02 16:51:12 -0500116
117 return (
Michelle Sepkap Simec050d9c2022-12-05 09:39:34 -0500118 <>
119 <AlertSnackbar
120 severity={'info'}
121 open={missedCallConversationId !== undefined}
122 onClose={() => setMissedCallConversationId(undefined)}
123 >
124 {t('missed_incoming_call', { conversationId: missedCallConversationId })}
125 </AlertSnackbar>
126 <CallManagerContext.Provider value={value}>
127 <WebRtcProvider>
128 <CallProvider>
129 {callData && callData.conversationId !== urlParams.conversationId && (
130 <RemoteVideoOverlay callConversationId={callData.conversationId} />
131 )}
132 {children}
133 </CallProvider>
134 </WebRtcProvider>
135 </CallManagerContext.Provider>
136 </>
simone35acc22022-12-02 16:51:12 -0500137 );
138};