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 | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 18 | import { ConversationMessage, IConversationSummary, LookupResult, WebSocketMessageType } from 'jami-web-common'; |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 19 | import { createContext, ReactNode, useContext, useEffect, useMemo, useState } from 'react'; |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 20 | |
Misha Krieger-Raynauld | cfa4430 | 2022-11-30 18:36:36 -0500 | [diff] [blame] | 21 | import { Contact } from '../models/contact'; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 22 | import { useConversationsSummariesQuery, useRefreshConversationsSummaries } from '../services/conversationQueries'; |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 23 | import { SetState } from '../utils/utils'; |
| 24 | import { useAuthContext } from './AuthProvider'; |
| 25 | import { WebSocketContext } from './WebSocketProvider'; |
| 26 | |
| 27 | export interface IMessengerContext { |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 28 | conversationsSummaries: IConversationSummary[] | undefined; |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 29 | |
| 30 | setSearchQuery: SetState<string | undefined>; |
| 31 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 32 | searchResult: Contact[] | undefined; |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | const defaultMessengerContext: IMessengerContext = { |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 36 | conversationsSummaries: undefined, |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 37 | setSearchQuery: () => {}, |
| 38 | searchResult: undefined, |
| 39 | }; |
| 40 | |
| 41 | export const MessengerContext = createContext<IMessengerContext>(defaultMessengerContext); |
| 42 | |
| 43 | export default ({ children }: { children: ReactNode }) => { |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 44 | const { accountId, axiosInstance } = useAuthContext(); |
| 45 | const webSocket = useContext(WebSocketContext); |
| 46 | |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 47 | const [searchQuery, setSearchQuery] = useState<string>(); |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 48 | const [searchResult, setSearchResults] = useState<Contact[] | undefined>(undefined); |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 49 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 50 | const conversationsSummariesQuery = useConversationsSummariesQuery(); |
| 51 | const conversationsSummaries = conversationsSummariesQuery.data; |
| 52 | const refreshConversationsSummaries = useRefreshConversationsSummaries(); |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 53 | |
| 54 | useEffect(() => { |
| 55 | if (!webSocket) { |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | const conversationMessageListener = (_data: ConversationMessage) => { |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 60 | refreshConversationsSummaries(); |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | webSocket.bind(WebSocketMessageType.ConversationMessage, conversationMessageListener); |
| 64 | |
| 65 | return () => { |
| 66 | webSocket.unbind(WebSocketMessageType.ConversationMessage, conversationMessageListener); |
| 67 | }; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 68 | }, [refreshConversationsSummaries, webSocket]); |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 69 | |
| 70 | useEffect(() => { |
| 71 | if (!searchQuery) return; |
| 72 | const controller = new AbortController(); |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 73 | axiosInstance |
Misha Krieger-Raynauld | cfa4430 | 2022-11-30 18:36:36 -0500 | [diff] [blame] | 74 | .get<LookupResult>(`/ns/username/${searchQuery}`, { |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 75 | signal: controller.signal, |
| 76 | }) |
| 77 | .then(({ data }) => { |
Misha Krieger-Raynauld | cfa4430 | 2022-11-30 18:36:36 -0500 | [diff] [blame] | 78 | const contact = new Contact(data.address, data.username); |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 79 | setSearchResults([contact]); |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 80 | }) |
| 81 | .catch(() => { |
| 82 | setSearchResults(undefined); |
| 83 | }); |
| 84 | // return () => controller.abort() // crash on React18 |
| 85 | }, [accountId, searchQuery, axiosInstance]); |
| 86 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 87 | const value = useMemo<IMessengerContext>( |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 88 | () => ({ |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 89 | conversationsSummaries, |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 90 | setSearchQuery, |
| 91 | searchResult, |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 92 | }), |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 93 | [conversationsSummaries, setSearchQuery, searchResult] |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 94 | ); |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 95 | |
| 96 | return <MessengerContext.Provider value={value}>{children}</MessengerContext.Provider>; |
simon | 21f7d9f | 2022-11-28 14:21:54 -0500 | [diff] [blame] | 97 | }; |