blob: b927278b96f43c9029761c2b161791b5ae7b9704 [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';
simon09fe4822022-11-30 23:36:25 -050019import { useContext, useEffect, useState } from 'react';
simonf929a362022-11-18 16:53:45 -050020
21import LoadingPage from '../components/Loading';
simon09fe4822022-11-30 23:36:25 -050022import { createOptionalContext } from '../hooks/createOptionalContext';
simonf929a362022-11-18 16:53:45 -050023import { useUrlParams } from '../hooks/useUrlParams';
Misha Krieger-Raynauld6bbdacf2022-11-29 21:45:40 -050024import { Conversation } from '../models/Conversation';
simonf929a362022-11-18 16:53:45 -050025import { ConversationRouteParams } from '../router';
Misha Krieger-Raynauld6bbdacf2022-11-29 21:45:40 -050026import { useConversationQuery } from '../services/conversationQueries';
simonf929a362022-11-18 16:53:45 -050027import { WithChildren } from '../utils/utils';
28import { useAuthContext } from './AuthProvider';
29import { WebSocketContext } from './WebSocketProvider';
30
simon09fe4822022-11-30 23:36:25 -050031interface IConversationContext {
simonf929a362022-11-18 16:53:45 -050032 conversationId: string;
33 conversation: Conversation;
simonf929a362022-11-18 16:53:45 -050034}
35
simon09fe4822022-11-30 23:36:25 -050036const optionalConversationContext = createOptionalContext<IConversationContext>('ConversationContext');
37const ConversationContext = optionalConversationContext.Context;
38export const useConversationContext = optionalConversationContext.useOptionalContext;
simonf929a362022-11-18 16:53:45 -050039
40export default ({ children }: WithChildren) => {
41 const {
42 urlParams: { conversationId },
43 } = useUrlParams<ConversationRouteParams>();
Misha Krieger-Raynauld20cf1c82022-11-23 20:26:50 -050044 const { accountId } = useAuthContext();
simonf929a362022-11-18 16:53:45 -050045 const webSocket = useContext(WebSocketContext);
46 const [isLoading, setIsLoading] = useState(false);
47 const [isError, setIsError] = useState(false);
48 const [conversation, setConversation] = useState<Conversation | undefined>();
simonf929a362022-11-18 16:53:45 -050049
simonff1cb352022-11-24 15:15:26 -050050 const conversationQuery = useConversationQuery(conversationId!);
simonf929a362022-11-18 16:53:45 -050051
52 useEffect(() => {
53 if (conversationQuery.isSuccess) {
54 const conversation = Conversation.from(accountId, conversationQuery.data);
55 setConversation(conversation);
56 }
57 }, [accountId, conversationQuery.isSuccess, conversationQuery.data]);
58
59 useEffect(() => {
60 setIsLoading(conversationQuery.isLoading);
61 }, [conversationQuery.isLoading]);
62
63 useEffect(() => {
64 setIsError(conversationQuery.isError);
65 }, [conversationQuery.isError]);
66
simonf929a362022-11-18 16:53:45 -050067 useEffect(() => {
simonff1cb352022-11-24 15:15:26 -050068 if (!conversation || !conversationId || !webSocket) {
simonf929a362022-11-18 16:53:45 -050069 return;
70 }
Misha Krieger-Raynauld20cf1c82022-11-23 20:26:50 -050071
72 const conversationView: ConversationView = {
73 conversationId,
74 };
75
76 webSocket.send(WebSocketMessageType.ConversationView, conversationView);
simonf929a362022-11-18 16:53:45 -050077 }, [accountId, conversation, conversationId, webSocket]);
78
79 if (isLoading) {
80 return <LoadingPage />;
81 }
82 if (isError || !conversation || !conversationId) {
83 return <div>Error loading conversation: {conversationId}</div>;
84 }
85
86 return (
87 <ConversationContext.Provider
88 value={{
89 conversationId,
90 conversation,
simonf929a362022-11-18 16:53:45 -050091 }}
92 >
93 {children}
94 </ConversationContext.Provider>
95 );
96};