blob: fbe988d0b78541b679ee189d34b9bcf4b4fdef26 [file] [log] [blame]
idillon07d31cc2022-12-06 22:40:14 -05001/*
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 */
idillon847b4642022-12-29 14:28:38 -050018import { GroupAddRounded } from '@mui/icons-material';
19import { Box, Dialog, DialogProps, Fab, List, Typography } from '@mui/material';
20import { useTranslation } from 'react-i18next';
21import { useNavigate } from 'react-router-dom';
idillon07d31cc2022-12-06 22:40:14 -050022
23import { Contact } from '../models/contact';
idillon847b4642022-12-29 14:28:38 -050024import { useAddContactMutation } from '../services/contactQueries';
idillon07d31cc2022-12-06 22:40:14 -050025import ConversationAvatar from './ConversationAvatar';
idillon847b4642022-12-29 14:28:38 -050026import { CustomListItemButton } from './CustomListItemButton';
idillon07d31cc2022-12-06 22:40:14 -050027import { useDialogHandler } from './Dialog';
28
29type ContactSearchResultListProps = {
30 contacts: Contact[];
31};
32
33export default ({ contacts }: ContactSearchResultListProps) => {
34 return (
35 <List>
36 {contacts?.map((contact) => (
37 <ContactSearchResultListItem key={contact.uri} contact={contact} />
38 ))}
39 </List>
40 );
41};
42
43type ContactSearchResultListItemProps = {
44 contact: Contact;
45};
46
47const ContactSearchResultListItem = ({ contact }: ContactSearchResultListItemProps) => {
48 const dialogHandler = useDialogHandler();
49
50 return (
51 <>
52 <AddContactDialog {...dialogHandler.props} contactId={contact.uri} />
idillon847b4642022-12-29 14:28:38 -050053 <CustomListItemButton
idillon07d31cc2022-12-06 22:40:14 -050054 key={contact.uri}
55 onClick={() => {
56 dialogHandler.openDialog();
57 }}
idillon847b4642022-12-29 14:28:38 -050058 icon={<ConversationAvatar displayName={contact.getDisplayName()} />}
59 primaryText={<Typography variant="body1">{contact.getDisplayName()}</Typography>}
60 />
idillon07d31cc2022-12-06 22:40:14 -050061 </>
62 );
63};
64
65type AddContactDialogProps = DialogProps & {
66 contactId: string;
67};
68
69const AddContactDialog = ({ contactId, ...props }: AddContactDialogProps) => {
idillon847b4642022-12-29 14:28:38 -050070 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
idillon07d31cc2022-12-06 22:40:14 -050081 return (
82 <Dialog {...props}>
idillon847b4642022-12-29 14:28:38 -050083 <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>
idillon07d31cc2022-12-06 22:40:14 -050091 </Dialog>
92 );
93};