simon | f929a36 | 2022-11-18 16:53:45 -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 { ConversationInfos, ConversationView, WebSocketMessageType } from 'jami-web-common'; |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 19 | import { useContext, useEffect, useMemo } from 'react'; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 20 | |
| 21 | import LoadingPage from '../components/Loading'; |
simon | 09fe482 | 2022-11-30 23:36:25 -0500 | [diff] [blame] | 22 | import { createOptionalContext } from '../hooks/createOptionalContext'; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 23 | import { useConversationDisplayName } from '../hooks/useConversationDisplayName'; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 24 | import { useUrlParams } from '../hooks/useUrlParams'; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 25 | import { ConversationMember } from '../models/conversation-member'; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 26 | import { ConversationRouteParams } from '../router'; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 27 | import { useConversationInfosQuery, useMembersQuery } from '../services/conversationQueries'; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 28 | import { WithChildren } from '../utils/utils'; |
| 29 | import { useAuthContext } from './AuthProvider'; |
| 30 | import { WebSocketContext } from './WebSocketProvider'; |
| 31 | |
simon | 09fe482 | 2022-11-30 23:36:25 -0500 | [diff] [blame] | 32 | interface IConversationContext { |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 33 | conversationId: string; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 34 | conversationDisplayName: string; |
| 35 | conversationInfos: ConversationInfos; |
| 36 | members: ConversationMember[]; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 37 | } |
| 38 | |
simon | 09fe482 | 2022-11-30 23:36:25 -0500 | [diff] [blame] | 39 | const optionalConversationContext = createOptionalContext<IConversationContext>('ConversationContext'); |
| 40 | const ConversationContext = optionalConversationContext.Context; |
| 41 | export const useConversationContext = optionalConversationContext.useOptionalContext; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 42 | |
| 43 | export default ({ children }: WithChildren) => { |
| 44 | const { |
| 45 | urlParams: { conversationId }, |
| 46 | } = useUrlParams<ConversationRouteParams>(); |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 47 | const { accountId, account } = useAuthContext(); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 48 | const webSocket = useContext(WebSocketContext); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 49 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 50 | const conversationInfosQuery = useConversationInfosQuery(conversationId!); |
| 51 | const membersQuery = useMembersQuery(conversationId!); |
| 52 | |
| 53 | const isError = useMemo( |
| 54 | () => conversationInfosQuery.isError || membersQuery.isError, |
| 55 | [conversationInfosQuery.isError, membersQuery.isError] |
| 56 | ); |
| 57 | |
| 58 | const isLoading = useMemo( |
| 59 | () => conversationInfosQuery.isLoading || membersQuery.isLoading, |
| 60 | [conversationInfosQuery.isLoading, membersQuery.isLoading] |
| 61 | ); |
| 62 | |
| 63 | const conversationInfos = conversationInfosQuery.data; |
| 64 | const members = membersQuery.data; |
| 65 | |
idillon | 9e542ca | 2022-12-15 17:54:07 -0500 | [diff] [blame] | 66 | const conversationDisplayName = useConversationDisplayName(account, conversationInfos, members); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 67 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 68 | useEffect(() => { |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 69 | if (!conversationInfos || !conversationId || !webSocket) { |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 70 | return; |
| 71 | } |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 72 | |
| 73 | const conversationView: ConversationView = { |
| 74 | conversationId, |
| 75 | }; |
| 76 | |
| 77 | webSocket.send(WebSocketMessageType.ConversationView, conversationView); |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 78 | }, [accountId, conversationInfos, conversationId, webSocket]); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 79 | |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 80 | const value = useMemo(() => { |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 81 | if (!conversationId || !conversationDisplayName || !conversationInfos || !members) { |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 82 | return; |
| 83 | } |
| 84 | |
| 85 | return { |
| 86 | conversationId, |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 87 | conversationDisplayName, |
| 88 | conversationInfos, |
| 89 | members, |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 90 | }; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 91 | }, [conversationId, conversationDisplayName, conversationInfos, members]); |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 92 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 93 | if (isLoading) { |
| 94 | return <LoadingPage />; |
| 95 | } |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 96 | if (isError || !value) { |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 97 | return <div>Error loading conversation: {conversationId}</div>; |
| 98 | } |
| 99 | |
simon | 5c67796 | 2022-12-02 16:51:54 -0500 | [diff] [blame] | 100 | return <ConversationContext.Provider value={value}>{children}</ConversationContext.Provider>; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 101 | }; |