tooot/src/screens/Tabs/Shared/Account.tsx

180 lines
6.2 KiB
TypeScript
Raw Normal View History

2022-12-03 01:08:38 +01:00
import menuAccount from '@components/contextMenu/account'
import menuShare from '@components/contextMenu/share'
2022-12-03 16:50:54 +01:00
import { HeaderLeft, HeaderRight } from '@components/Header'
2021-02-08 23:47:20 +01:00
import Timeline from '@components/Timeline'
2021-02-27 16:33:54 +01:00
import TimelineDefault from '@components/Timeline/Default'
2021-06-01 22:27:27 +02:00
import SegmentedControl from '@react-native-community/segmented-control'
2021-08-29 15:25:38 +02:00
import { TabSharedStackScreenProps } from '@utils/navigation/navigators'
2021-01-11 21:36:57 +01:00
import { useAccountQuery } from '@utils/queryHooks/account'
2021-02-27 16:33:54 +01:00
import { QueryKeyTimeline } from '@utils/queryHooks/timeline'
2021-06-01 22:27:27 +02:00
import { StyleConstants } from '@utils/styles/constants'
2021-01-16 00:00:31 +01:00
import { useTheme } from '@utils/styles/ThemeManager'
2022-11-06 23:39:31 +01:00
import React, { useEffect, useMemo, useRef, useState } from 'react'
2021-04-09 21:43:12 +02:00
import { useTranslation } from 'react-i18next'
2022-12-03 15:50:15 +01:00
import { Text, View } from 'react-native'
2021-01-15 01:15:27 +01:00
import { useSharedValue } from 'react-native-reanimated'
import { useIsFetching } from '@tanstack/react-query'
2022-12-03 01:08:38 +01:00
import * as DropdownMenu from 'zeego/dropdown-menu'
2021-01-16 00:00:31 +01:00
import AccountAttachments from './Account/Attachments'
import AccountHeader from './Account/Header'
import AccountInformation from './Account/Information'
import AccountNav from './Account/Nav'
2020-10-28 00:02:37 +01:00
2022-11-06 23:39:31 +01:00
const TabSharedAccount: React.FC<TabSharedStackScreenProps<'Tab-Shared-Account'>> = ({
2022-12-03 01:08:38 +01:00
navigation,
2020-10-26 00:27:53 +01:00
route: {
2020-12-21 21:47:15 +01:00
params: { account }
2022-06-07 20:07:14 +02:00
}
2020-10-31 21:04:46 +01:00
}) => {
2021-04-09 21:43:12 +02:00
const { t, i18n } = useTranslation('screenTabs')
2022-02-12 14:51:01 +01:00
const { colors, mode } = useTheme()
2021-01-16 00:00:31 +01:00
2022-12-03 01:08:38 +01:00
const mShare = menuShare({ type: 'account', url: account.url })
2022-12-03 15:50:15 +01:00
const mAccount = menuAccount({ type: 'account', openChange: true, account })
2022-12-03 01:08:38 +01:00
useEffect(() => {
navigation.setOptions({
2022-12-03 16:50:54 +01:00
headerTransparent: true,
headerStyle: {
backgroundColor: `rgba(255, 255, 255, 0)`
},
title: '',
headerLeft: () => <HeaderLeft onPress={() => navigation.goBack()} background />,
2022-12-03 01:08:38 +01:00
headerRight: () => {
return (
<DropdownMenu.Root>
<DropdownMenu.Trigger>
<HeaderRight
accessibilityLabel={t('shared.account.actions.accessibilityLabel', {
user: account.acct
})}
accessibilityHint={t('shared.account.actions.accessibilityHint')}
content='MoreHorizontal'
onPress={() => {}}
background
/>
</DropdownMenu.Trigger>
<DropdownMenu.Content>
{mShare.map((mGroup, index) => (
<DropdownMenu.Group key={index}>
{mGroup.map(menu => (
<DropdownMenu.Item key={menu.key} {...menu.item}>
<DropdownMenu.ItemTitle children={menu.title} />
<DropdownMenu.ItemIcon iosIconName={menu.icon} />
</DropdownMenu.Item>
))}
</DropdownMenu.Group>
))}
{mAccount.map((mGroup, index) => (
<DropdownMenu.Group key={index}>
{mGroup.map(menu => (
<DropdownMenu.Item key={menu.key} {...menu.item}>
<DropdownMenu.ItemTitle children={menu.title} />
<DropdownMenu.ItemIcon iosIconName={menu.icon} />
</DropdownMenu.Item>
))}
</DropdownMenu.Group>
))}
</DropdownMenu.Content>
</DropdownMenu.Root>
)
}
})
2022-12-04 16:16:53 +01:00
}, [mAccount])
2022-12-03 01:08:38 +01:00
2021-01-11 21:36:57 +01:00
const { data } = useAccountQuery({ id: account.id })
const scrollY = useSharedValue(0)
2020-10-26 00:27:53 +01:00
2021-06-01 22:27:27 +02:00
const [queryKey, setQueryKey] = useState<QueryKeyTimeline>([
2021-02-27 16:33:54 +01:00
'Timeline',
{ page: 'Account_Default', account: account.id }
2021-06-01 22:27:27 +02:00
])
const isFetchingTimeline = useIsFetching(queryKey)
const fetchedTimeline = useRef(false)
useEffect(() => {
if (!isFetchingTimeline && !fetchedTimeline.current) {
fetchedTimeline.current = true
}
}, [isFetchingTimeline, fetchedTimeline.current])
const ListHeaderComponent = useMemo(() => {
return (
<>
2022-12-03 15:50:15 +01:00
<View style={{ borderBottomWidth: 1, borderBottomColor: colors.border }}>
2021-06-01 22:27:27 +02:00
<AccountHeader account={data} />
<AccountInformation account={data} />
2022-11-06 23:39:31 +01:00
{!data?.suspended && fetchedTimeline.current ? (
2021-06-01 22:27:27 +02:00
<AccountAttachments account={data} />
) : null}
</View>
2022-11-06 23:39:31 +01:00
{!data?.suspended ? (
<SegmentedControl
appearance={mode}
values={[t('shared.account.toots.default'), t('shared.account.toots.all')]}
selectedIndex={queryKey[1].page === 'Account_Default' ? 0 : 1}
onChange={({ nativeEvent }) => {
switch (nativeEvent.selectedSegmentIndex) {
case 0:
setQueryKey([queryKey[0], { ...queryKey[1], page: 'Account_Default' }])
break
case 1:
setQueryKey([queryKey[0], { ...queryKey[1], page: 'Account_All' }])
break
}
}}
2022-12-03 15:50:15 +01:00
style={{
marginTop: StyleConstants.Spacing.M,
marginHorizontal: StyleConstants.Spacing.Global.PagePadding
}}
2022-11-06 23:39:31 +01:00
/>
) : null}
{data?.suspended ? (
<View
style={{
flex: 1,
justifyContent: 'center',
alignItems: 'center',
paddingHorizontal: StyleConstants.Spacing.Global.PagePadding
}}
>
2022-12-03 01:08:38 +01:00
<Text
style={{
...StyleConstants.FontStyle.M,
color: colors.secondary,
textAlign: 'center'
}}
>
2022-11-06 23:39:31 +01:00
{t('shared.account.suspended')}
</Text>
</View>
) : null}
2021-06-01 22:27:27 +02:00
</>
)
2021-06-02 22:27:51 +02:00
}, [data, fetchedTimeline.current, queryKey[1].page, i18n.language, mode])
2021-02-27 16:33:54 +01:00
2020-11-22 00:46:23 +01:00
return (
2021-05-09 21:59:03 +02:00
<>
<AccountNav scrollY={scrollY} account={data} />
2021-01-15 01:15:27 +01:00
2022-11-06 23:39:31 +01:00
{data?.suspended ? (
ListHeaderComponent
) : (
<Timeline
queryKey={queryKey}
disableRefresh
customProps={{
renderItem: ({ item }) => <TimelineDefault item={item} queryKey={queryKey} />,
onScroll: ({ nativeEvent }) => (scrollY.value = nativeEvent.contentOffset.y),
ListHeaderComponent,
maintainVisibleContentPosition: undefined
}}
/>
)}
2021-05-09 21:59:03 +02:00
</>
2020-10-26 00:27:53 +01:00
)
}
2020-10-28 00:02:37 +01:00
2021-01-30 01:29:15 +01:00
export default TabSharedAccount