blob: c38294acc75841f0ad4b26e12fb213c7058cb081 [file] [log] [blame]
simon26e79f72022-10-05 22:16:08 -04001/*
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 */
simon07b4eb02022-09-29 17:50:26 -040018import { Stack } from '@mui/material';
simon20076982022-10-11 15:04:13 -040019import { Contact, Conversation } from 'jami-web-common';
simond47ef9e2022-09-28 22:24:28 -040020import { useEffect, useState } from 'react';
simon07b4eb02022-09-29 17:50:26 -040021import { useParams } from 'react-router';
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040022
simon07b4eb02022-09-29 17:50:26 -040023import authManager from '../AuthManager';
Adrien Béraud6ecaa402021-04-06 17:37:25 -040024//import Sound from 'react-sound';
Adrien Béraud995e8022021-04-08 13:46:51 -040025import ConversationList from '../components/ConversationList';
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040026import ConversationView from '../components/ConversationView';
simon07b4eb02022-09-29 17:50:26 -040027import Header from '../components/Header';
simon6b9ddfb2022-10-03 00:04:50 -040028import LoadingPage from '../components/Loading';
simon07b4eb02022-09-29 17:50:26 -040029import NewContactForm from '../components/NewContactForm';
simond8ca2f22022-10-11 23:30:55 -040030import { useAppSelector } from '../redux/hooks';
simonfe1de722022-10-02 00:21:43 -040031import AddContactPage from './AddContactPage';
ervinanoh34eb9472022-09-13 04:20:28 -040032
simonfe1de722022-10-02 00:21:43 -040033type MessengerProps = {
34 accountId?: string;
35 conversationId?: string;
36 contactId?: string;
37};
38
39const Messenger = (props: MessengerProps) => {
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040040 const { refresh } = useAppSelector((state) => state.userInfo);
ervinanoh34eb9472022-09-13 04:20:28 -040041
simonfe1de722022-10-02 00:21:43 -040042 const [conversations, setConversations] = useState<Conversation[] | undefined>(undefined);
simond47ef9e2022-09-28 22:24:28 -040043 const [searchQuery, setSearchQuery] = useState('');
simonfe1de722022-10-02 00:21:43 -040044 const [searchResult, setSearchResults] = useState<Conversation | undefined>(undefined);
Larbi Gharibe9af9732021-03-31 15:08:01 +010045
simond47ef9e2022-09-28 22:24:28 -040046 const params = useParams();
47 const accountId = props.accountId || params.accountId;
48 const conversationId = props.conversationId || params.conversationId;
49 const contactId = props.contactId || params.contactId;
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040050
simonfe1de722022-10-02 00:21:43 -040051 if (accountId == null) {
52 throw new Error('Missing accountId');
53 }
54
Adrien Béraud4e287b92021-04-24 16:15:56 -040055 useEffect(() => {
simond47ef9e2022-09-28 22:24:28 -040056 console.log('REFRESH CONVERSATIONS FROM MESSENGER');
57 const controller = new AbortController();
58 authManager
59 .fetch(`/api/accounts/${accountId}/conversations`, { signal: controller.signal })
60 .then((res) => res.json())
simonfe1de722022-10-02 00:21:43 -040061 .then((result: Conversation[]) => {
simond47ef9e2022-09-28 22:24:28 -040062 console.log(result);
63 setConversations(Object.values(result).map((c) => Conversation.from(accountId, c)));
64 });
ervinanoh34eb9472022-09-13 04:20:28 -040065 // return () => controller.abort()
simond47ef9e2022-09-28 22:24:28 -040066 }, [accountId, refresh]);
Adrien Béraud995e8022021-04-08 13:46:51 -040067
Adrien Béraudabba2e52021-04-24 21:39:56 -040068 useEffect(() => {
simond47ef9e2022-09-28 22:24:28 -040069 if (!searchQuery) return;
70 const controller = new AbortController();
71 authManager
72 .fetch(`/api/accounts/${accountId}/ns/name/${searchQuery}`, { signal: controller.signal })
73 .then((response) => {
74 if (response.status === 200) {
75 return response.json();
76 } else {
simonfe1de722022-10-02 00:21:43 -040077 throw new Error(response.status.toString());
simond47ef9e2022-09-28 22:24:28 -040078 }
79 })
80 .then((response) => {
81 console.log(response);
82 const contact = new Contact(response.address);
83 contact.setRegisteredName(response.name);
84 setSearchResults(contact ? Conversation.fromSingleContact(accountId, contact) : undefined);
85 })
simon416d0792022-11-03 02:46:18 -040086 .catch(() => {
simond47ef9e2022-09-28 22:24:28 -040087 setSearchResults(undefined);
88 });
89 // return () => controller.abort() // crash on React18
90 }, [accountId, searchQuery]);
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040091
simond47ef9e2022-09-28 22:24:28 -040092 console.log('Messenger render');
Adrien Béraud4e287b92021-04-24 16:15:56 -040093 return (
simond47ef9e2022-09-28 22:24:28 -040094 <Stack direction="row" height="100vh" width="100vw">
95 <Stack flexGrow={0} flexShrink={0} overflow="auto">
idillonbef18a52022-09-01 01:51:40 -040096 <Header />
97 <NewContactForm onChange={setSearchQuery} />
98 {contactId && <AddContactPage accountId={accountId} contactId={contactId} />}
simond47ef9e2022-09-28 22:24:28 -040099 {conversations ? (
100 <ConversationList search={searchResult} conversations={conversations} accountId={accountId} />
101 ) : (
102 <div className="rooms-list">
103 <LoadingPage />
104 </div>
105 )}
idillonbef18a52022-09-01 01:51:40 -0400106 </Stack>
simond47ef9e2022-09-28 22:24:28 -0400107 <Stack flexGrow={1}>
idillonbef18a52022-09-01 01:51:40 -0400108 {conversationId && <ConversationView accountId={accountId} conversationId={conversationId} />}
109 </Stack>
110 </Stack>
simond47ef9e2022-09-28 22:24:28 -0400111 );
112};
Larbi Gharibe9af9732021-03-31 15:08:01 +0100113
simond47ef9e2022-09-28 22:24:28 -0400114export default Messenger;