blob: fe96468bff913f15e024a0f7bac98d20b62eddce [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';
idillona4b96ab2023-02-01 15:30:12 -050019import { createContext, ReactNode, useEffect, useMemo } from 'react';
simon21f7d9f2022-11-28 14:21:54 -050020
idillon18283ac2023-01-07 12:06:42 -050021import { useAddConversationRequestToCache, useRefreshConversationsSummaries } from '../services/conversationQueries';
idillona4b96ab2023-02-01 15:30:12 -050022import { useWebSocketContext } from './WebSocketProvider';
simon21f7d9f2022-11-28 14:21:54 -050023
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 }) => {
idillona4b96ab2023-02-01 15:30:12 -050033 const webSocket = useWebSocketContext();
simon21f7d9f2022-11-28 14:21:54 -050034
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(() => {
simon21f7d9f2022-11-28 14:21:54 -050039 const conversationMessageListener = (_data: ConversationMessage) => {
idillon07d31cc2022-12-06 22:40:14 -050040 refreshConversationsSummaries();
simon21f7d9f2022-11-28 14:21:54 -050041 };
42
idillon18283ac2023-01-07 12:06:42 -050043 const conversationRequestListener = (conversationRequest: IConversationRequest) => {
44 addConversationRequestToCache(conversationRequest);
45 };
46
simon21f7d9f2022-11-28 14:21:54 -050047 webSocket.bind(WebSocketMessageType.ConversationMessage, conversationMessageListener);
idillon18283ac2023-01-07 12:06:42 -050048 webSocket.bind(WebSocketMessageType.ConversationRequest, conversationRequestListener);
simon21f7d9f2022-11-28 14:21:54 -050049
50 return () => {
51 webSocket.unbind(WebSocketMessageType.ConversationMessage, conversationMessageListener);
idillon18283ac2023-01-07 12:06:42 -050052 webSocket.unbind(WebSocketMessageType.ConversationRequest, conversationRequestListener);
simon21f7d9f2022-11-28 14:21:54 -050053 };
idillon18283ac2023-01-07 12:06:42 -050054 }, [addConversationRequestToCache, refreshConversationsSummaries, webSocket]);
simon21f7d9f2022-11-28 14:21:54 -050055
idillon427c7ef2023-01-11 12:25:55 -050056 const value = useMemo<IMessengerContext>(() => ({}), []);
simon5c677962022-12-02 16:51:54 -050057
58 return <MessengerContext.Provider value={value}>{children}</MessengerContext.Provider>;
simon21f7d9f2022-11-28 14:21:54 -050059};