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

275 lines
9.9 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'
2023-02-11 22:04:32 +01:00
import SegmentedControl from '@react-native-segmented-control/segmented-control'
2022-12-18 23:32:57 +01:00
import { useQueryClient } from '@tanstack/react-query'
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'
import { useRelationshipQuery } from '@utils/queryHooks/relationship'
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-12-25 17:40:53 +01:00
import React, { Fragment, useEffect, useMemo, useState } from 'react'
2021-04-09 21:43:12 +02:00
import { useTranslation } from 'react-i18next'
2023-01-12 21:44:28 +01:00
import { Platform, Text, View } from 'react-native'
2021-01-15 01:15:27 +01:00
import { useSharedValue } from 'react-native-reanimated'
2022-12-03 01:08:38 +01:00
import * as DropdownMenu from 'zeego/dropdown-menu'
import AccountAttachments from './Attachments'
2023-01-02 02:08:12 +01:00
import AccountContext from './Context'
import AccountHeader from './Header'
import AccountInformation from './Information'
import AccountNav from './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: {
2023-01-02 23:18:22 +01:00
params: { account }
2022-06-07 20:07:14 +02:00
}
2020-10-31 21:04:46 +01:00
}) => {
2022-12-14 23:37:41 +01:00
const { t } = useTranslation('screenTabs')
2022-02-12 14:51:01 +01:00
const { colors, mode } = useTheme()
2021-01-16 00:00:31 +01:00
2023-01-02 02:08:12 +01:00
const { data, dataUpdatedAt } = useAccountQuery({
2023-01-02 23:18:22 +01:00
account,
2023-01-03 23:57:23 +01:00
_local: true,
2023-01-02 23:18:22 +01:00
options: {
2023-01-29 21:53:13 +01:00
placeholderData: (account._remote
? { ...account, id: undefined }
: account) as Mastodon.Account,
2023-01-02 23:18:22 +01:00
onSuccess: a => {
if (account._remote) {
setQueryKey([
queryKey[0],
{
...queryKey[1],
page: 'Account',
id: a.id,
exclude_reblogs: true,
only_media: false
}
])
}
2023-01-05 00:49:10 +01:00
},
onError: () => navigation.goBack()
2023-01-02 23:18:22 +01:00
}
2023-01-02 02:08:12 +01:00
})
const { data: dataRelationship } = useRelationshipQuery({
id: account._remote ? data?.id : account.id,
options: { enabled: account._remote ? !!data?.id : true }
})
2023-01-02 02:08:12 +01:00
2023-01-04 22:39:29 +01:00
const queryClient = useQueryClient()
const [queryKey, setQueryKey] = useState<QueryKeyTimeline>([
'Timeline',
{
page: 'Account',
id: account._remote ? data?.id : account.id,
exclude_reblogs: true,
only_media: false
}
])
2023-01-02 02:08:12 +01:00
const mShare = menuShare({ type: 'account', url: data?.url })
const mAccount = menuAccount({ type: 'account', openChange: true, account: data })
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='more-horizontal'
2022-12-03 01:08:38 +01:00
onPress={() => {}}
background
/>
</DropdownMenu.Trigger>
<DropdownMenu.Content>
2023-01-08 16:59:35 +01:00
{[mShare, mAccount].map((menu, i) => (
2022-12-25 17:40:53 +01:00
<Fragment key={i}>
2023-01-08 16:59:35 +01:00
{menu.map((group, index) => (
2022-12-24 01:59:18 +01:00
<DropdownMenu.Group key={index}>
2023-01-08 16:59:35 +01:00
{group.map(item => {
switch (item.type) {
case 'item':
return (
<DropdownMenu.Item key={item.key} {...item.props}>
<DropdownMenu.ItemTitle children={item.title} />
{item.icon ? (
<DropdownMenu.ItemIcon ios={{ name: item.icon }} />
) : null}
</DropdownMenu.Item>
)
case 'sub':
2023-01-12 21:44:28 +01:00
if (Platform.OS === 'ios') {
return (
// @ts-ignore
<DropdownMenu.Sub key={item.key}>
<DropdownMenu.SubTrigger
key={item.trigger.key}
{...item.trigger.props}
>
<DropdownMenu.ItemTitle children={item.trigger.title} />
{item.trigger.icon ? (
<DropdownMenu.ItemIcon ios={{ name: item.trigger.icon }} />
) : null}
</DropdownMenu.SubTrigger>
<DropdownMenu.SubContent>
{item.items.map(sub => (
<DropdownMenu.Item key={sub.key} {...sub.props}>
<DropdownMenu.ItemTitle children={sub.title} />
{sub.icon ? (
<DropdownMenu.ItemIcon ios={{ name: sub.icon }} />
) : null}
</DropdownMenu.Item>
))}
</DropdownMenu.SubContent>
</DropdownMenu.Sub>
)
} else {
return (
<Fragment key={item.key}>
2023-01-08 16:59:35 +01:00
{item.items.map(sub => (
<DropdownMenu.Item key={sub.key} {...sub.props}>
<DropdownMenu.ItemTitle children={sub.title} />
{sub.icon ? (
<DropdownMenu.ItemIcon ios={{ name: sub.icon }} />
) : null}
</DropdownMenu.Item>
))}
2023-01-12 21:44:28 +01:00
</Fragment>
)
}
2023-01-08 16:59:35 +01:00
}
})}
2022-12-24 01:59:18 +01:00
</DropdownMenu.Group>
2022-12-03 01:08:38 +01:00
))}
2022-12-25 17:40:53 +01:00
</Fragment>
2022-12-03 01:08:38 +01:00
))}
</DropdownMenu.Content>
</DropdownMenu.Root>
)
}
})
2022-12-04 16:16:53 +01:00
}, [mAccount])
2023-01-04 22:39:29 +01:00
useEffect(() => {
navigation.setParams({ queryKey })
}, [queryKey[1]])
2022-12-03 01:08:38 +01:00
const scrollY = useSharedValue(0)
2020-10-26 00:27:53 +01:00
2022-12-14 23:37:41 +01:00
const page = queryKey[1]
2021-06-01 22:27:27 +02:00
2022-12-18 11:16:30 +01:00
const [segment, setSegment] = useState<number>(0)
2021-06-01 22:27:27 +02:00
const ListHeaderComponent = useMemo(() => {
return (
<>
2022-12-03 15:50:15 +01:00
<View style={{ borderBottomWidth: 1, borderBottomColor: colors.border }}>
2023-01-02 02:08:12 +01:00
<AccountHeader />
<AccountInformation />
<AccountAttachments />
2021-06-01 22:27:27 +02:00
</View>
2022-11-06 23:39:31 +01:00
{!data?.suspended ? (
2023-02-11 22:04:32 +01:00
// @ts-ignore
2022-11-06 23:39:31 +01:00
<SegmentedControl
appearance={mode}
values={[t('shared.account.toots.default'), t('shared.account.toots.all')]}
2022-12-18 11:16:30 +01:00
selectedIndex={segment}
2023-02-11 22:04:32 +01:00
onChange={({ nativeEvent }: any) => {
2022-12-18 11:16:30 +01:00
setSegment(nativeEvent.selectedSegmentIndex)
2022-11-06 23:39:31 +01:00
switch (nativeEvent.selectedSegmentIndex) {
case 0:
2022-12-14 23:37:41 +01:00
setQueryKey([
queryKey[0],
{
...page,
page: 'Account',
2023-01-02 02:08:12 +01:00
id: data?.id,
2022-12-14 23:37:41 +01:00
exclude_reblogs: true,
only_media: false
}
])
2022-11-06 23:39:31 +01:00
break
case 1:
2022-12-14 23:37:41 +01:00
setQueryKey([
queryKey[0],
{
...page,
page: 'Account',
2023-01-02 02:08:12 +01:00
id: data?.id,
2022-12-14 23:37:41 +01:00
exclude_reblogs: false,
only_media: false
}
])
2022-11-06 23:39:31 +01:00
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
</>
)
2023-01-02 02:08:12 +01:00
}, [segment, dataUpdatedAt, mode])
2021-02-27 16:33:54 +01:00
2020-11-22 00:46:23 +01:00
return (
<AccountContext.Provider value={{ account: data, relationship: dataRelationship }}>
2023-01-02 02:08:12 +01:00
<AccountNav scrollY={scrollY} />
2021-01-15 01:15:27 +01:00
2022-11-06 23:39:31 +01:00
{data?.suspended ? (
ListHeaderComponent
) : (
<Timeline
queryKey={queryKey}
disableRefresh
2023-01-02 23:18:22 +01:00
queryOptions={{ enabled: account._remote ? !!data?.id : true }}
2022-11-06 23:39:31 +01:00
customProps={{
renderItem: ({ item }) => <TimelineDefault item={item} queryKey={queryKey} />,
onScroll: ({ nativeEvent }) => (scrollY.value = nativeEvent.contentOffset.y),
ListHeaderComponent,
2022-12-18 23:32:57 +01:00
maintainVisibleContentPosition: undefined,
onRefresh: () => queryClient.refetchQueries(queryKey),
2022-12-23 18:49:50 +01:00
refreshing: false
2022-11-06 23:39:31 +01:00
}}
/>
)}
2023-01-02 02:08:12 +01:00
</AccountContext.Provider>
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