simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 1 | /* |
| 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 | */ |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 18 | import { CallBegin, WebSocketMessageType } from 'jami-web-common'; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 19 | import { useContext, useEffect } from 'react'; |
| 20 | import { useNavigate } from 'react-router-dom'; |
| 21 | |
| 22 | import { useAuthContext } from '../contexts/AuthProvider'; |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 23 | import { CallStatus } from '../contexts/CallProvider'; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 24 | import { WebSocketContext } from '../contexts/WebSocketProvider'; |
| 25 | import { WithChildren } from '../utils/utils'; |
| 26 | |
| 27 | /** |
| 28 | * Binds notification listeners to the WebSocket from a WebSocketContext |
| 29 | */ |
| 30 | export 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 | |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 40 | const callBeginListener = (data: CallBegin) => { |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 41 | console.info('Received event on CallBegin', data); |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 42 | navigate(`/conversation/${data.conversationId}/call?role=receiver`, { |
| 43 | state: { |
| 44 | callStatus: CallStatus.Ringing, |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 45 | isVideoOn: data.withVideoOn, |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 46 | }, |
| 47 | }); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | webSocket.bind(WebSocketMessageType.CallBegin, callBeginListener); |
| 51 | |
| 52 | return () => { |
| 53 | webSocket.unbind(WebSocketMessageType.CallBegin, callBeginListener); |
| 54 | }; |
| 55 | }, [webSocket, navigate, axiosInstance]); |
| 56 | |
| 57 | return <>{children}</>; |
| 58 | }; |