simon | 26e79f7 | 2022-10-05 22:16:08 -0400 | [diff] [blame] | 1 | /* |
| 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 | */ |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 18 | import { Stack } from '@mui/material'; |
Michelle Sepkap Sime | dd82cbf | 2022-11-17 23:31:49 -0500 | [diff] [blame] | 19 | import { Contact, Conversation, WebSocketMessageType } from 'jami-web-common'; |
| 20 | import { useContext, useEffect, useState } from 'react'; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 21 | import { Outlet } from 'react-router-dom'; |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 22 | |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 23 | //import Sound from 'react-sound'; |
Adrien Béraud | 995e802 | 2021-04-08 13:46:51 -0400 | [diff] [blame] | 24 | import ConversationList from '../components/ConversationList'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 25 | import Header from '../components/Header'; |
simon | 6b9ddfb | 2022-10-03 00:04:50 -0400 | [diff] [blame] | 26 | import LoadingPage from '../components/Loading'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 27 | import NewContactForm from '../components/NewContactForm'; |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 28 | import { useAuthContext } from '../contexts/AuthProvider'; |
Michelle Sepkap Sime | dd82cbf | 2022-11-17 23:31:49 -0500 | [diff] [blame] | 29 | import { WebSocketContext } from '../contexts/WebSocketProvider'; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 30 | import { useUrlParams } from '../hooks/useUrlParams'; |
Michelle Sepkap Sime | dd82cbf | 2022-11-17 23:31:49 -0500 | [diff] [blame] | 31 | import { setRefreshFromSlice } from '../redux/appSlice'; |
| 32 | import { useAppDispatch, useAppSelector } from '../redux/hooks'; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 33 | import { AddContactRouteParams } from '../router'; |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 34 | import AddContactPage from './AddContactPage'; |
ervinanoh | 34eb947 | 2022-09-13 04:20:28 -0400 | [diff] [blame] | 35 | |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 36 | const Messenger = () => { |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 37 | const { refresh } = useAppSelector((state) => state.userInfo); |
Michelle Sepkap Sime | dd82cbf | 2022-11-17 23:31:49 -0500 | [diff] [blame] | 38 | const dispatch = useAppDispatch(); |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 39 | const { account, axiosInstance } = useAuthContext(); |
Michelle Sepkap Sime | dd82cbf | 2022-11-17 23:31:49 -0500 | [diff] [blame] | 40 | const webSocket = useContext(WebSocketContext); |
ervinanoh | 34eb947 | 2022-09-13 04:20:28 -0400 | [diff] [blame] | 41 | |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 42 | const [conversations, setConversations] = useState<Conversation[] | undefined>(undefined); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 43 | const [searchQuery, setSearchQuery] = useState(''); |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 44 | const [searchResult, setSearchResults] = useState<Conversation | undefined>(undefined); |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 45 | |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 46 | const { |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 47 | urlParams: { contactId }, |
| 48 | } = useUrlParams<AddContactRouteParams>(); |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 49 | |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 50 | const accountId = account.getId(); |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 51 | |
Adrien Béraud | 4e287b9 | 2021-04-24 16:15:56 -0400 | [diff] [blame] | 52 | useEffect(() => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 53 | const controller = new AbortController(); |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 54 | axiosInstance |
| 55 | .get<Conversation[]>('/conversations', { |
| 56 | signal: controller.signal, |
| 57 | }) |
| 58 | .then(({ data }) => { |
| 59 | setConversations(Object.values(data).map((c) => Conversation.from(accountId, c))); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 60 | }); |
ervinanoh | 34eb947 | 2022-09-13 04:20:28 -0400 | [diff] [blame] | 61 | // return () => controller.abort() |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 62 | }, [axiosInstance, accountId, refresh]); |
Adrien Béraud | 995e802 | 2021-04-08 13:46:51 -0400 | [diff] [blame] | 63 | |
Adrien Béraud | abba2e5 | 2021-04-24 21:39:56 -0400 | [diff] [blame] | 64 | useEffect(() => { |
Michelle Sepkap Sime | dd82cbf | 2022-11-17 23:31:49 -0500 | [diff] [blame] | 65 | 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(() => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 75 | if (!searchQuery) return; |
| 76 | const controller = new AbortController(); |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 77 | // 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, |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 81 | }) |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 82 | .then(({ data }) => { |
| 83 | const contact = new Contact(data.address); |
| 84 | contact.setRegisteredName(data.username); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 85 | setSearchResults(contact ? Conversation.fromSingleContact(accountId, contact) : undefined); |
| 86 | }) |
simon | 416d079 | 2022-11-03 02:46:18 -0400 | [diff] [blame] | 87 | .catch(() => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 88 | setSearchResults(undefined); |
| 89 | }); |
| 90 | // return () => controller.abort() // crash on React18 |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 91 | }, [accountId, searchQuery, axiosInstance]); |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 92 | |
Adrien Béraud | 4e287b9 | 2021-04-24 16:15:56 -0400 | [diff] [blame] | 93 | return ( |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 94 | <Stack direction="row" height="100vh" width="100vw"> |
| 95 | <Stack flexGrow={0} flexShrink={0} overflow="auto"> |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 96 | <Header /> |
| 97 | <NewContactForm onChange={setSearchQuery} /> |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 98 | {contactId && <AddContactPage contactId={contactId} />} |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 99 | {conversations ? ( |
| 100 | <ConversationList search={searchResult} conversations={conversations} accountId={accountId} /> |
| 101 | ) : ( |
| 102 | <div className="rooms-list"> |
| 103 | <LoadingPage /> |
| 104 | </div> |
| 105 | )} |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 106 | </Stack> |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 107 | <Stack flexGrow={1}> |
| 108 | <Outlet /> |
| 109 | </Stack> |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 110 | </Stack> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 111 | ); |
| 112 | }; |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 113 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 114 | export default Messenger; |