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 | */ |
Ziwei Wang | a41e166 | 2023-01-27 14:56:32 -0500 | [diff] [blame] | 18 | import SearchRounded from '@mui/icons-material/SearchRounded'; |
idillon | 8fef7db | 2023-01-11 11:03:18 -0500 | [diff] [blame] | 19 | import { InputBase, Stack } from '@mui/material'; |
idillon | 18283ac | 2023-01-07 12:06:42 -0500 | [diff] [blame] | 20 | import { ChangeEvent, useCallback, useEffect, useState } from 'react'; |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame] | 21 | import { useTranslation } from 'react-i18next'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 22 | |
idillon | 18283ac | 2023-01-07 12:06:42 -0500 | [diff] [blame] | 23 | import { useConversationRequestsQuery, useConversationsSummariesQuery } from '../services/conversationQueries'; |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame] | 24 | import { SquareButton } from './Button'; |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 25 | import ContactSearchResultList from './ContactSearchResultList'; |
idillon | 18283ac | 2023-01-07 12:06:42 -0500 | [diff] [blame] | 26 | import { ConversationRequestList } from './ConversationRequestList'; |
idillon | 8fef7db | 2023-01-11 11:03:18 -0500 | [diff] [blame] | 27 | import { ConversationSummaryList } from './ConversationSummaryList'; |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame] | 28 | import { PeopleGroupIcon } from './SvgIcon'; |
idillon | 18283ac | 2023-01-07 12:06:42 -0500 | [diff] [blame] | 29 | import { Tab, TabPanel, TabPanelProps, Tabs, TabsList, TabsProps } from './Tabs'; |
Adrien Béraud | 995e802 | 2021-04-08 13:46:51 -0400 | [diff] [blame] | 30 | |
idillon | 07d31cc | 2022-12-06 22:40:14 -0500 | [diff] [blame] | 31 | export default function ConversationList() { |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame] | 32 | const { t } = useTranslation(); |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame] | 33 | const [searchFilter, setSearchFilter] = useState(''); |
| 34 | |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame] | 35 | const handleInputChange = useCallback((event: ChangeEvent<HTMLInputElement>) => { |
| 36 | setSearchFilter(event.target.value); |
| 37 | }, []); |
ervinanoh | 34eb947 | 2022-09-13 04:20:28 -0400 | [diff] [blame] | 38 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 39 | return ( |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame] | 40 | <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> |
idillon | 2ef2be9 | 2022-12-30 10:59:06 -0500 | [diff] [blame] | 53 | {searchFilter ? <ContactSearchResultList searchFilter={searchFilter} /> : <ConversationTabs />} |
idillon | 847b464 | 2022-12-29 14:28:38 -0500 | [diff] [blame] | 54 | </Stack> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 55 | ); |
Adrien Béraud | 995e802 | 2021-04-08 13:46:51 -0400 | [diff] [blame] | 56 | } |
idillon | 2ef2be9 | 2022-12-30 10:59:06 -0500 | [diff] [blame] | 57 | |
| 58 | const ConversationTabs = () => { |
idillon | 8fef7db | 2023-01-11 11:03:18 -0500 | [diff] [blame] | 59 | const summariesTabIndex = 0; |
idillon | 18283ac | 2023-01-07 12:06:42 -0500 | [diff] [blame] | 60 | const invitationsTabIndex = 1; |
idillon | 2ef2be9 | 2022-12-30 10:59:06 -0500 | [diff] [blame] | 61 | const { t } = useTranslation(); |
idillon | 18283ac | 2023-01-07 12:06:42 -0500 | [diff] [blame] | 62 | const conversationRequestsQuery = useConversationRequestsQuery(); |
| 63 | const conversationRequestsLength = conversationRequestsQuery.data?.length; |
| 64 | |
| 65 | const [isShowingTabMenu, setIsShowingTabMenu] = useState(false); |
idillon | 8fef7db | 2023-01-11 11:03:18 -0500 | [diff] [blame] | 66 | const [tabIndex, setTabIndex] = useState(summariesTabIndex); |
idillon | 18283ac | 2023-01-07 12:06:42 -0500 | [diff] [blame] | 67 | |
| 68 | useEffect(() => { |
| 69 | const newIsShowingTabMenu = !!conversationRequestsLength; |
| 70 | setIsShowingTabMenu(newIsShowingTabMenu); |
| 71 | if (!newIsShowingTabMenu) { |
idillon | 8fef7db | 2023-01-11 11:03:18 -0500 | [diff] [blame] | 72 | setTabIndex(summariesTabIndex); |
idillon | 18283ac | 2023-01-07 12:06:42 -0500 | [diff] [blame] | 73 | } |
| 74 | }, [conversationRequestsLength]); |
| 75 | |
| 76 | const onChange = useCallback<NonNullable<TabsProps['onChange']>>((_event, value) => { |
| 77 | if (typeof value === 'number') { |
| 78 | setTabIndex(value); |
| 79 | } |
| 80 | }, []); |
idillon | 2ef2be9 | 2022-12-30 10:59:06 -0500 | [diff] [blame] | 81 | |
| 82 | return ( |
idillon | 8fef7db | 2023-01-11 11:03:18 -0500 | [diff] [blame] | 83 | <Tabs defaultValue={summariesTabIndex} value={tabIndex} onChange={onChange}> |
idillon | 18283ac | 2023-01-07 12:06:42 -0500 | [diff] [blame] | 84 | {isShowingTabMenu && ( |
idillon | 2ef2be9 | 2022-12-30 10:59:06 -0500 | [diff] [blame] | 85 | <TabsList> |
| 86 | <Tab>{t('conversations')}</Tab> |
idillon | 18283ac | 2023-01-07 12:06:42 -0500 | [diff] [blame] | 87 | <Tab> |
| 88 | {t('invitations')} {conversationRequestsLength} |
| 89 | </Tab> |
idillon | 2ef2be9 | 2022-12-30 10:59:06 -0500 | [diff] [blame] | 90 | </TabsList> |
| 91 | )} |
idillon | 8fef7db | 2023-01-11 11:03:18 -0500 | [diff] [blame] | 92 | <SummariesTabPanel value={summariesTabIndex} /> |
idillon | 18283ac | 2023-01-07 12:06:42 -0500 | [diff] [blame] | 93 | <InvitationsTabPanel value={invitationsTabIndex} /> |
idillon | 2ef2be9 | 2022-12-30 10:59:06 -0500 | [diff] [blame] | 94 | </Tabs> |
| 95 | ); |
| 96 | }; |
| 97 | |
idillon | 8fef7db | 2023-01-11 11:03:18 -0500 | [diff] [blame] | 98 | const SummariesTabPanel = (props: TabPanelProps) => { |
idillon | 2ef2be9 | 2022-12-30 10:59:06 -0500 | [diff] [blame] | 99 | const conversationsSummariesQuery = useConversationsSummariesQuery(); |
idillon | 8fef7db | 2023-01-11 11:03:18 -0500 | [diff] [blame] | 100 | const conversationsSummaries = conversationsSummariesQuery.data || []; |
idillon | 2ef2be9 | 2022-12-30 10:59:06 -0500 | [diff] [blame] | 101 | |
| 102 | return ( |
idillon | 18283ac | 2023-01-07 12:06:42 -0500 | [diff] [blame] | 103 | <TabPanel {...props}> |
idillon | 8fef7db | 2023-01-11 11:03:18 -0500 | [diff] [blame] | 104 | <ConversationSummaryList conversationsSummaries={conversationsSummaries} /> |
idillon | 2ef2be9 | 2022-12-30 10:59:06 -0500 | [diff] [blame] | 105 | </TabPanel> |
| 106 | ); |
| 107 | }; |
| 108 | |
idillon | 18283ac | 2023-01-07 12:06:42 -0500 | [diff] [blame] | 109 | const InvitationsTabPanel = (props: TabPanelProps) => { |
| 110 | return ( |
| 111 | <TabPanel {...props}> |
| 112 | <ConversationRequestList /> |
| 113 | </TabPanel> |
| 114 | ); |
idillon | 2ef2be9 | 2022-12-30 10:59:06 -0500 | [diff] [blame] | 115 | }; |