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 | |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 22 | import { CallStatus } from '../contexts/CallProvider'; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 23 | import { WebSocketContext } from '../contexts/WebSocketProvider'; |
| 24 | import { WithChildren } from '../utils/utils'; |
| 25 | |
| 26 | /** |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 27 | * Binds notification listeners to the WebSocket from a WebSocketContext. |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 28 | */ |
| 29 | export default ({ children }: WithChildren) => { |
| 30 | const webSocket = useContext(WebSocketContext); |
| 31 | const navigate = useNavigate(); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 32 | |
| 33 | useEffect(() => { |
| 34 | if (!webSocket) { |
| 35 | return; |
| 36 | } |
| 37 | |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 38 | const callBeginListener = (data: CallBegin) => { |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 39 | console.info('Received event on CallBegin', data); |
Charlie | b837e8f | 2022-11-28 19:18:46 -0500 | [diff] [blame] | 40 | navigate(`/conversation/${data.conversationId}/call`, { |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 41 | state: { |
Charlie | b837e8f | 2022-11-28 19:18:46 -0500 | [diff] [blame] | 42 | role: 'receiver', |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 43 | callStatus: CallStatus.Ringing, |
MichelleSS | 5516420 | 2022-11-25 18:36:14 -0500 | [diff] [blame] | 44 | isVideoOn: data.withVideoOn, |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 45 | }, |
| 46 | }); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | webSocket.bind(WebSocketMessageType.CallBegin, callBeginListener); |
| 50 | |
| 51 | return () => { |
| 52 | webSocket.unbind(WebSocketMessageType.CallBegin, callBeginListener); |
| 53 | }; |
simon | 09fe482 | 2022-11-30 23:36:25 -0500 | [diff] [blame] | 54 | }, [webSocket, navigate]); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 55 | |
| 56 | return <>{children}</>; |
| 57 | }; |