blob: 2f86ad98867d0ed35be764117e47b50aab267273 [file] [log] [blame]
simon21f7d9f2022-11-28 14:21:54 -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 */
idillon18283ac2023-01-07 12:06:42 -050018import { ConversationMessage, IConversationRequest, WebSocketMessageType } from 'jami-web-common';
idillon427c7ef2023-01-11 12:25:55 -050019import { createContext, ReactNode, useContext, useEffect, useMemo } from 'react';
simon21f7d9f2022-11-28 14:21:54 -050020
idillon18283ac2023-01-07 12:06:42 -050021import { useAddConversationRequestToCache, useRefreshConversationsSummaries } from '../services/conversationQueries';
simon21f7d9f2022-11-28 14:21:54 -050022import { WebSocketContext } from './WebSocketProvider';
23
idillon427c7ef2023-01-11 12:25:55 -050024// It is not sure yet we want this context to have no value
25// eslint-disable-next-line @typescript-eslint/no-empty-interface
26export interface IMessengerContext {}
simon21f7d9f2022-11-28 14:21:54 -050027
idillon427c7ef2023-01-11 12:25:55 -050028const defaultMessengerContext: IMessengerContext = {};
simon21f7d9f2022-11-28 14:21:54 -050029
30export const MessengerContext = createContext<IMessengerContext>(defaultMessengerContext);
31
32export default ({ children }: { children: ReactNode }) => {
simon21f7d9f2022-11-28 14:21:54 -050033 const webSocket = useContext(WebSocketContext);
34
idillon07d31cc2022-12-06 22:40:14 -050035 const refreshConversationsSummaries = useRefreshConversationsSummaries();
idillon18283ac2023-01-07 12:06:42 -050036 const addConversationRequestToCache = useAddConversationRequestToCache();
simon21f7d9f2022-11-28 14:21:54 -050037
38 useEffect(() => {
39 if (!webSocket) {
40 return;
41 }
42
43 const conversationMessageListener = (_data: ConversationMessage) => {
idillon07d31cc2022-12-06 22:40:14 -050044 refreshConversationsSummaries();
simon21f7d9f2022-11-28 14:21:54 -050045 };
46
idillon18283ac2023-01-07 12:06:42 -050047 const conversationRequestListener = (conversationRequest: IConversationRequest) => {
48 addConversationRequestToCache(conversationRequest);
49 };
50
simon21f7d9f2022-11-28 14:21:54 -050051 webSocket.bind(WebSocketMessageType.ConversationMessage, conversationMessageListener);
idillon18283ac2023-01-07 12:06:42 -050052 webSocket.bind(WebSocketMessageType.ConversationRequest, conversationRequestListener);
simon21f7d9f2022-11-28 14:21:54 -050053
54 return () => {
55 webSocket.unbind(WebSocketMessageType.ConversationMessage, conversationMessageListener);
idillon18283ac2023-01-07 12:06:42 -050056 webSocket.unbind(WebSocketMessageType.ConversationRequest, conversationRequestListener);
simon21f7d9f2022-11-28 14:21:54 -050057 };
idillon18283ac2023-01-07 12:06:42 -050058 }, [addConversationRequestToCache, refreshConversationsSummaries, webSocket]);
simon21f7d9f2022-11-28 14:21:54 -050059
idillon427c7ef2023-01-11 12:25:55 -050060 const value = useMemo<IMessengerContext>(() => ({}), []);
simon5c677962022-12-02 16:51:54 -050061
62 return <MessengerContext.Provider value={value}>{children}</MessengerContext.Provider>;
simon21f7d9f2022-11-28 14:21:54 -050063};