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 | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 19 | import { useContext, useEffect } 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 | cfa4430 | 2022-11-30 18:36:36 -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); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 46 | |
simon | e35acc2 | 2022-12-02 16:51:12 -0500 | [diff] [blame] | 47 | const { conversation, isLoading, isError } = useConversationQuery(conversationId!); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 48 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 49 | useEffect(() => { |
simon | ff1cb35 | 2022-11-24 15:15:26 -0500 | [diff] [blame] | 50 | if (!conversation || !conversationId || !webSocket) { |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 51 | return; |
| 52 | } |
Misha Krieger-Raynauld | 20cf1c8 | 2022-11-23 20:26:50 -0500 | [diff] [blame] | 53 | |
| 54 | const conversationView: ConversationView = { |
| 55 | conversationId, |
| 56 | }; |
| 57 | |
| 58 | webSocket.send(WebSocketMessageType.ConversationView, conversationView); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 59 | }, [accountId, conversation, conversationId, webSocket]); |
| 60 | |
| 61 | if (isLoading) { |
| 62 | return <LoadingPage />; |
| 63 | } |
| 64 | if (isError || !conversation || !conversationId) { |
| 65 | return <div>Error loading conversation: {conversationId}</div>; |
| 66 | } |
| 67 | |
| 68 | return ( |
| 69 | <ConversationContext.Provider |
| 70 | value={{ |
| 71 | conversationId, |
| 72 | conversation, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 73 | }} |
| 74 | > |
| 75 | {children} |
| 76 | </ConversationContext.Provider> |
| 77 | ); |
| 78 | }; |