blob: 4154c334b62fa6c3cff483ad24838cab3a3a43b5 [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';
23import { WebSocketContext } from '../contexts/WebSocketProvider';
24import { WithChildren } from '../utils/utils';
25
26/**
27 * Binds notification listeners to the WebSocket from a WebSocketContext
28 */
29export default ({ children }: WithChildren) => {
30 const webSocket = useContext(WebSocketContext);
31 const navigate = useNavigate();
32 const { axiosInstance } = useAuthContext();
33
34 useEffect(() => {
35 if (!webSocket) {
36 return;
37 }
38
Misha Krieger-Raynauld20cf1c82022-11-23 20:26:50 -050039 const callBeginListener = (data: CallAction) => {
simonf929a362022-11-18 16:53:45 -050040 console.info('Received event on CallBegin', data);
Misha Krieger-Raynauld20cf1c82022-11-23 20:26:50 -050041 navigate(`/conversation/${data.conversationId}/call?role=receiver`);
simonf929a362022-11-18 16:53:45 -050042 };
43
44 webSocket.bind(WebSocketMessageType.CallBegin, callBeginListener);
45
46 return () => {
47 webSocket.unbind(WebSocketMessageType.CallBegin, callBeginListener);
48 };
49 }, [webSocket, navigate, axiosInstance]);
50
51 return <>{children}</>;
52};