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'; |
simon | 2007698 | 2022-10-11 15:04:13 -0400 | [diff] [blame] | 19 | import { Contact, Conversation } from 'jami-web-common'; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 20 | import { useEffect, useState } from 'react'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 21 | import { useParams } from 'react-router'; |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 22 | |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 23 | import authManager from '../AuthManager'; |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 24 | //import Sound from 'react-sound'; |
Adrien Béraud | 995e802 | 2021-04-08 13:46:51 -0400 | [diff] [blame] | 25 | import ConversationList from '../components/ConversationList'; |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 26 | import ConversationView from '../components/ConversationView'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 27 | import Header from '../components/Header'; |
simon | 6b9ddfb | 2022-10-03 00:04:50 -0400 | [diff] [blame] | 28 | import LoadingPage from '../components/Loading'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 29 | import NewContactForm from '../components/NewContactForm'; |
simon | d8ca2f2 | 2022-10-11 23:30:55 -0400 | [diff] [blame] | 30 | import { useAppSelector } from '../redux/hooks'; |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 31 | import AddContactPage from './AddContactPage'; |
ervinanoh | 34eb947 | 2022-09-13 04:20:28 -0400 | [diff] [blame] | 32 | |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 33 | type MessengerProps = { |
| 34 | accountId?: string; |
| 35 | conversationId?: string; |
| 36 | contactId?: string; |
| 37 | }; |
| 38 | |
| 39 | const Messenger = (props: MessengerProps) => { |
ervinanoh | 34eb947 | 2022-09-13 04:20:28 -0400 | [diff] [blame] | 40 | const { refresh } = useAppSelector((state) => state.app); |
| 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 | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 46 | const params = useParams(); |
| 47 | const accountId = props.accountId || params.accountId; |
| 48 | const conversationId = props.conversationId || params.conversationId; |
| 49 | const contactId = props.contactId || params.contactId; |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 50 | |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 51 | if (accountId == null) { |
| 52 | throw new Error('Missing accountId'); |
| 53 | } |
| 54 | |
Adrien Béraud | 4e287b9 | 2021-04-24 16:15:56 -0400 | [diff] [blame] | 55 | useEffect(() => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 56 | console.log('REFRESH CONVERSATIONS FROM MESSENGER'); |
| 57 | const controller = new AbortController(); |
| 58 | authManager |
| 59 | .fetch(`/api/accounts/${accountId}/conversations`, { signal: controller.signal }) |
| 60 | .then((res) => res.json()) |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 61 | .then((result: Conversation[]) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 62 | console.log(result); |
| 63 | setConversations(Object.values(result).map((c) => Conversation.from(accountId, c))); |
| 64 | }); |
ervinanoh | 34eb947 | 2022-09-13 04:20:28 -0400 | [diff] [blame] | 65 | // return () => controller.abort() |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 66 | }, [accountId, refresh]); |
Adrien Béraud | 995e802 | 2021-04-08 13:46:51 -0400 | [diff] [blame] | 67 | |
Adrien Béraud | abba2e5 | 2021-04-24 21:39:56 -0400 | [diff] [blame] | 68 | useEffect(() => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 69 | if (!searchQuery) return; |
| 70 | const controller = new AbortController(); |
| 71 | authManager |
| 72 | .fetch(`/api/accounts/${accountId}/ns/name/${searchQuery}`, { signal: controller.signal }) |
| 73 | .then((response) => { |
| 74 | if (response.status === 200) { |
| 75 | return response.json(); |
| 76 | } else { |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 77 | throw new Error(response.status.toString()); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 78 | } |
| 79 | }) |
| 80 | .then((response) => { |
| 81 | console.log(response); |
| 82 | const contact = new Contact(response.address); |
| 83 | contact.setRegisteredName(response.name); |
| 84 | setSearchResults(contact ? Conversation.fromSingleContact(accountId, contact) : undefined); |
| 85 | }) |
| 86 | .catch((e) => { |
| 87 | setSearchResults(undefined); |
| 88 | }); |
| 89 | // return () => controller.abort() // crash on React18 |
| 90 | }, [accountId, searchQuery]); |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 91 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 92 | console.log('Messenger render'); |
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} /> |
| 98 | {contactId && <AddContactPage accountId={accountId} 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 | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 107 | <Stack flexGrow={1}> |
idillon | bef18a5 | 2022-09-01 01:51:40 -0400 | [diff] [blame] | 108 | {conversationId && <ConversationView accountId={accountId} conversationId={conversationId} />} |
| 109 | </Stack> |
| 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; |