blob: c2bc1e7278f28d457652124d155a4b856ef83c38 [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';
Misha Krieger-Raynauld20cf1c82022-11-23 20:26:50 -050019import { Contact, Conversation, ConversationMessage, WebSocketMessageType } from 'jami-web-common';
Michelle Sepkap Simedd82cbf2022-11-17 23:31:49 -050020import { 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
Misha Krieger-Raynauld20cf1c82022-11-23 20:26:50 -050069 const conversationMessageListener = (_data: ConversationMessage) => {
70 dispatch(setRefreshFromSlice());
71 };
72
Michelle Sepkap Simedd82cbf2022-11-17 23:31:49 -050073 webSocket.bind(WebSocketMessageType.ConversationMessage, conversationMessageListener);
Misha Krieger-Raynauld20cf1c82022-11-23 20:26:50 -050074
75 return () => {
76 webSocket.unbind(WebSocketMessageType.ConversationMessage, conversationMessageListener);
77 };
Michelle Sepkap Simedd82cbf2022-11-17 23:31:49 -050078 }, [webSocket, dispatch]);
79
80 useEffect(() => {
simond47ef9e2022-09-28 22:24:28 -040081 if (!searchQuery) return;
82 const controller = new AbortController();
simon94fe53e2022-11-10 12:51:58 -050083 // TODO: Type properly https://git.jami.net/savoirfairelinux/jami-web/-/issues/92
84 axiosInstance
85 .get<{ state: number; address: string; username: string }>(`/ns/username/${searchQuery}`, {
86 signal: controller.signal,
simond47ef9e2022-09-28 22:24:28 -040087 })
simon94fe53e2022-11-10 12:51:58 -050088 .then(({ data }) => {
89 const contact = new Contact(data.address);
90 contact.setRegisteredName(data.username);
simond47ef9e2022-09-28 22:24:28 -040091 setSearchResults(contact ? Conversation.fromSingleContact(accountId, contact) : undefined);
92 })
simon416d0792022-11-03 02:46:18 -040093 .catch(() => {
simond47ef9e2022-09-28 22:24:28 -040094 setSearchResults(undefined);
95 });
96 // return () => controller.abort() // crash on React18
simon94fe53e2022-11-10 12:51:58 -050097 }, [accountId, searchQuery, axiosInstance]);
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040098
Adrien Béraud4e287b92021-04-24 16:15:56 -040099 return (
simon9f814a32022-11-22 21:40:53 -0500100 <Box display="flex" height="100%">
simond47ef9e2022-09-28 22:24:28 -0400101 <Stack flexGrow={0} flexShrink={0} overflow="auto">
idillonbef18a52022-09-01 01:51:40 -0400102 <Header />
103 <NewContactForm onChange={setSearchQuery} />
simon5da8ca62022-11-09 15:21:25 -0500104 {contactId && <AddContactPage contactId={contactId} />}
simond47ef9e2022-09-28 22:24:28 -0400105 {conversations ? (
106 <ConversationList search={searchResult} conversations={conversations} accountId={accountId} />
107 ) : (
108 <div className="rooms-list">
109 <LoadingPage />
110 </div>
111 )}
idillonbef18a52022-09-01 01:51:40 -0400112 </Stack>
simon9f814a32022-11-22 21:40:53 -0500113 <Box flexGrow={1} display="flex" position="relative">
simonf929a362022-11-18 16:53:45 -0500114 <Outlet />
simon9f814a32022-11-22 21:40:53 -0500115 </Box>
116 </Box>
simond47ef9e2022-09-28 22:24:28 -0400117 );
118};
Larbi Gharibe9af9732021-03-31 15:08:01 +0100119
simond47ef9e2022-09-28 22:24:28 -0400120export default Messenger;