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

88 lines
2.8 KiB
TypeScript
Raw Normal View History

2022-11-20 16:14:08 +01:00
import haptics from '@components/haptics'
2022-12-03 16:50:54 +01:00
import { HeaderLeft, HeaderRight } from '@components/Header'
2022-11-20 16:14:08 +01:00
import { displayMessage } from '@components/Message'
2021-02-08 23:47:20 +01:00
import Timeline from '@components/Timeline'
2022-12-10 20:19:18 +01:00
import { useQueryClient } from '@tanstack/react-query'
import { featureCheck } from '@utils/helpers/featureCheck'
2021-08-29 15:25:38 +02:00
import { TabSharedStackScreenProps } from '@utils/navigation/navigators'
2022-12-10 20:19:18 +01:00
import { QueryKeyFollowedTags, useTagsMutation, useTagsQuery } from '@utils/queryHooks/tags'
2021-02-27 16:33:54 +01:00
import { QueryKeyTimeline } from '@utils/queryHooks/timeline'
2022-11-20 16:14:08 +01:00
import React, { useEffect } from 'react'
import { useTranslation } from 'react-i18next'
2020-10-26 00:27:53 +01:00
2022-11-20 16:14:08 +01:00
const TabSharedHashtag: React.FC<TabSharedStackScreenProps<'Tab-Shared-Hashtag'>> = ({
navigation,
2020-10-26 00:27:53 +01:00
route: {
params: { hashtag }
}
2020-10-31 21:04:46 +01:00
}) => {
2023-01-04 22:39:29 +01:00
const queryKey: QueryKeyTimeline = ['Timeline', { page: 'Hashtag', hashtag }]
2022-12-03 16:50:54 +01:00
useEffect(() => {
navigation.setOptions({
headerLeft: () => <HeaderLeft onPress={() => navigation.goBack()} />,
title: `#${decodeURIComponent(hashtag)}`
})
2023-01-04 22:39:29 +01:00
navigation.setParams({ queryKey: queryKey })
2022-12-03 16:50:54 +01:00
}, [])
2022-12-23 15:53:40 +01:00
const { t } = useTranslation(['common', 'screenTabs'])
2022-11-20 16:14:08 +01:00
const canFollowTags = featureCheck('follow_tags')
2022-11-20 16:14:08 +01:00
const { data, isFetching, refetch } = useTagsQuery({
tag: hashtag,
options: { enabled: canFollowTags }
})
2022-12-10 20:19:18 +01:00
const queryClient = useQueryClient()
2022-11-20 16:14:08 +01:00
const mutation = useTagsMutation({
onSuccess: () => {
haptics('Success')
refetch()
2022-12-10 20:19:18 +01:00
const queryKeyFollowedTags: QueryKeyFollowedTags = ['FollowedTags']
queryClient.invalidateQueries({ queryKey: queryKeyFollowedTags })
2022-11-20 16:14:08 +01:00
},
onError: (err: any, { to }) => {
displayMessage({
type: 'error',
message: t('common:message.error.message', {
2022-12-23 15:53:40 +01:00
function: to
? t('screenTabs:shared.hashtag.follow')
: t('screenTabs:shared.hashtag.unfollow')
2022-11-20 16:14:08 +01:00
}),
...(err.status &&
typeof err.status === 'number' &&
err.data &&
err.data.error &&
typeof err.data.error === 'string' && {
description: err.data.error
})
})
}
})
useEffect(() => {
if (!canFollowTags) return
navigation.setOptions({
headerRight: () => (
<HeaderRight
loading={isFetching || mutation.isLoading}
type='text'
2022-12-23 15:53:40 +01:00
content={
data?.following
? t('screenTabs:shared.hashtag.unfollow')
: t('screenTabs:shared.hashtag.follow')
}
2022-11-20 16:14:08 +01:00
onPress={() =>
typeof data?.following === 'boolean' &&
2022-12-10 20:19:18 +01:00
mutation.mutate({ tag: hashtag, to: !data.following })
2022-11-20 16:14:08 +01:00
}
/>
)
})
}, [canFollowTags, data?.following, isFetching])
return <Timeline queryKey={queryKey} />
2020-10-26 00:27:53 +01:00
}
2020-10-31 21:04:46 +01:00
2021-01-30 01:29:15 +01:00
export default TabSharedHashtag