blob: fd8dc991ff6b415247e70fd846aec330af3c9218 [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';
simon3f5f3e72022-11-08 21:01:57 -050030import { MessengerRouteParams } from '../router';
simon5da8ca62022-11-09 15:21:25 -050031import { useUrlParams } from '../utils/hooks';
simonfe1de722022-10-02 00:21:43 -040032import AddContactPage from './AddContactPage';
ervinanoh34eb9472022-09-13 04:20:28 -040033
simon5da8ca62022-11-09 15:21:25 -050034const Messenger = () => {
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040035 const { refresh } = useAppSelector((state) => state.userInfo);
simon94fe53e2022-11-10 12:51:58 -050036 const { account, axiosInstance } = useAuthContext();
ervinanoh34eb9472022-09-13 04:20:28 -040037
simonfe1de722022-10-02 00:21:43 -040038 const [conversations, setConversations] = useState<Conversation[] | undefined>(undefined);
simond47ef9e2022-09-28 22:24:28 -040039 const [searchQuery, setSearchQuery] = useState('');
simonfe1de722022-10-02 00:21:43 -040040 const [searchResult, setSearchResults] = useState<Conversation | undefined>(undefined);
Larbi Gharibe9af9732021-03-31 15:08:01 +010041
simon5da8ca62022-11-09 15:21:25 -050042 const {
43 urlParams: { conversationId, contactId },
44 } = useUrlParams<MessengerRouteParams>();
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040045
simon5da8ca62022-11-09 15:21:25 -050046 const accountId = account.getId();
simonfe1de722022-10-02 00:21:43 -040047
Adrien Béraud4e287b92021-04-24 16:15:56 -040048 useEffect(() => {
simond47ef9e2022-09-28 22:24:28 -040049 const controller = new AbortController();
simon94fe53e2022-11-10 12:51:58 -050050 axiosInstance
51 .get<Conversation[]>('/conversations', {
52 signal: controller.signal,
53 })
54 .then(({ data }) => {
55 setConversations(Object.values(data).map((c) => Conversation.from(accountId, c)));
simond47ef9e2022-09-28 22:24:28 -040056 });
ervinanoh34eb9472022-09-13 04:20:28 -040057 // return () => controller.abort()
simon94fe53e2022-11-10 12:51:58 -050058 }, [axiosInstance, accountId, refresh]);
Adrien Béraud995e8022021-04-08 13:46:51 -040059
Adrien Béraudabba2e52021-04-24 21:39:56 -040060 useEffect(() => {
simond47ef9e2022-09-28 22:24:28 -040061 if (!searchQuery) return;
62 const controller = new AbortController();
simon94fe53e2022-11-10 12:51:58 -050063 // TODO: Type properly https://git.jami.net/savoirfairelinux/jami-web/-/issues/92
64 axiosInstance
65 .get<{ state: number; address: string; username: string }>(`/ns/username/${searchQuery}`, {
66 signal: controller.signal,
simond47ef9e2022-09-28 22:24:28 -040067 })
simon94fe53e2022-11-10 12:51:58 -050068 .then(({ data }) => {
69 const contact = new Contact(data.address);
70 contact.setRegisteredName(data.username);
simond47ef9e2022-09-28 22:24:28 -040071 setSearchResults(contact ? Conversation.fromSingleContact(accountId, contact) : undefined);
72 })
simon416d0792022-11-03 02:46:18 -040073 .catch(() => {
simond47ef9e2022-09-28 22:24:28 -040074 setSearchResults(undefined);
75 });
76 // return () => controller.abort() // crash on React18
simon94fe53e2022-11-10 12:51:58 -050077 }, [accountId, searchQuery, axiosInstance]);
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040078
Adrien Béraud4e287b92021-04-24 16:15:56 -040079 return (
simond47ef9e2022-09-28 22:24:28 -040080 <Stack direction="row" height="100vh" width="100vw">
81 <Stack flexGrow={0} flexShrink={0} overflow="auto">
idillonbef18a52022-09-01 01:51:40 -040082 <Header />
83 <NewContactForm onChange={setSearchQuery} />
simon5da8ca62022-11-09 15:21:25 -050084 {contactId && <AddContactPage contactId={contactId} />}
simond47ef9e2022-09-28 22:24:28 -040085 {conversations ? (
86 <ConversationList search={searchResult} conversations={conversations} accountId={accountId} />
87 ) : (
88 <div className="rooms-list">
89 <LoadingPage />
90 </div>
91 )}
idillonbef18a52022-09-01 01:51:40 -040092 </Stack>
simon5da8ca62022-11-09 15:21:25 -050093 <Stack flexGrow={1}>{conversationId && <ConversationView conversationId={conversationId} />}</Stack>
idillonbef18a52022-09-01 01:51:40 -040094 </Stack>
simond47ef9e2022-09-28 22:24:28 -040095 );
96};
Larbi Gharibe9af9732021-03-31 15:08:01 +010097
simond47ef9e2022-09-28 22:24:28 -040098export default Messenger;