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 | */ |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame^] | 18 | import { GroupRounded as GroupIcon, SearchRounded } from '@mui/icons-material'; |
| 19 | import { InputBase } from '@mui/material'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 20 | import List from '@mui/material/List'; |
| 21 | import ListSubheader from '@mui/material/ListSubheader'; |
Adrien Béraud | ab519ff | 2022-05-03 15:34:48 -0400 | [diff] [blame] | 22 | import Typography from '@mui/material/Typography'; |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame^] | 23 | import { Stack } from '@mui/system'; |
| 24 | import { ChangeEvent, useCallback, useState } from 'react'; |
| 25 | import { useTranslation } from 'react-i18next'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 26 | |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame^] | 27 | import { useContactsSearchQuery } from '../services/contactQueries'; |
| 28 | import { useConversationsSummariesQuery } from '../services/conversationQueries'; |
| 29 | import { SquareButton } from './Button'; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 30 | import ContactSearchResultList from './ContactSearchResultList'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 31 | import ConversationListItem from './ConversationListItem'; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 32 | import LoadingPage from './Loading'; |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame^] | 33 | import { PeopleGroupIcon } from './SvgIcon'; |
Adrien Béraud | 995e802 | 2021-04-08 13:46:51 -0400 | [diff] [blame] | 34 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 35 | export default function ConversationList() { |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame^] | 36 | const { t } = useTranslation(); |
| 37 | |
| 38 | const [searchFilter, setSearchFilter] = useState(''); |
| 39 | |
| 40 | const conversationsSummariesQuery = useConversationsSummariesQuery(); |
| 41 | const contactsSearchQuery = useContactsSearchQuery(searchFilter); |
| 42 | |
| 43 | const conversationsSummaries = conversationsSummariesQuery.data; |
| 44 | const contactsSearchResult = contactsSearchQuery.data; |
| 45 | |
| 46 | const handleInputChange = useCallback((event: ChangeEvent<HTMLInputElement>) => { |
| 47 | setSearchFilter(event.target.value); |
| 48 | }, []); |
ervinanoh | 34eb947 | 2022-09-13 04:20:28 -0400 | [diff] [blame] | 49 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 50 | if (!conversationsSummaries) { |
| 51 | return <LoadingPage />; |
| 52 | } |
ervinanoh | 34eb947 | 2022-09-13 04:20:28 -0400 | [diff] [blame] | 53 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 54 | return ( |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame^] | 55 | <Stack> |
| 56 | <Stack direction="row"> |
| 57 | <InputBase |
| 58 | type="search" |
| 59 | placeholder={t('find_users_and_conversations')} |
| 60 | onChange={handleInputChange} |
| 61 | startAdornment={<SearchRounded />} |
| 62 | sx={{ |
| 63 | flexGrow: 1, |
| 64 | }} |
| 65 | /> |
| 66 | <SquareButton aria-label="start swarm" Icon={PeopleGroupIcon} /> |
| 67 | </Stack> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 68 | <List> |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame^] | 69 | {contactsSearchResult && contactsSearchResult.length > 0 && ( |
| 70 | <> |
| 71 | <ListSubheader>{t('search_results')}</ListSubheader> |
| 72 | <ContactSearchResultList contacts={contactsSearchResult} /> |
| 73 | <ListSubheader>{t('conversations')}</ListSubheader> |
| 74 | </> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 75 | )} |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 76 | {conversationsSummaries.map((conversationSummary) => ( |
| 77 | <ConversationListItem key={conversationSummary.id} conversationSummary={conversationSummary} /> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 78 | ))} |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 79 | {conversationsSummaries.length === 0 && ( |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame^] | 80 | <Stack> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 81 | <GroupIcon color="disabled" fontSize="large" /> |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame^] | 82 | <Typography variant="subtitle2">{t('no_conversations')}</Typography> |
| 83 | </Stack> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 84 | )} |
| 85 | </List> |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame^] | 86 | </Stack> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 87 | ); |
Adrien Béraud | 995e802 | 2021-04-08 13:46:51 -0400 | [diff] [blame] | 88 | } |