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