blob: b9fbb0bf9994f1d2df77a57c7fb9bf15c6a10419 [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 */
idillon07d31cc2022-12-06 22:40:14 -050018import { CallBegin, ConversationInfos, 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
simondaae9102022-12-02 16:51:31 -050023import { RemoteVideoOverlay } from '../components/VideoOverlay';
24import { useUrlParams } from '../hooks/useUrlParams';
idillon07d31cc2022-12-06 22:40:14 -050025import { ConversationMember } from '../models/conversation-member';
simondaae9102022-12-02 16:51:31 -050026import { ConversationRouteParams } from '../router';
idillon07d31cc2022-12-06 22:40:14 -050027import { useConversationInfosQuery, useMembersQuery } from '../services/conversationQueries';
simone35acc22022-12-02 16:51:12 -050028import { SetState, WithChildren } from '../utils/utils';
Ziwei Wang49765ec2023-02-13 16:35:32 -050029import { AlertSnackbarContext } from './AlertSnackbarProvider';
simone35acc22022-12-02 16:51:12 -050030import 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;
idillon07d31cc2022-12-06 22:40:14 -050042 callConversationInfos: ConversationInfos | undefined;
43 callMembers: ConversationMember[] | undefined;
simone35acc22022-12-02 16:51:12 -050044 startCall: SetState<CallData | undefined>;
45 exitCall: () => void;
46};
47
simon5c677962022-12-02 16:51:54 -050048const defaultCallManagerContext: ICallManagerContext = {
49 callData: undefined,
idillon07d31cc2022-12-06 22:40:14 -050050 callConversationInfos: undefined,
51 callMembers: undefined,
simon5c677962022-12-02 16:51:54 -050052 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);
Ziwei Wang49765ec2023-02-13 16:35:32 -050062 const { setAlertContent } = useContext(AlertSnackbarContext);
simone35acc22022-12-02 16:51:12 -050063 const navigate = useNavigate();
idillon07d31cc2022-12-06 22:40:14 -050064 const { data: conversationInfos } = useConversationInfosQuery(callData?.conversationId);
65 const { data: members } = useMembersQuery(callData?.conversationId);
simon5c677962022-12-02 16:51:54 -050066 const { urlParams } = useUrlParams<ConversationRouteParams>();
Michelle Sepkap Simec050d9c2022-12-05 09:39:34 -050067 const { t } = useTranslation();
simone35acc22022-12-02 16:51:12 -050068
69 const failStartCall = useCallback(() => {
70 throw new Error('Cannot start call: Already in a call');
71 }, []);
72
73 const startCall = !callData ? setCallData : failStartCall;
74
75 const exitCall = useCallback(() => {
76 if (!callData) {
77 return;
78 }
79
80 setCallData(undefined);
81 // TODO: write in chat that the call ended
82 }, [callData]);
83
84 useEffect(() => {
simone35acc22022-12-02 16:51:12 -050085 if (!webSocket) {
86 return;
87 }
88
89 const callBeginListener = ({ conversationId, withVideoOn }: CallBegin) => {
Michelle Sepkap Simec050d9c2022-12-05 09:39:34 -050090 if (callData) {
91 // TODO: Currently, we display a notification if already in a call.
92 // In the future, we should handle receiving a call while already in another.
Ziwei Wang49765ec2023-02-13 16:35:32 -050093 setAlertContent({
94 messageI18nKey: 'missed_incoming_call',
95 messageI18nContext: { conversationId },
96 severity: 'info',
97 alertOpen: true,
98 });
Michelle Sepkap Simec050d9c2022-12-05 09:39:34 -050099 return;
100 }
101
simone35acc22022-12-02 16:51:12 -0500102 startCall({ conversationId: conversationId, role: 'receiver', withVideoOn });
103 navigate(`/conversation/${conversationId}`);
104 };
105
106 webSocket.bind(WebSocketMessageType.CallBegin, callBeginListener);
107
108 return () => {
109 webSocket.unbind(WebSocketMessageType.CallBegin, callBeginListener);
110 };
Ziwei Wang49765ec2023-02-13 16:35:32 -0500111 }, [webSocket, navigate, startCall, callData, setAlertContent, t]);
simone35acc22022-12-02 16:51:12 -0500112
simon5c677962022-12-02 16:51:54 -0500113 const value = useMemo(
114 () => ({
115 startCall,
116 callData,
idillon07d31cc2022-12-06 22:40:14 -0500117 callConversationInfos: conversationInfos,
118 callMembers: members,
simon5c677962022-12-02 16:51:54 -0500119 exitCall,
120 }),
idillon07d31cc2022-12-06 22:40:14 -0500121 [startCall, callData, conversationInfos, members, exitCall]
simone35acc22022-12-02 16:51:12 -0500122 );
simone35acc22022-12-02 16:51:12 -0500123
124 return (
Michelle Sepkap Simec050d9c2022-12-05 09:39:34 -0500125 <>
Michelle Sepkap Simec050d9c2022-12-05 09:39:34 -0500126 <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};