blob: ec68765168d136ecbef89b2d76db8f0488105530 [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';
idillona4b96ab2023-02-01 15:30:12 -050031import { useWebSocketContext } from './WebSocketProvider';
simone35acc22022-12-02 16:51:12 -050032
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>();
Ziwei Wang49765ec2023-02-13 16:35:32 -050060 const { setAlertContent } = useContext(AlertSnackbarContext);
idillona4b96ab2023-02-01 15:30:12 -050061 const webSocket = useWebSocketContext();
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 const callBeginListener = ({ conversationId, withVideoOn }: CallBegin) => {
Michelle Sepkap Simec050d9c2022-12-05 09:39:34 -050085 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 Wang49765ec2023-02-13 16:35:32 -050088 setAlertContent({
89 messageI18nKey: 'missed_incoming_call',
90 messageI18nContext: { conversationId },
91 severity: 'info',
92 alertOpen: true,
93 });
Michelle Sepkap Simec050d9c2022-12-05 09:39:34 -050094 return;
95 }
96
simone35acc22022-12-02 16:51:12 -050097 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 Wang49765ec2023-02-13 16:35:32 -0500106 }, [webSocket, navigate, startCall, callData, setAlertContent, t]);
simone35acc22022-12-02 16:51:12 -0500107
simon5c677962022-12-02 16:51:54 -0500108 const value = useMemo(
109 () => ({
110 startCall,
111 callData,
idillon07d31cc2022-12-06 22:40:14 -0500112 callConversationInfos: conversationInfos,
113 callMembers: members,
simon5c677962022-12-02 16:51:54 -0500114 exitCall,
115 }),
idillon07d31cc2022-12-06 22:40:14 -0500116 [startCall, callData, conversationInfos, members, exitCall]
simone35acc22022-12-02 16:51:12 -0500117 );
simone35acc22022-12-02 16:51:12 -0500118
119 return (
Michelle Sepkap Simec050d9c2022-12-05 09:39:34 -0500120 <>
Michelle Sepkap Simec050d9c2022-12-05 09:39:34 -0500121 <CallManagerContext.Provider value={value}>
idilloncf42c652023-01-31 18:56:17 -0500122 <CallProvider>
123 {callData && callData.conversationId !== urlParams.conversationId && (
124 <RemoteVideoOverlay callConversationId={callData.conversationId} />
125 )}
126 {children}
127 </CallProvider>
Michelle Sepkap Simec050d9c2022-12-05 09:39:34 -0500128 </CallManagerContext.Provider>
129 </>
simone35acc22022-12-02 16:51:12 -0500130 );
131};