blob: 9b28a28c08faf442d84b66ec443bda7a00a89c7f [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 */
simon9f814a32022-11-22 21:40:53 -050018import { Box, Stack } from '@mui/material';
Michelle Sepkap Simedd82cbf2022-11-17 23:31:49 -050019import { Contact, Conversation, WebSocketMessageType } from 'jami-web-common';
20import { useContext, useEffect, useState } from 'react';
simonf929a362022-11-18 16:53:45 -050021import { Outlet } from 'react-router-dom';
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040022
Adrien Béraud6ecaa402021-04-06 17:37:25 -040023//import Sound from 'react-sound';
Adrien Béraud995e8022021-04-08 13:46:51 -040024import ConversationList from '../components/ConversationList';
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';
Michelle Sepkap Simedd82cbf2022-11-17 23:31:49 -050029import { WebSocketContext } from '../contexts/WebSocketProvider';
simonf929a362022-11-18 16:53:45 -050030import { useUrlParams } from '../hooks/useUrlParams';
Michelle Sepkap Simedd82cbf2022-11-17 23:31:49 -050031import { setRefreshFromSlice } from '../redux/appSlice';
32import { useAppDispatch, useAppSelector } from '../redux/hooks';
simonf929a362022-11-18 16:53:45 -050033import { AddContactRouteParams } from '../router';
simonfe1de722022-10-02 00:21:43 -040034import AddContactPage from './AddContactPage';
ervinanoh34eb9472022-09-13 04:20:28 -040035
simon5da8ca62022-11-09 15:21:25 -050036const Messenger = () => {
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040037 const { refresh } = useAppSelector((state) => state.userInfo);
Michelle Sepkap Simedd82cbf2022-11-17 23:31:49 -050038 const dispatch = useAppDispatch();
simon94fe53e2022-11-10 12:51:58 -050039 const { account, axiosInstance } = useAuthContext();
Michelle Sepkap Simedd82cbf2022-11-17 23:31:49 -050040 const webSocket = useContext(WebSocketContext);
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
simon5da8ca62022-11-09 15:21:25 -050046 const {
simonf929a362022-11-18 16:53:45 -050047 urlParams: { contactId },
48 } = useUrlParams<AddContactRouteParams>();
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040049
simon5da8ca62022-11-09 15:21:25 -050050 const accountId = account.getId();
simonfe1de722022-10-02 00:21:43 -040051
Adrien Béraud4e287b92021-04-24 16:15:56 -040052 useEffect(() => {
simond47ef9e2022-09-28 22:24:28 -040053 const controller = new AbortController();
simon94fe53e2022-11-10 12:51:58 -050054 axiosInstance
55 .get<Conversation[]>('/conversations', {
56 signal: controller.signal,
57 })
58 .then(({ data }) => {
59 setConversations(Object.values(data).map((c) => Conversation.from(accountId, c)));
simond47ef9e2022-09-28 22:24:28 -040060 });
ervinanoh34eb9472022-09-13 04:20:28 -040061 // return () => controller.abort()
simon94fe53e2022-11-10 12:51:58 -050062 }, [axiosInstance, accountId, refresh]);
Adrien Béraud995e8022021-04-08 13:46:51 -040063
Adrien Béraudabba2e52021-04-24 21:39:56 -040064 useEffect(() => {
Michelle Sepkap Simedd82cbf2022-11-17 23:31:49 -050065 if (!webSocket) {
66 return;
67 }
68
69 const conversationMessageListener = () => dispatch(setRefreshFromSlice());
70 webSocket.bind(WebSocketMessageType.ConversationMessage, conversationMessageListener);
71 return () => webSocket.unbind(WebSocketMessageType.ConversationMessage, conversationMessageListener);
72 }, [webSocket, dispatch]);
73
74 useEffect(() => {
simond47ef9e2022-09-28 22:24:28 -040075 if (!searchQuery) return;
76 const controller = new AbortController();
simon94fe53e2022-11-10 12:51:58 -050077 // TODO: Type properly https://git.jami.net/savoirfairelinux/jami-web/-/issues/92
78 axiosInstance
79 .get<{ state: number; address: string; username: string }>(`/ns/username/${searchQuery}`, {
80 signal: controller.signal,
simond47ef9e2022-09-28 22:24:28 -040081 })
simon94fe53e2022-11-10 12:51:58 -050082 .then(({ data }) => {
83 const contact = new Contact(data.address);
84 contact.setRegisteredName(data.username);
simond47ef9e2022-09-28 22:24:28 -040085 setSearchResults(contact ? Conversation.fromSingleContact(accountId, contact) : undefined);
86 })
simon416d0792022-11-03 02:46:18 -040087 .catch(() => {
simond47ef9e2022-09-28 22:24:28 -040088 setSearchResults(undefined);
89 });
90 // return () => controller.abort() // crash on React18
simon94fe53e2022-11-10 12:51:58 -050091 }, [accountId, searchQuery, axiosInstance]);
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040092
Adrien Béraud4e287b92021-04-24 16:15:56 -040093 return (
simon9f814a32022-11-22 21:40:53 -050094 <Box display="flex" height="100%">
simond47ef9e2022-09-28 22:24:28 -040095 <Stack flexGrow={0} flexShrink={0} overflow="auto">
idillonbef18a52022-09-01 01:51:40 -040096 <Header />
97 <NewContactForm onChange={setSearchQuery} />
simon5da8ca62022-11-09 15:21:25 -050098 {contactId && <AddContactPage 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>
simon9f814a32022-11-22 21:40:53 -0500107 <Box flexGrow={1} display="flex" position="relative">
simonf929a362022-11-18 16:53:45 -0500108 <Outlet />
simon9f814a32022-11-22 21:40:53 -0500109 </Box>
110 </Box>
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;