blob: dacc3c14913f49b97a83ad36413d4272ad47d85c [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 */
Misha Krieger-Raynauld20cf1c82022-11-23 20:26:50 -050018import { CallAction, 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/**
28 * Binds notification listeners to the WebSocket from a WebSocketContext
29 */
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
Misha Krieger-Raynauld20cf1c82022-11-23 20:26:50 -050040 const callBeginListener = (data: CallAction) => {
simonf929a362022-11-18 16:53:45 -050041 console.info('Received event on CallBegin', data);
simonff1cb352022-11-24 15:15:26 -050042 navigate(`/conversation/${data.conversationId}/call?role=receiver`, {
43 state: {
44 callStatus: CallStatus.Ringing,
45 },
46 });
simonf929a362022-11-18 16:53:45 -050047 };
48
49 webSocket.bind(WebSocketMessageType.CallBegin, callBeginListener);
50
51 return () => {
52 webSocket.unbind(WebSocketMessageType.CallBegin, callBeginListener);
53 };
54 }, [webSocket, navigate, axiosInstance]);
55
56 return <>{children}</>;
57};