blob: b6d2f4a614c8344a08475341f27da5c8c2935a3e [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 */
idillon07d31cc2022-12-06 22:40:14 -050018import { ConversationInfos, 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';
idillon07d31cc2022-12-06 22:40:14 -050023import { useConversationDisplayName } from '../hooks/useConversationDisplayName';
simonf929a362022-11-18 16:53:45 -050024import { useUrlParams } from '../hooks/useUrlParams';
idillon07d31cc2022-12-06 22:40:14 -050025import { ConversationMember } from '../models/conversation-member';
simonf929a362022-11-18 16:53:45 -050026import { ConversationRouteParams } from '../router';
idillon07d31cc2022-12-06 22:40:14 -050027import { useConversationInfosQuery, useMembersQuery } from '../services/conversationQueries';
simonf929a362022-11-18 16:53:45 -050028import { WithChildren } from '../utils/utils';
29import { useAuthContext } from './AuthProvider';
30import { WebSocketContext } from './WebSocketProvider';
31
simon09fe4822022-11-30 23:36:25 -050032interface IConversationContext {
simonf929a362022-11-18 16:53:45 -050033 conversationId: string;
idillon07d31cc2022-12-06 22:40:14 -050034 conversationDisplayName: string;
35 conversationInfos: ConversationInfos;
36 members: ConversationMember[];
simonf929a362022-11-18 16:53:45 -050037}
38
simon09fe4822022-11-30 23:36:25 -050039const optionalConversationContext = createOptionalContext<IConversationContext>('ConversationContext');
40const ConversationContext = optionalConversationContext.Context;
41export const useConversationContext = optionalConversationContext.useOptionalContext;
simonf929a362022-11-18 16:53:45 -050042
43export default ({ children }: WithChildren) => {
44 const {
45 urlParams: { conversationId },
46 } = useUrlParams<ConversationRouteParams>();
idillon07d31cc2022-12-06 22:40:14 -050047 const { accountId, account } = useAuthContext();
simonf929a362022-11-18 16:53:45 -050048 const webSocket = useContext(WebSocketContext);
simonf929a362022-11-18 16:53:45 -050049
idillon07d31cc2022-12-06 22:40:14 -050050 const conversationInfosQuery = useConversationInfosQuery(conversationId!);
51 const membersQuery = useMembersQuery(conversationId!);
52
53 const isError = useMemo(
54 () => conversationInfosQuery.isError || membersQuery.isError,
55 [conversationInfosQuery.isError, membersQuery.isError]
56 );
57
58 const isLoading = useMemo(
59 () => conversationInfosQuery.isLoading || membersQuery.isLoading,
60 [conversationInfosQuery.isLoading, membersQuery.isLoading]
61 );
62
63 const conversationInfos = conversationInfosQuery.data;
64 const members = membersQuery.data;
65
66 const conversationDisplayName = useConversationDisplayName(account, conversationId, conversationInfos, members);
simonf929a362022-11-18 16:53:45 -050067
simonf929a362022-11-18 16:53:45 -050068 useEffect(() => {
idillon07d31cc2022-12-06 22:40:14 -050069 if (!conversationInfos || !conversationId || !webSocket) {
simonf929a362022-11-18 16:53:45 -050070 return;
71 }
Misha Krieger-Raynauld20cf1c82022-11-23 20:26:50 -050072
73 const conversationView: ConversationView = {
74 conversationId,
75 };
76
77 webSocket.send(WebSocketMessageType.ConversationView, conversationView);
idillon07d31cc2022-12-06 22:40:14 -050078 }, [accountId, conversationInfos, conversationId, webSocket]);
simonf929a362022-11-18 16:53:45 -050079
simon5c677962022-12-02 16:51:54 -050080 const value = useMemo(() => {
idillon07d31cc2022-12-06 22:40:14 -050081 if (!conversationId || !conversationDisplayName || !conversationInfos || !members) {
simon5c677962022-12-02 16:51:54 -050082 return;
83 }
84
85 return {
86 conversationId,
idillon07d31cc2022-12-06 22:40:14 -050087 conversationDisplayName,
88 conversationInfos,
89 members,
simon5c677962022-12-02 16:51:54 -050090 };
idillon07d31cc2022-12-06 22:40:14 -050091 }, [conversationId, conversationDisplayName, conversationInfos, members]);
simon5c677962022-12-02 16:51:54 -050092
simonf929a362022-11-18 16:53:45 -050093 if (isLoading) {
94 return <LoadingPage />;
95 }
simon5c677962022-12-02 16:51:54 -050096 if (isError || !value) {
simonf929a362022-11-18 16:53:45 -050097 return <div>Error loading conversation: {conversationId}</div>;
98 }
99
simon5c677962022-12-02 16:51:54 -0500100 return <ConversationContext.Provider value={value}>{children}</ConversationContext.Provider>;
simonf929a362022-11-18 16:53:45 -0500101};