blob: f2897b39dd1d1e2eb5feaf94affc4550f41eef65 [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 */
Ziwei Wanga41e1662023-01-27 14:56:32 -050018import SearchRounded from '@mui/icons-material/SearchRounded';
idillon8fef7db2023-01-11 11:03:18 -050019import { InputBase, Stack } from '@mui/material';
idillon18283ac2023-01-07 12:06:42 -050020import { ChangeEvent, useCallback, useEffect, useState } from 'react';
idillon847b4642022-12-29 14:28:38 -050021import { useTranslation } from 'react-i18next';
simon07b4eb02022-09-29 17:50:26 -040022
idillon18283ac2023-01-07 12:06:42 -050023import { useConversationRequestsQuery, useConversationsSummariesQuery } from '../services/conversationQueries';
idillon847b4642022-12-29 14:28:38 -050024import { SquareButton } from './Button';
idillon07d31cc2022-12-06 22:40:14 -050025import ContactSearchResultList from './ContactSearchResultList';
idillon18283ac2023-01-07 12:06:42 -050026import { ConversationRequestList } from './ConversationRequestList';
idillon8fef7db2023-01-11 11:03:18 -050027import { ConversationSummaryList } from './ConversationSummaryList';
idillon847b4642022-12-29 14:28:38 -050028import { PeopleGroupIcon } from './SvgIcon';
idillon18283ac2023-01-07 12:06:42 -050029import { Tab, TabPanel, TabPanelProps, Tabs, TabsList, TabsProps } from './Tabs';
Adrien Béraud995e8022021-04-08 13:46:51 -040030
idillon07d31cc2022-12-06 22:40:14 -050031export default function ConversationList() {
idillon847b4642022-12-29 14:28:38 -050032 const { t } = useTranslation();
idillon847b4642022-12-29 14:28:38 -050033 const [searchFilter, setSearchFilter] = useState('');
34
idillon847b4642022-12-29 14:28:38 -050035 const handleInputChange = useCallback((event: ChangeEvent<HTMLInputElement>) => {
36 setSearchFilter(event.target.value);
37 }, []);
ervinanoh34eb9472022-09-13 04:20:28 -040038
simond47ef9e2022-09-28 22:24:28 -040039 return (
idillon847b4642022-12-29 14:28:38 -050040 <Stack>
41 <Stack direction="row">
42 <InputBase
43 type="search"
44 placeholder={t('find_users_and_conversations')}
45 onChange={handleInputChange}
46 startAdornment={<SearchRounded />}
47 sx={{
48 flexGrow: 1,
49 }}
50 />
51 <SquareButton aria-label="start swarm" Icon={PeopleGroupIcon} />
52 </Stack>
idillon2ef2be92022-12-30 10:59:06 -050053 {searchFilter ? <ContactSearchResultList searchFilter={searchFilter} /> : <ConversationTabs />}
idillon847b4642022-12-29 14:28:38 -050054 </Stack>
simond47ef9e2022-09-28 22:24:28 -040055 );
Adrien Béraud995e8022021-04-08 13:46:51 -040056}
idillon2ef2be92022-12-30 10:59:06 -050057
58const ConversationTabs = () => {
idillon8fef7db2023-01-11 11:03:18 -050059 const summariesTabIndex = 0;
idillon18283ac2023-01-07 12:06:42 -050060 const invitationsTabIndex = 1;
idillon2ef2be92022-12-30 10:59:06 -050061 const { t } = useTranslation();
idillon18283ac2023-01-07 12:06:42 -050062 const conversationRequestsQuery = useConversationRequestsQuery();
63 const conversationRequestsLength = conversationRequestsQuery.data?.length;
64
65 const [isShowingTabMenu, setIsShowingTabMenu] = useState(false);
idillon8fef7db2023-01-11 11:03:18 -050066 const [tabIndex, setTabIndex] = useState(summariesTabIndex);
idillon18283ac2023-01-07 12:06:42 -050067
68 useEffect(() => {
69 const newIsShowingTabMenu = !!conversationRequestsLength;
70 setIsShowingTabMenu(newIsShowingTabMenu);
71 if (!newIsShowingTabMenu) {
idillon8fef7db2023-01-11 11:03:18 -050072 setTabIndex(summariesTabIndex);
idillon18283ac2023-01-07 12:06:42 -050073 }
74 }, [conversationRequestsLength]);
75
76 const onChange = useCallback<NonNullable<TabsProps['onChange']>>((_event, value) => {
77 if (typeof value === 'number') {
78 setTabIndex(value);
79 }
80 }, []);
idillon2ef2be92022-12-30 10:59:06 -050081
82 return (
idillon8fef7db2023-01-11 11:03:18 -050083 <Tabs defaultValue={summariesTabIndex} value={tabIndex} onChange={onChange}>
idillon18283ac2023-01-07 12:06:42 -050084 {isShowingTabMenu && (
idillon2ef2be92022-12-30 10:59:06 -050085 <TabsList>
86 <Tab>{t('conversations')}</Tab>
idillon18283ac2023-01-07 12:06:42 -050087 <Tab>
88 {t('invitations')} {conversationRequestsLength}
89 </Tab>
idillon2ef2be92022-12-30 10:59:06 -050090 </TabsList>
91 )}
idillon8fef7db2023-01-11 11:03:18 -050092 <SummariesTabPanel value={summariesTabIndex} />
idillon18283ac2023-01-07 12:06:42 -050093 <InvitationsTabPanel value={invitationsTabIndex} />
idillon2ef2be92022-12-30 10:59:06 -050094 </Tabs>
95 );
96};
97
idillon8fef7db2023-01-11 11:03:18 -050098const SummariesTabPanel = (props: TabPanelProps) => {
idillon2ef2be92022-12-30 10:59:06 -050099 const conversationsSummariesQuery = useConversationsSummariesQuery();
idillon8fef7db2023-01-11 11:03:18 -0500100 const conversationsSummaries = conversationsSummariesQuery.data || [];
idillon2ef2be92022-12-30 10:59:06 -0500101
102 return (
idillon18283ac2023-01-07 12:06:42 -0500103 <TabPanel {...props}>
idillon8fef7db2023-01-11 11:03:18 -0500104 <ConversationSummaryList conversationsSummaries={conversationsSummaries} />
idillon2ef2be92022-12-30 10:59:06 -0500105 </TabPanel>
106 );
107};
108
idillon18283ac2023-01-07 12:06:42 -0500109const InvitationsTabPanel = (props: TabPanelProps) => {
110 return (
111 <TabPanel {...props}>
112 <ConversationRequestList />
113 </TabPanel>
114 );
idillon2ef2be92022-12-30 10:59:06 -0500115};