blob: e2aa4bc24ef69eaf4cb03d5a31202ce12eecc361 [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';
simone35acc22022-12-02 16:51:12 -050019import { useContext, useEffect } 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-Raynauldcfa44302022-11-30 18:36:36 -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);
simonf929a362022-11-18 16:53:45 -050046
simone35acc22022-12-02 16:51:12 -050047 const { conversation, isLoading, isError } = useConversationQuery(conversationId!);
simonf929a362022-11-18 16:53:45 -050048
simonf929a362022-11-18 16:53:45 -050049 useEffect(() => {
simonff1cb352022-11-24 15:15:26 -050050 if (!conversation || !conversationId || !webSocket) {
simonf929a362022-11-18 16:53:45 -050051 return;
52 }
Misha Krieger-Raynauld20cf1c82022-11-23 20:26:50 -050053
54 const conversationView: ConversationView = {
55 conversationId,
56 };
57
58 webSocket.send(WebSocketMessageType.ConversationView, conversationView);
simonf929a362022-11-18 16:53:45 -050059 }, [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,
simonf929a362022-11-18 16:53:45 -050073 }}
74 >
75 {children}
76 </ConversationContext.Provider>
77 );
78};