blob: 7d09881ad5f0aed7737a7a175accceae60cdfb55 [file] [log] [blame]
simonf929a362022-11-18 16:53:45 -05001/*
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 */
simonff1cb352022-11-24 15:15:26 -050018import { Conversation, ConversationView, WebSocketMessageType } from 'jami-web-common';
19import { createContext, useContext, useEffect, useState } from 'react';
simonf929a362022-11-18 16:53:45 -050020
21import LoadingPage from '../components/Loading';
22import { useUrlParams } from '../hooks/useUrlParams';
23import { ConversationRouteParams } from '../router';
24import { useConversationQuery } from '../services/Conversation';
25import { WithChildren } from '../utils/utils';
26import { useAuthContext } from './AuthProvider';
27import { WebSocketContext } from './WebSocketProvider';
28
29interface IConversationProvider {
30 conversationId: string;
31 conversation: Conversation;
simonf929a362022-11-18 16:53:45 -050032}
33
34export const ConversationContext = createContext<IConversationProvider>(undefined!);
35
36export default ({ children }: WithChildren) => {
37 const {
38 urlParams: { conversationId },
39 } = useUrlParams<ConversationRouteParams>();
Misha Krieger-Raynauld20cf1c82022-11-23 20:26:50 -050040 const { accountId } = useAuthContext();
simonf929a362022-11-18 16:53:45 -050041 const webSocket = useContext(WebSocketContext);
42 const [isLoading, setIsLoading] = useState(false);
43 const [isError, setIsError] = useState(false);
44 const [conversation, setConversation] = useState<Conversation | undefined>();
simonf929a362022-11-18 16:53:45 -050045
simonff1cb352022-11-24 15:15:26 -050046 const conversationQuery = useConversationQuery(conversationId!);
simonf929a362022-11-18 16:53:45 -050047
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
simonf929a362022-11-18 16:53:45 -050063 useEffect(() => {
simonff1cb352022-11-24 15:15:26 -050064 if (!conversation || !conversationId || !webSocket) {
simonf929a362022-11-18 16:53:45 -050065 return;
66 }
Misha Krieger-Raynauld20cf1c82022-11-23 20:26:50 -050067
68 const conversationView: ConversationView = {
69 conversationId,
70 };
71
72 webSocket.send(WebSocketMessageType.ConversationView, conversationView);
simonf929a362022-11-18 16:53:45 -050073 }, [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,
simonf929a362022-11-18 16:53:45 -050087 }}
88 >
89 {children}
90 </ConversationContext.Provider>
91 );
92};