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 | */ |
Misha Krieger-Raynauld | 6bbdacf | 2022-11-29 21:45:40 -0500 | [diff] [blame] | 18 | import { ConversationView, WebSocketMessageType } from 'jami-web-common'; |
simon | 09fe482 | 2022-11-30 23:36:25 -0500 | [diff] [blame] | 19 | import { useContext, useEffect, useState } 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'; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 23 | import { useUrlParams } from '../hooks/useUrlParams'; |
Misha Krieger-Raynauld | 6bbdacf | 2022-11-29 21:45:40 -0500 | [diff] [blame] | 24 | import { Conversation } from '../models/Conversation'; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 25 | import { ConversationRouteParams } from '../router'; |
Misha Krieger-Raynauld | 6bbdacf | 2022-11-29 21:45:40 -0500 | [diff] [blame] | 26 | import { useConversationQuery } from '../services/conversationQueries'; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 27 | import { WithChildren } from '../utils/utils'; |
| 28 | import { useAuthContext } from './AuthProvider'; |
| 29 | import { WebSocketContext } from './WebSocketProvider'; |
| 30 | |
simon | 09fe482 | 2022-11-30 23:36:25 -0500 | [diff] [blame] | 31 | interface IConversationContext { |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 32 | conversationId: string; |
| 33 | conversation: Conversation; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 34 | } |
| 35 | |
simon | 09fe482 | 2022-11-30 23:36:25 -0500 | [diff] [blame] | 36 | const optionalConversationContext = createOptionalContext<IConversationContext>('ConversationContext'); |
| 37 | const ConversationContext = optionalConversationContext.Context; |
| 38 | export const useConversationContext = optionalConversationContext.useOptionalContext; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 39 | |
| 40 | export default ({ children }: WithChildren) => { |
| 41 | const { |
| 42 | urlParams: { conversationId }, |
| 43 | } = useUrlParams<ConversationRouteParams>(); |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 44 | const { accountId } = useAuthContext(); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 45 | const webSocket = useContext(WebSocketContext); |
| 46 | const [isLoading, setIsLoading] = useState(false); |
| 47 | const [isError, setIsError] = useState(false); |
| 48 | const [conversation, setConversation] = useState<Conversation | undefined>(); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 49 | |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 50 | const conversationQuery = useConversationQuery(conversationId!); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 51 | |
| 52 | useEffect(() => { |
| 53 | if (conversationQuery.isSuccess) { |
| 54 | const conversation = Conversation.from(accountId, conversationQuery.data); |
| 55 | setConversation(conversation); |
| 56 | } |
| 57 | }, [accountId, conversationQuery.isSuccess, conversationQuery.data]); |
| 58 | |
| 59 | useEffect(() => { |
| 60 | setIsLoading(conversationQuery.isLoading); |
| 61 | }, [conversationQuery.isLoading]); |
| 62 | |
| 63 | useEffect(() => { |
| 64 | setIsError(conversationQuery.isError); |
| 65 | }, [conversationQuery.isError]); |
| 66 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 67 | useEffect(() => { |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 68 | if (!conversation || !conversationId || !webSocket) { |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 69 | return; |
| 70 | } |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 71 | |
| 72 | const conversationView: ConversationView = { |
| 73 | conversationId, |
| 74 | }; |
| 75 | |
| 76 | webSocket.send(WebSocketMessageType.ConversationView, conversationView); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 77 | }, [accountId, conversation, conversationId, webSocket]); |
| 78 | |
| 79 | if (isLoading) { |
| 80 | return <LoadingPage />; |
| 81 | } |
| 82 | if (isError || !conversation || !conversationId) { |
| 83 | return <div>Error loading conversation: {conversationId}</div>; |
| 84 | } |
| 85 | |
| 86 | return ( |
| 87 | <ConversationContext.Provider |
| 88 | value={{ |
| 89 | conversationId, |
| 90 | conversation, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 91 | }} |
| 92 | > |
| 93 | {children} |
| 94 | </ConversationContext.Provider> |
| 95 | ); |
| 96 | }; |