blob: 63eccc6dcc6ddc27eba156889d0cd73f92b814da [file] [log] [blame]
simonf929a362022-11-18 16:53:45 -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 */
MichelleSS55164202022-11-25 18:36:14 -050018import { CallBegin, WebSocketMessageType } from 'jami-web-common';
simonf929a362022-11-18 16:53:45 -050019import { useContext, useEffect } from 'react';
20import { useNavigate } from 'react-router-dom';
21
22import { useAuthContext } from '../contexts/AuthProvider';
simonff1cb352022-11-24 15:15:26 -050023import { CallStatus } from '../contexts/CallProvider';
simonf929a362022-11-18 16:53:45 -050024import { WebSocketContext } from '../contexts/WebSocketProvider';
25import { WithChildren } from '../utils/utils';
26
27/**
simon492e8402022-11-29 16:48:37 -050028 * Binds notification listeners to the WebSocket from a WebSocketContext.
simonf929a362022-11-18 16:53:45 -050029 */
30export default ({ children }: WithChildren) => {
31 const webSocket = useContext(WebSocketContext);
32 const navigate = useNavigate();
33 const { axiosInstance } = useAuthContext();
34
35 useEffect(() => {
36 if (!webSocket) {
37 return;
38 }
39
MichelleSS55164202022-11-25 18:36:14 -050040 const callBeginListener = (data: CallBegin) => {
simonf929a362022-11-18 16:53:45 -050041 console.info('Received event on CallBegin', data);
Charlieb837e8f2022-11-28 19:18:46 -050042 navigate(`/conversation/${data.conversationId}/call`, {
simonff1cb352022-11-24 15:15:26 -050043 state: {
Charlieb837e8f2022-11-28 19:18:46 -050044 role: 'receiver',
simonff1cb352022-11-24 15:15:26 -050045 callStatus: CallStatus.Ringing,
MichelleSS55164202022-11-25 18:36:14 -050046 isVideoOn: data.withVideoOn,
simonff1cb352022-11-24 15:15:26 -050047 },
48 });
simonf929a362022-11-18 16:53:45 -050049 };
50
51 webSocket.bind(WebSocketMessageType.CallBegin, callBeginListener);
52
53 return () => {
54 webSocket.unbind(WebSocketMessageType.CallBegin, callBeginListener);
55 };
56 }, [webSocket, navigate, axiosInstance]);
57
58 return <>{children}</>;
59};