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