blob: fba24dcdf1bcb4a339a3d12d1e16fba2abd59076 [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 */
idillon427c7ef2023-01-11 12:25:55 -050018import { ConversationMessage, WebSocketMessageType } from 'jami-web-common';
19import { createContext, ReactNode, useContext, useEffect, useMemo } from 'react';
simon21f7d9f2022-11-28 14:21:54 -050020
idillon427c7ef2023-01-11 12:25:55 -050021import { 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();
simon21f7d9f2022-11-28 14:21:54 -050036
37 useEffect(() => {
38 if (!webSocket) {
39 return;
40 }
41
42 const conversationMessageListener = (_data: ConversationMessage) => {
idillon07d31cc2022-12-06 22:40:14 -050043 refreshConversationsSummaries();
simon21f7d9f2022-11-28 14:21:54 -050044 };
45
46 webSocket.bind(WebSocketMessageType.ConversationMessage, conversationMessageListener);
47
48 return () => {
49 webSocket.unbind(WebSocketMessageType.ConversationMessage, conversationMessageListener);
50 };
idillon07d31cc2022-12-06 22:40:14 -050051 }, [refreshConversationsSummaries, webSocket]);
simon21f7d9f2022-11-28 14:21:54 -050052
idillon427c7ef2023-01-11 12:25:55 -050053 const value = useMemo<IMessengerContext>(() => ({}), []);
simon5c677962022-12-02 16:51:54 -050054
55 return <MessengerContext.Provider value={value}>{children}</MessengerContext.Provider>;
simon21f7d9f2022-11-28 14:21:54 -050056};