blob: 25137c07a6c8b7b0c4272871e7b4856b67e66cbd [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 */
Misha Krieger-Raynauld6bbdacf2022-11-29 21:45:40 -050018import { ConversationView, WebSocketMessageType } from 'jami-web-common';
simonff1cb352022-11-24 15:15:26 -050019import { createContext, useContext, useEffect, useState } from 'react';
simonf929a362022-11-18 16:53:45 -050020
21import LoadingPage from '../components/Loading';
22import { useUrlParams } from '../hooks/useUrlParams';
Misha Krieger-Raynauld6bbdacf2022-11-29 21:45:40 -050023import { Conversation } from '../models/Conversation';
simonf929a362022-11-18 16:53:45 -050024import { ConversationRouteParams } from '../router';
Misha Krieger-Raynauld6bbdacf2022-11-29 21:45:40 -050025import { useConversationQuery } from '../services/conversationQueries';
simonf929a362022-11-18 16:53:45 -050026import { WithChildren } from '../utils/utils';
27import { useAuthContext } from './AuthProvider';
28import { WebSocketContext } from './WebSocketProvider';
29
30interface IConversationProvider {
31 conversationId: string;
32 conversation: Conversation;
simonf929a362022-11-18 16:53:45 -050033}
34
35export const ConversationContext = createContext<IConversationProvider>(undefined!);
36
37export default ({ children }: WithChildren) => {
38 const {
39 urlParams: { conversationId },
40 } = useUrlParams<ConversationRouteParams>();
Misha Krieger-Raynauld20cf1c82022-11-23 20:26:50 -050041 const { accountId } = useAuthContext();
simonf929a362022-11-18 16:53:45 -050042 const webSocket = useContext(WebSocketContext);
43 const [isLoading, setIsLoading] = useState(false);
44 const [isError, setIsError] = useState(false);
45 const [conversation, setConversation] = useState<Conversation | undefined>();
simonf929a362022-11-18 16:53:45 -050046
simonff1cb352022-11-24 15:15:26 -050047 const conversationQuery = useConversationQuery(conversationId!);
simonf929a362022-11-18 16:53:45 -050048
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
simonf929a362022-11-18 16:53:45 -050064 useEffect(() => {
simonff1cb352022-11-24 15:15:26 -050065 if (!conversation || !conversationId || !webSocket) {
simonf929a362022-11-18 16:53:45 -050066 return;
67 }
Misha Krieger-Raynauld20cf1c82022-11-23 20:26:50 -050068
69 const conversationView: ConversationView = {
70 conversationId,
71 };
72
73 webSocket.send(WebSocketMessageType.ConversationView, conversationView);
simonf929a362022-11-18 16:53:45 -050074 }, [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,
simonf929a362022-11-18 16:53:45 -050088 }}
89 >
90 {children}
91 </ConversationContext.Provider>
92 );
93};