blob: 907efb57db7370c8faf8f3be704000df26c13542 [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
simonff1cb352022-11-24 15:15:26 -050022import { CallStatus } from '../contexts/CallProvider';
simonf929a362022-11-18 16:53:45 -050023import { WebSocketContext } from '../contexts/WebSocketProvider';
24import { WithChildren } from '../utils/utils';
25
26/**
simon492e8402022-11-29 16:48:37 -050027 * Binds notification listeners to the WebSocket from a WebSocketContext.
simonf929a362022-11-18 16:53:45 -050028 */
29export default ({ children }: WithChildren) => {
30 const webSocket = useContext(WebSocketContext);
31 const navigate = useNavigate();
simonf929a362022-11-18 16:53:45 -050032
33 useEffect(() => {
34 if (!webSocket) {
35 return;
36 }
37
MichelleSS55164202022-11-25 18:36:14 -050038 const callBeginListener = (data: CallBegin) => {
simonf929a362022-11-18 16:53:45 -050039 console.info('Received event on CallBegin', data);
Charlieb837e8f2022-11-28 19:18:46 -050040 navigate(`/conversation/${data.conversationId}/call`, {
simonff1cb352022-11-24 15:15:26 -050041 state: {
Charlieb837e8f2022-11-28 19:18:46 -050042 role: 'receiver',
simonff1cb352022-11-24 15:15:26 -050043 callStatus: CallStatus.Ringing,
MichelleSS55164202022-11-25 18:36:14 -050044 isVideoOn: data.withVideoOn,
simonff1cb352022-11-24 15:15:26 -050045 },
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 };
simon09fe4822022-11-30 23:36:25 -050054 }, [webSocket, navigate]);
simonf929a362022-11-18 16:53:45 -050055
56 return <>{children}</>;
57};