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 | */ |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame^] | 18 | import { GroupAddRounded } from '@mui/icons-material'; |
| 19 | import { Box, Dialog, DialogProps, Fab, List, Typography } from '@mui/material'; |
| 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 | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame^] | 24 | import { useAddContactMutation } from '../services/contactQueries'; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 25 | import ConversationAvatar from './ConversationAvatar'; |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame^] | 26 | import { CustomListItemButton } from './CustomListItemButton'; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 27 | import { useDialogHandler } from './Dialog'; |
| 28 | |
| 29 | type ContactSearchResultListProps = { |
| 30 | contacts: Contact[]; |
| 31 | }; |
| 32 | |
| 33 | export default ({ contacts }: ContactSearchResultListProps) => { |
| 34 | return ( |
| 35 | <List> |
| 36 | {contacts?.map((contact) => ( |
| 37 | <ContactSearchResultListItem key={contact.uri} contact={contact} /> |
| 38 | ))} |
| 39 | </List> |
| 40 | ); |
| 41 | }; |
| 42 | |
| 43 | type ContactSearchResultListItemProps = { |
| 44 | contact: Contact; |
| 45 | }; |
| 46 | |
| 47 | const ContactSearchResultListItem = ({ contact }: ContactSearchResultListItemProps) => { |
| 48 | const dialogHandler = useDialogHandler(); |
| 49 | |
| 50 | return ( |
| 51 | <> |
| 52 | <AddContactDialog {...dialogHandler.props} contactId={contact.uri} /> |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame^] | 53 | <CustomListItemButton |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 54 | key={contact.uri} |
| 55 | onClick={() => { |
| 56 | dialogHandler.openDialog(); |
| 57 | }} |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame^] | 58 | icon={<ConversationAvatar displayName={contact.getDisplayName()} />} |
| 59 | primaryText={<Typography variant="body1">{contact.getDisplayName()}</Typography>} |
| 60 | /> |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 61 | </> |
| 62 | ); |
| 63 | }; |
| 64 | |
| 65 | type AddContactDialogProps = DialogProps & { |
| 66 | contactId: string; |
| 67 | }; |
| 68 | |
| 69 | const AddContactDialog = ({ contactId, ...props }: AddContactDialogProps) => { |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame^] | 70 | const { t } = useTranslation(); |
| 71 | const navigate = useNavigate(); |
| 72 | const addContactMutation = useAddContactMutation(); |
| 73 | |
| 74 | const handleClick = async () => { |
| 75 | addContactMutation.mutate(contactId, { |
| 76 | onSuccess: (data) => navigate(`/conversation/${data.conversationId}`), |
| 77 | onSettled: () => props.onClose?.({}, 'escapeKeyDown'), // dummy arguments for 'onClose' |
| 78 | }); |
| 79 | }; |
| 80 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 81 | return ( |
| 82 | <Dialog {...props}> |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame^] | 83 | <Typography variant="h6">{t('jami_user_id')}</Typography> |
| 84 | <Typography variant="body1">{contactId}</Typography> |
| 85 | <Box style={{ textAlign: 'center', marginTop: 16 }}> |
| 86 | <Fab variant="extended" color="primary" onClick={handleClick}> |
| 87 | <GroupAddRounded /> |
| 88 | {t('conversation_add_contact')} |
| 89 | </Fab> |
| 90 | </Box> |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 91 | </Dialog> |
| 92 | ); |
| 93 | }; |