simon | 21f7d9f | 2022-11-28 14:21:54 -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 | */ |
idillon | 18283ac | 2023-01-07 12:06:42 -0500 | [diff] [blame] | 18 | import { ConversationMessage, IConversationRequest, WebSocketMessageType } from 'jami-web-common'; |
idillon | a4b96ab | 2023-02-01 15:30:12 -0500 | [diff] [blame] | 19 | import { createContext, ReactNode, useEffect, useMemo } from 'react'; |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 20 | |
idillon | 18283ac | 2023-01-07 12:06:42 -0500 | [diff] [blame] | 21 | import { useAddConversationRequestToCache, useRefreshConversationsSummaries } from '../services/conversationQueries'; |
idillon | a4b96ab | 2023-02-01 15:30:12 -0500 | [diff] [blame] | 22 | import { useWebSocketContext } from './WebSocketProvider'; |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 23 | |
idillon | 427c7ef | 2023-01-11 12:25:55 -0500 | [diff] [blame] | 24 | // It is not sure yet we want this context to have no value |
| 25 | // eslint-disable-next-line @typescript-eslint/no-empty-interface |
| 26 | export interface IMessengerContext {} |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 27 | |
idillon | 427c7ef | 2023-01-11 12:25:55 -0500 | [diff] [blame] | 28 | const defaultMessengerContext: IMessengerContext = {}; |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 29 | |
| 30 | export const MessengerContext = createContext<IMessengerContext>(defaultMessengerContext); |
| 31 | |
| 32 | export default ({ children }: { children: ReactNode }) => { |
idillon | a4b96ab | 2023-02-01 15:30:12 -0500 | [diff] [blame] | 33 | const webSocket = useWebSocketContext(); |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 34 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 35 | const refreshConversationsSummaries = useRefreshConversationsSummaries(); |
idillon | 18283ac | 2023-01-07 12:06:42 -0500 | [diff] [blame] | 36 | const addConversationRequestToCache = useAddConversationRequestToCache(); |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 37 | |
| 38 | useEffect(() => { |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 39 | const conversationMessageListener = (_data: ConversationMessage) => { |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 40 | refreshConversationsSummaries(); |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 41 | }; |
| 42 | |
idillon | 18283ac | 2023-01-07 12:06:42 -0500 | [diff] [blame] | 43 | const conversationRequestListener = (conversationRequest: IConversationRequest) => { |
| 44 | addConversationRequestToCache(conversationRequest); |
| 45 | }; |
| 46 | |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 47 | webSocket.bind(WebSocketMessageType.ConversationMessage, conversationMessageListener); |
idillon | 18283ac | 2023-01-07 12:06:42 -0500 | [diff] [blame] | 48 | webSocket.bind(WebSocketMessageType.ConversationRequest, conversationRequestListener); |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 49 | |
| 50 | return () => { |
| 51 | webSocket.unbind(WebSocketMessageType.ConversationMessage, conversationMessageListener); |
idillon | 18283ac | 2023-01-07 12:06:42 -0500 | [diff] [blame] | 52 | webSocket.unbind(WebSocketMessageType.ConversationRequest, conversationRequestListener); |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 53 | }; |
idillon | 18283ac | 2023-01-07 12:06:42 -0500 | [diff] [blame] | 54 | }, [addConversationRequestToCache, refreshConversationsSummaries, webSocket]); |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 55 | |
idillon | 427c7ef | 2023-01-11 12:25:55 -0500 | [diff] [blame] | 56 | const value = useMemo<IMessengerContext>(() => ({}), []); |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 57 | |
| 58 | return <MessengerContext.Provider value={value}>{children}</MessengerContext.Provider>; |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 59 | }; |