idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [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 | */ |
Ziwei Wang | a41e166 | 2023-01-27 14:56:32 -0500 | [diff] [blame] | 18 | import GroupAddRounded from '@mui/icons-material/GroupAddRounded'; |
idillon | 2ef2be9 | 2022-12-30 10:59:06 -0500 | [diff] [blame] | 19 | import { Box, Dialog, DialogProps, Fab, List, ListSubheader, Typography } from '@mui/material'; |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame] | 20 | import { useTranslation } from 'react-i18next'; |
| 21 | import { useNavigate } from 'react-router-dom'; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 22 | |
| 23 | import { Contact } from '../models/contact'; |
idillon | 2ef2be9 | 2022-12-30 10:59:06 -0500 | [diff] [blame] | 24 | import { useAddContactMutation, useContactsSearchQuery } from '../services/contactQueries'; |
| 25 | import { useConversationsSummariesQuery } from '../services/conversationQueries'; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 26 | import ConversationAvatar from './ConversationAvatar'; |
idillon | 8fef7db | 2023-01-11 11:03:18 -0500 | [diff] [blame] | 27 | import { ConversationSummaryList } from './ConversationSummaryList'; |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame] | 28 | import { CustomListItemButton } from './CustomListItemButton'; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 29 | import { useDialogHandler } from './Dialog'; |
idillon | 2ef2be9 | 2022-12-30 10:59:06 -0500 | [diff] [blame] | 30 | import LoadingPage from './Loading'; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 31 | |
| 32 | type ContactSearchResultListProps = { |
idillon | 2ef2be9 | 2022-12-30 10:59:06 -0500 | [diff] [blame] | 33 | searchFilter: string; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 34 | }; |
| 35 | |
idillon | 2ef2be9 | 2022-12-30 10:59:06 -0500 | [diff] [blame] | 36 | export default ({ searchFilter }: ContactSearchResultListProps) => { |
| 37 | const { t } = useTranslation(); |
| 38 | |
| 39 | const contactsSearchQuery = useContactsSearchQuery(searchFilter); |
| 40 | // TODO: Filter conversations |
| 41 | const conversationsSummariesQuery = useConversationsSummariesQuery(); |
| 42 | |
| 43 | const isLoading = contactsSearchQuery.isLoading && conversationsSummariesQuery.isLoading; |
| 44 | |
| 45 | if (isLoading) { |
| 46 | return <LoadingPage />; |
| 47 | } |
| 48 | |
| 49 | const conversationsSummaries = conversationsSummariesQuery.data; |
| 50 | const contactsSearchResult = contactsSearchQuery.data; |
| 51 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 52 | return ( |
| 53 | <List> |
idillon | 2ef2be9 | 2022-12-30 10:59:06 -0500 | [diff] [blame] | 54 | {contactsSearchResult && contactsSearchResult.length > 0 && ( |
| 55 | <> |
| 56 | <ListSubheader>{t('search_results')}</ListSubheader> |
| 57 | {contactsSearchResult?.map((contact) => ( |
| 58 | <ContactSearchResultListItem key={contact.uri} contact={contact} /> |
| 59 | ))} |
| 60 | </> |
| 61 | )} |
| 62 | |
| 63 | {conversationsSummaries && conversationsSummaries.length > 0 && ( |
| 64 | <> |
| 65 | <ListSubheader>{t('conversations')}</ListSubheader> |
idillon | 8fef7db | 2023-01-11 11:03:18 -0500 | [diff] [blame] | 66 | <ConversationSummaryList conversationsSummaries={conversationsSummaries} /> |
idillon | 2ef2be9 | 2022-12-30 10:59:06 -0500 | [diff] [blame] | 67 | </> |
| 68 | )} |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 69 | </List> |
| 70 | ); |
| 71 | }; |
| 72 | |
| 73 | type ContactSearchResultListItemProps = { |
| 74 | contact: Contact; |
| 75 | }; |
| 76 | |
| 77 | const ContactSearchResultListItem = ({ contact }: ContactSearchResultListItemProps) => { |
| 78 | const dialogHandler = useDialogHandler(); |
| 79 | |
| 80 | return ( |
| 81 | <> |
| 82 | <AddContactDialog {...dialogHandler.props} contactId={contact.uri} /> |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame] | 83 | <CustomListItemButton |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 84 | key={contact.uri} |
| 85 | onClick={() => { |
| 86 | dialogHandler.openDialog(); |
| 87 | }} |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame] | 88 | icon={<ConversationAvatar displayName={contact.getDisplayName()} />} |
| 89 | primaryText={<Typography variant="body1">{contact.getDisplayName()}</Typography>} |
| 90 | /> |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 91 | </> |
| 92 | ); |
| 93 | }; |
| 94 | |
| 95 | type AddContactDialogProps = DialogProps & { |
| 96 | contactId: string; |
| 97 | }; |
| 98 | |
| 99 | const AddContactDialog = ({ contactId, ...props }: AddContactDialogProps) => { |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame] | 100 | const { t } = useTranslation(); |
| 101 | const navigate = useNavigate(); |
| 102 | const addContactMutation = useAddContactMutation(); |
| 103 | |
| 104 | const handleClick = async () => { |
| 105 | addContactMutation.mutate(contactId, { |
| 106 | onSuccess: (data) => navigate(`/conversation/${data.conversationId}`), |
| 107 | onSettled: () => props.onClose?.({}, 'escapeKeyDown'), // dummy arguments for 'onClose' |
| 108 | }); |
| 109 | }; |
| 110 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 111 | return ( |
| 112 | <Dialog {...props}> |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame] | 113 | <Typography variant="h6">{t('jami_user_id')}</Typography> |
| 114 | <Typography variant="body1">{contactId}</Typography> |
| 115 | <Box style={{ textAlign: 'center', marginTop: 16 }}> |
| 116 | <Fab variant="extended" color="primary" onClick={handleClick}> |
| 117 | <GroupAddRounded /> |
| 118 | {t('conversation_add_contact')} |
| 119 | </Fab> |
| 120 | </Box> |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 121 | </Dialog> |
| 122 | ); |
| 123 | }; |