blob: 33b3055271f358df96eca97a2fa030c0667b9a6f [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';
simone35acc22022-12-02 16:51:12 -050031import { WebSocketContext } from './WebSocketProvider';
32
simon5c677962022-12-02 16:51:54 -050033export type CallData = {
simone35acc22022-12-02 16:51:12 -050034 conversationId: string;
35 role: CallRole;
36 withVideoOn?: boolean;
37};
38
39type ICallManagerContext = {
40 callData: CallData | undefined;
idillon07d31cc2022-12-06 22:40:14 -050041 callConversationInfos: ConversationInfos | undefined;
42 callMembers: ConversationMember[] | undefined;
simone35acc22022-12-02 16:51:12 -050043 startCall: SetState<CallData | undefined>;
44 exitCall: () => void;
45};
46
simon5c677962022-12-02 16:51:54 -050047const defaultCallManagerContext: ICallManagerContext = {
48 callData: undefined,
idillon07d31cc2022-12-06 22:40:14 -050049 callConversationInfos: undefined,
50 callMembers: undefined,
simon5c677962022-12-02 16:51:54 -050051 startCall: () => {},
52 exitCall: () => {},
53};
54
55export const CallManagerContext = createContext<ICallManagerContext>(defaultCallManagerContext);
simone35acc22022-12-02 16:51:12 -050056CallManagerContext.displayName = 'CallManagerContext';
57
58export default ({ children }: WithChildren) => {
59 const [callData, setCallData] = useState<CallData>();
60 const webSocket = useContext(WebSocketContext);
Ziwei Wang49765ec2023-02-13 16:35:32 -050061 const { setAlertContent } = useContext(AlertSnackbarContext);
simone35acc22022-12-02 16:51:12 -050062 const navigate = useNavigate();
idillon07d31cc2022-12-06 22:40:14 -050063 const { data: conversationInfos } = useConversationInfosQuery(callData?.conversationId);
64 const { data: members } = useMembersQuery(callData?.conversationId);
simon5c677962022-12-02 16:51:54 -050065 const { urlParams } = useUrlParams<ConversationRouteParams>();
Michelle Sepkap Simec050d9c2022-12-05 09:39:34 -050066 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.
Ziwei Wang49765ec2023-02-13 16:35:32 -050092 setAlertContent({
93 messageI18nKey: 'missed_incoming_call',
94 messageI18nContext: { conversationId },
95 severity: 'info',
96 alertOpen: true,
97 });
Michelle Sepkap Simec050d9c2022-12-05 09:39:34 -050098 return;
99 }
100
simone35acc22022-12-02 16:51:12 -0500101 startCall({ conversationId: conversationId, role: 'receiver', withVideoOn });
102 navigate(`/conversation/${conversationId}`);
103 };
104
105 webSocket.bind(WebSocketMessageType.CallBegin, callBeginListener);
106
107 return () => {
108 webSocket.unbind(WebSocketMessageType.CallBegin, callBeginListener);
109 };
Ziwei Wang49765ec2023-02-13 16:35:32 -0500110 }, [webSocket, navigate, startCall, callData, setAlertContent, t]);
simone35acc22022-12-02 16:51:12 -0500111
simon5c677962022-12-02 16:51:54 -0500112 const value = useMemo(
113 () => ({
114 startCall,
115 callData,
idillon07d31cc2022-12-06 22:40:14 -0500116 callConversationInfos: conversationInfos,
117 callMembers: members,
simon5c677962022-12-02 16:51:54 -0500118 exitCall,
119 }),
idillon07d31cc2022-12-06 22:40:14 -0500120 [startCall, callData, conversationInfos, members, exitCall]
simone35acc22022-12-02 16:51:12 -0500121 );
simone35acc22022-12-02 16:51:12 -0500122
123 return (
Michelle Sepkap Simec050d9c2022-12-05 09:39:34 -0500124 <>
Michelle Sepkap Simec050d9c2022-12-05 09:39:34 -0500125 <CallManagerContext.Provider value={value}>
idilloncf42c652023-01-31 18:56:17 -0500126 <CallProvider>
127 {callData && callData.conversationId !== urlParams.conversationId && (
128 <RemoteVideoOverlay callConversationId={callData.conversationId} />
129 )}
130 {children}
131 </CallProvider>
Michelle Sepkap Simec050d9c2022-12-05 09:39:34 -0500132 </CallManagerContext.Provider>
133 </>
simone35acc22022-12-02 16:51:12 -0500134 );
135};