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

316 lines
12 KiB
TypeScript
Raw Normal View History

2024-02-07 00:22:22 +01:00
import { HeaderRight } from '@components/Header'
2023-02-26 21:51:31 +01:00
import Icon from '@components/Icon'
import CustomText from '@components/Text'
2021-02-08 23:47:20 +01:00
import Timeline from '@components/Timeline'
2024-02-07 00:22:22 +01:00
import menuAccount from '@components/contextMenu/account'
import menuShare from '@components/contextMenu/share'
2021-08-29 15:25:38 +02:00
import { TabSharedStackScreenProps } from '@utils/navigation/navigators'
2023-02-26 21:51:31 +01:00
import { queryClient } from '@utils/queryHooks'
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'
2023-02-25 23:42:04 +01:00
import { useAccountStorage } from '@utils/storage/actions'
2021-01-16 00:00:31 +01:00
import { useTheme } from '@utils/styles/ThemeManager'
2024-02-07 00:22:22 +01:00
import { StyleConstants } from '@utils/styles/constants'
2023-02-26 21:51:31 +01:00
import React, { Fragment, useEffect, useMemo } from 'react'
2021-04-09 21:43:12 +02:00
import { useTranslation } from 'react-i18next'
2023-02-26 21:51:31 +01:00
import { Platform, Pressable, 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
}) => {
2023-02-26 21:51:31 +01:00
const { t } = useTranslation(['common', 'screenTabs'])
2022-02-12 14:51:01 +01:00
const { colors, mode } = useTheme()
2021-01-16 00:00:31 +01:00
2023-02-25 23:42:04 +01:00
const { data, dataUpdatedAt, isFetched } = 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-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({
2023-02-25 23:42:04 +01:00
id: data?.id,
options: { enabled: account._remote ? isFetched : true }
})
2023-02-26 21:51:31 +01:00
const queryKeyDefault: QueryKeyTimeline = [
2023-01-04 22:39:29 +01:00
'Timeline',
{
page: 'Account',
2023-02-26 21:51:31 +01:00
type: 'default',
2023-02-25 23:42:04 +01:00
id: data?.id,
...(account._remote && { remote_id: account.id, remote_domain: account._remote })
2023-01-04 22:39:29 +01:00
}
2023-02-26 21:51:31 +01:00
]
2023-03-19 23:26:30 +01:00
useEffect(() => {
navigation.setParams({ queryKey: queryKeyDefault })
}, [dataUpdatedAt])
2023-01-04 22:39:29 +01:00
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,
2023-02-25 23:42:04 +01:00
headerStyle: { backgroundColor: `rgba(255, 255, 255, 0)` },
2022-12-03 01:08:38 +01:00
headerRight: () => {
return (
<DropdownMenu.Root>
<DropdownMenu.Trigger>
<HeaderRight
2023-02-26 21:51:31 +01:00
accessibilityLabel={t('screenTabs:shared.account.actions.accessibilityLabel', {
2022-12-03 01:08:38 +01:00
user: account.acct
})}
2023-02-26 21:51:31 +01:00
accessibilityHint={t('screenTabs: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])
2022-12-03 01:08:38 +01:00
const scrollY = useSharedValue(0)
2020-10-26 00:27:53 +01:00
2023-02-26 21:51:31 +01:00
const [timelineSettings, setTimelineSettings] = useAccountStorage.object('page_account_timeline')
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 />
2023-02-25 23:42:04 +01:00
<AccountAttachments
{...(account._remote && { remote_id: account.id, remote_domain: account._remote })}
/>
2021-06-01 22:27:27 +02:00
</View>
2022-11-06 23:39:31 +01:00
{!data?.suspended ? (
2023-02-26 21:51:31 +01:00
<DropdownMenu.Root>
<DropdownMenu.Trigger>
<Pressable
style={{
flex: 1,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
gap: StyleConstants.Spacing.XS,
paddingVertical: StyleConstants.Spacing.S,
paddingHorizontal: StyleConstants.Spacing.Global.PagePadding
}}
>
<View style={{ flex: 1 }} />
<View
style={{ flex: 1 }}
children={
<CustomText
style={{ color: colors.secondary, alignSelf: 'center' }}
children={t('screenTabs:shared.account.summary.statuses_count', {
count: data?.statuses_count || 0
})}
/>
}
/>
<View
style={{
flex: 1,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'flex-end'
}}
>
<Icon name='filter' color={colors.secondary} size={StyleConstants.Font.Size.M} />
</View>
</Pressable>
</DropdownMenu.Trigger>
<DropdownMenu.Content>
<DropdownMenu.Group>
<DropdownMenu.CheckboxItem
key='showBoosts'
value={
(
typeof timelineSettings?.excludeBoosts === 'boolean'
? timelineSettings.excludeBoosts
: true
)
? 'off'
: 'on'
}
onValueChange={() => {
setTimelineSettings({
...timelineSettings,
excludeBoosts: !timelineSettings?.excludeBoosts
})
queryClient.refetchQueries(queryKeyDefault)
}}
>
<DropdownMenu.ItemIndicator />
<DropdownMenu.ItemTitle
children={t('screenTabs:tabs.local.options.showBoosts')}
/>
</DropdownMenu.CheckboxItem>
<DropdownMenu.CheckboxItem
key='showReplies'
value={
(
typeof timelineSettings?.excludeReplies === 'boolean'
? timelineSettings.excludeReplies
: true
)
? 'off'
: 'on'
}
onValueChange={() => {
setTimelineSettings({
...timelineSettings,
excludeReplies: !timelineSettings?.excludeReplies
})
queryClient.refetchQueries(queryKeyDefault)
}}
>
<DropdownMenu.ItemTitle
children={t('screenTabs:tabs.local.options.showReplies')}
/>
<DropdownMenu.ItemIndicator />
</DropdownMenu.CheckboxItem>
</DropdownMenu.Group>
</DropdownMenu.Content>
</DropdownMenu.Root>
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'
}}
>
2023-02-26 21:51:31 +01:00
{t('screenTabs:shared.account.suspended')}
2022-11-06 23:39:31 +01:00
</Text>
</View>
) : null}
2021-06-01 22:27:27 +02:00
</>
)
2023-02-26 21:51:31 +01:00
}, [timelineSettings, dataUpdatedAt, mode])
2021-02-27 16:33:54 +01:00
2023-02-25 23:42:04 +01:00
const [domain] = useAccountStorage.string('auth.account.domain')
2020-11-22 00:46:23 +01:00
return (
2023-02-25 23:42:04 +01:00
<AccountContext.Provider
value={{
account: data,
relationship: dataRelationship,
localInstance: account?.acct?.includes('@')
? account?.acct?.includes(`@${domain}`)
: !account?._remote
}}
>
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
2023-02-26 21:51:31 +01:00
queryKey={queryKeyDefault}
2022-11-06 23:39:31 +01:00
disableRefresh
customProps={{
2023-02-25 16:52:38 +01:00
keyboardShouldPersistTaps: 'always',
2022-11-06 23:39:31 +01:00
onScroll: ({ nativeEvent }) => (scrollY.value = nativeEvent.contentOffset.y),
ListHeaderComponent,
2023-02-26 21:51:31 +01:00
refreshing: false,
onRefresh: () => queryClient.refetchQueries(queryKeyDefault)
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