blob: 8488feba9c23b2c74fa2c89433bfe75c9e84c066 [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';
simon5c677962022-12-02 16:51:54 -050019import { useContext, useEffect, useMemo } 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
simon5c677962022-12-02 16:51:54 -050061 const value = useMemo(() => {
62 if (!conversation || !conversationId) {
63 return;
64 }
65
66 return {
67 conversationId,
68 conversation,
69 };
70 }, [conversationId, conversation]);
71
simonf929a362022-11-18 16:53:45 -050072 if (isLoading) {
73 return <LoadingPage />;
74 }
simon5c677962022-12-02 16:51:54 -050075 if (isError || !value) {
simonf929a362022-11-18 16:53:45 -050076 return <div>Error loading conversation: {conversationId}</div>;
77 }
78
simon5c677962022-12-02 16:51:54 -050079 return <ConversationContext.Provider value={value}>{children}</ConversationContext.Provider>;
simonf929a362022-11-18 16:53:45 -050080};