tooot/src/screens/Tabs/Me/FollowedTags.tsx

79 lines
2.3 KiB
TypeScript
Raw Normal View History

2022-12-10 20:19:18 +01:00
import Button from '@components/Button'
import haptics from '@components/haptics'
import ComponentHashtag from '@components/Hashtag'
import { displayMessage } from '@components/Message'
import ComponentSeparator from '@components/Separator'
import { TabMeStackScreenProps } from '@utils/navigation/navigators'
2023-01-26 23:07:13 +01:00
import { useFollowedTagsQuery, useTagMutation } from '@utils/queryHooks/tags'
2022-12-31 00:07:28 +01:00
import { flattenPages } from '@utils/queryHooks/utils'
2022-12-10 20:19:18 +01:00
import React, { useEffect } from 'react'
import { useTranslation } from 'react-i18next'
import { FlatList } from 'react-native-gesture-handler'
2022-12-10 20:19:18 +01:00
const TabMeFollowedTags: React.FC<TabMeStackScreenProps<'Tab-Me-FollowedTags'>> = ({
navigation
}) => {
2023-01-26 23:07:13 +01:00
const { t } = useTranslation(['common', 'screenTabs', 'componentContextMenu'])
2022-12-10 20:19:18 +01:00
const { data, fetchNextPage, refetch } = useFollowedTagsQuery()
2022-12-31 00:07:28 +01:00
const flattenData = flattenPages(data)
2022-12-10 20:19:18 +01:00
useEffect(() => {
if (flattenData.length === 0) {
navigation.goBack()
}
}, [flattenData.length])
2023-01-26 23:07:13 +01:00
const mutation = useTagMutation({
2022-12-10 20:19:18 +01:00
onSuccess: () => {
haptics('Light')
refetch()
},
onError: (err: any, { to }) => {
displayMessage({
type: 'error',
message: t('common:message.error.message', {
2023-01-26 23:07:13 +01:00
function: t('componentContextMenu:hashtag.follow.action', {
defaultValue: 'false',
context: to.toString()
})
2022-12-10 20:19:18 +01:00
}),
...(err.status &&
typeof err.status === 'number' &&
err.data &&
err.data.error &&
typeof err.data.error === 'string' && {
description: err.data.error
})
})
}
})
return (
<FlatList
style={{ flex: 1 }}
2022-12-10 20:19:18 +01:00
data={flattenData}
renderItem={({ item }) => (
<ComponentHashtag
hashtag={item}
children={
<Button
type='text'
2023-01-26 23:07:13 +01:00
content={t('componentContextMenu:hashtag.follow.action', {
2023-03-12 18:49:06 +01:00
defaultValue: 'true',
context: 'true'
2023-01-26 23:07:13 +01:00
})}
onPress={() => mutation.mutate({ tag_name: item.name, to: !item.following })}
2022-12-10 20:19:18 +01:00
/>
}
/>
)}
onEndReached={() => fetchNextPage()}
onEndReachedThreshold={0.5}
ItemSeparatorComponent={ComponentSeparator}
/>
)
}
export default TabMeFollowedTags