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 | */ |
| 18 | import { AccountTextMessage, CallBegin, WebSocketMessageType } from 'jami-web-common'; |
| 19 | import { useContext, useEffect } from 'react'; |
| 20 | import { useNavigate } from 'react-router-dom'; |
| 21 | |
| 22 | import { useAuthContext } from '../contexts/AuthProvider'; |
| 23 | import { WebSocketContext } from '../contexts/WebSocketProvider'; |
| 24 | import { WithChildren } from '../utils/utils'; |
| 25 | |
| 26 | /** |
| 27 | * Binds notification listeners to the WebSocket from a WebSocketContext |
| 28 | */ |
| 29 | export 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 | |
| 39 | const callBeginListener = (data: AccountTextMessage<CallBegin>) => { |
| 40 | console.info('Received event on CallBegin', data); |
| 41 | navigate(`/conversation/${data.message.conversationId}/call?role=receiver`); |
| 42 | }; |
| 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 | }; |