blob: fdbb0f4d9ff6c4b4d51d406a677c2df518837d27 [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';
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040021
Adrien Béraud6ecaa402021-04-06 17:37:25 -040022//import Sound from 'react-sound';
Adrien Béraud995e8022021-04-08 13:46:51 -040023import ConversationList from '../components/ConversationList';
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040024import ConversationView from '../components/ConversationView';
simon07b4eb02022-09-29 17:50:26 -040025import Header from '../components/Header';
simon6b9ddfb2022-10-03 00:04:50 -040026import LoadingPage from '../components/Loading';
simon07b4eb02022-09-29 17:50:26 -040027import NewContactForm from '../components/NewContactForm';
simon5da8ca62022-11-09 15:21:25 -050028import { useAuthContext } from '../contexts/AuthProvider';
simond8ca2f22022-10-11 23:30:55 -040029import { useAppSelector } from '../redux/hooks';
simon5da8ca62022-11-09 15:21:25 -050030import { apiUrl } from '../utils/constants';
31import { useUrlParams } from '../utils/hooks';
simonfe1de722022-10-02 00:21:43 -040032import AddContactPage from './AddContactPage';
simon5da8ca62022-11-09 15:21:25 -050033import { MessengerRouteParams } from './JamiMessenger';
ervinanoh34eb9472022-09-13 04:20:28 -040034
simon5da8ca62022-11-09 15:21:25 -050035const Messenger = () => {
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040036 const { refresh } = useAppSelector((state) => state.userInfo);
simon5da8ca62022-11-09 15:21:25 -050037 const { token, account } = useAuthContext();
ervinanoh34eb9472022-09-13 04:20:28 -040038
simonfe1de722022-10-02 00:21:43 -040039 const [conversations, setConversations] = useState<Conversation[] | undefined>(undefined);
simond47ef9e2022-09-28 22:24:28 -040040 const [searchQuery, setSearchQuery] = useState('');
simonfe1de722022-10-02 00:21:43 -040041 const [searchResult, setSearchResults] = useState<Conversation | undefined>(undefined);
Larbi Gharibe9af9732021-03-31 15:08:01 +010042
simon5da8ca62022-11-09 15:21:25 -050043 const {
44 urlParams: { conversationId, contactId },
45 } = useUrlParams<MessengerRouteParams>();
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040046
simon5da8ca62022-11-09 15:21:25 -050047 const accountId = account.getId();
simonfe1de722022-10-02 00:21:43 -040048
Adrien Béraud4e287b92021-04-24 16:15:56 -040049 useEffect(() => {
simond47ef9e2022-09-28 22:24:28 -040050 console.log('REFRESH CONVERSATIONS FROM MESSENGER');
51 const controller = new AbortController();
simon5da8ca62022-11-09 15:21:25 -050052 fetch(new URL(`/conversations`, apiUrl), {
53 headers: {
54 Authorization: `Bearer ${token}`,
55 },
56 signal: controller.signal,
57 })
simond47ef9e2022-09-28 22:24:28 -040058 .then((res) => res.json())
simonfe1de722022-10-02 00:21:43 -040059 .then((result: Conversation[]) => {
simond47ef9e2022-09-28 22:24:28 -040060 console.log(result);
61 setConversations(Object.values(result).map((c) => Conversation.from(accountId, c)));
62 });
ervinanoh34eb9472022-09-13 04:20:28 -040063 // return () => controller.abort()
simon5da8ca62022-11-09 15:21:25 -050064 }, [token, accountId, refresh]);
Adrien Béraud995e8022021-04-08 13:46:51 -040065
Adrien Béraudabba2e52021-04-24 21:39:56 -040066 useEffect(() => {
simond47ef9e2022-09-28 22:24:28 -040067 if (!searchQuery) return;
68 const controller = new AbortController();
simon5da8ca62022-11-09 15:21:25 -050069 fetch(new URL(`/ns/username/${searchQuery}`, apiUrl), {
70 headers: {
71 Authorization: `Bearer ${token}`,
72 },
73 signal: controller.signal,
74 })
simond47ef9e2022-09-28 22:24:28 -040075 .then((response) => {
76 if (response.status === 200) {
77 return response.json();
78 } else {
simonfe1de722022-10-02 00:21:43 -040079 throw new Error(response.status.toString());
simond47ef9e2022-09-28 22:24:28 -040080 }
81 })
82 .then((response) => {
83 console.log(response);
84 const contact = new Contact(response.address);
simon5da8ca62022-11-09 15:21:25 -050085 contact.setRegisteredName(response.username);
simond47ef9e2022-09-28 22:24:28 -040086 setSearchResults(contact ? Conversation.fromSingleContact(accountId, contact) : undefined);
87 })
simon416d0792022-11-03 02:46:18 -040088 .catch(() => {
simond47ef9e2022-09-28 22:24:28 -040089 setSearchResults(undefined);
90 });
91 // return () => controller.abort() // crash on React18
simon5da8ca62022-11-09 15:21:25 -050092 }, [accountId, searchQuery, token]);
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040093
simond47ef9e2022-09-28 22:24:28 -040094 console.log('Messenger render');
Adrien Béraud4e287b92021-04-24 16:15:56 -040095 return (
simond47ef9e2022-09-28 22:24:28 -040096 <Stack direction="row" height="100vh" width="100vw">
97 <Stack flexGrow={0} flexShrink={0} overflow="auto">
idillonbef18a52022-09-01 01:51:40 -040098 <Header />
99 <NewContactForm onChange={setSearchQuery} />
simon5da8ca62022-11-09 15:21:25 -0500100 {contactId && <AddContactPage contactId={contactId} />}
simond47ef9e2022-09-28 22:24:28 -0400101 {conversations ? (
102 <ConversationList search={searchResult} conversations={conversations} accountId={accountId} />
103 ) : (
104 <div className="rooms-list">
105 <LoadingPage />
106 </div>
107 )}
idillonbef18a52022-09-01 01:51:40 -0400108 </Stack>
simon5da8ca62022-11-09 15:21:25 -0500109 <Stack flexGrow={1}>{conversationId && <ConversationView conversationId={conversationId} />}</Stack>
idillonbef18a52022-09-01 01:51:40 -0400110 </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;