blob: 332011007ddf1d87898fbf87baa4dea2a7178272 [file] [log] [blame]
simon26e79f72022-10-05 22:16:08 -04001/*
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 { GroupRounded as GroupIcon, SearchRounded } from '@mui/icons-material';
19import { InputBase } from '@mui/material';
simon07b4eb02022-09-29 17:50:26 -040020import List from '@mui/material/List';
21import ListSubheader from '@mui/material/ListSubheader';
Adrien Béraudab519ff2022-05-03 15:34:48 -040022import Typography from '@mui/material/Typography';
idillon847b4642022-12-29 14:28:38 -050023import { Stack } from '@mui/system';
24import { ChangeEvent, useCallback, useState } from 'react';
25import { useTranslation } from 'react-i18next';
simon07b4eb02022-09-29 17:50:26 -040026
idillon847b4642022-12-29 14:28:38 -050027import { useContactsSearchQuery } from '../services/contactQueries';
28import { useConversationsSummariesQuery } from '../services/conversationQueries';
29import { SquareButton } from './Button';
idillon07d31cc2022-12-06 22:40:14 -050030import ContactSearchResultList from './ContactSearchResultList';
simon07b4eb02022-09-29 17:50:26 -040031import ConversationListItem from './ConversationListItem';
idillon07d31cc2022-12-06 22:40:14 -050032import LoadingPage from './Loading';
idillon847b4642022-12-29 14:28:38 -050033import { PeopleGroupIcon } from './SvgIcon';
Adrien Béraud995e8022021-04-08 13:46:51 -040034
idillon07d31cc2022-12-06 22:40:14 -050035export default function ConversationList() {
idillon847b4642022-12-29 14:28:38 -050036 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 }, []);
ervinanoh34eb9472022-09-13 04:20:28 -040049
idillon07d31cc2022-12-06 22:40:14 -050050 if (!conversationsSummaries) {
51 return <LoadingPage />;
52 }
ervinanoh34eb9472022-09-13 04:20:28 -040053
simond47ef9e2022-09-28 22:24:28 -040054 return (
idillon847b4642022-12-29 14:28:38 -050055 <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>
simond47ef9e2022-09-28 22:24:28 -040068 <List>
idillon847b4642022-12-29 14:28:38 -050069 {contactsSearchResult && contactsSearchResult.length > 0 && (
70 <>
71 <ListSubheader>{t('search_results')}</ListSubheader>
72 <ContactSearchResultList contacts={contactsSearchResult} />
73 <ListSubheader>{t('conversations')}</ListSubheader>
74 </>
simond47ef9e2022-09-28 22:24:28 -040075 )}
idillon07d31cc2022-12-06 22:40:14 -050076 {conversationsSummaries.map((conversationSummary) => (
77 <ConversationListItem key={conversationSummary.id} conversationSummary={conversationSummary} />
simond47ef9e2022-09-28 22:24:28 -040078 ))}
idillon07d31cc2022-12-06 22:40:14 -050079 {conversationsSummaries.length === 0 && (
idillon847b4642022-12-29 14:28:38 -050080 <Stack>
simond47ef9e2022-09-28 22:24:28 -040081 <GroupIcon color="disabled" fontSize="large" />
idillon847b4642022-12-29 14:28:38 -050082 <Typography variant="subtitle2">{t('no_conversations')}</Typography>
83 </Stack>
simond47ef9e2022-09-28 22:24:28 -040084 )}
85 </List>
idillon847b4642022-12-29 14:28:38 -050086 </Stack>
simond47ef9e2022-09-28 22:24:28 -040087 );
Adrien Béraud995e8022021-04-08 13:46:51 -040088}