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

82 lines
2.5 KiB
TypeScript
Raw Normal View History

2022-11-20 16:14:08 +01:00
import haptics from '@components/haptics'
import { HeaderRight } from '@components/Header'
import { displayMessage } from '@components/Message'
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-08-29 15:25:38 +02:00
import { TabSharedStackScreenProps } from '@utils/navigation/navigators'
2022-11-20 16:14:08 +01:00
import { 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 { checkInstanceFeature } from '@utils/slices/instancesSlice'
import { useTheme } from '@utils/styles/ThemeManager'
import React, { useEffect } from 'react'
import { useTranslation } from 'react-i18next'
import { useSelector } from 'react-redux'
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
}) => {
2021-02-27 16:33:54 +01:00
const queryKey: QueryKeyTimeline = ['Timeline', { page: 'Hashtag', hashtag }]
2022-11-20 16:14:08 +01:00
const { theme } = useTheme()
const { t } = useTranslation('screenTabs')
const canFollowTags = useSelector(checkInstanceFeature('follow_tags'))
const { data, isFetching, refetch } = useTagsQuery({
tag: hashtag,
options: { enabled: canFollowTags }
})
const mutation = useTagsMutation({
onSuccess: () => {
haptics('Success')
refetch()
},
onError: (err: any, { to }) => {
displayMessage({
theme,
type: 'error',
message: t('common:message.error.message', {
function: to ? t('shared.hashtag.follow') : t('shared.hashtag.unfollow')
}),
...(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'
content={data?.following ? t('shared.hashtag.unfollow') : t('shared.hashtag.follow')}
onPress={() =>
typeof data?.following === 'boolean' &&
mutation.mutate({ tag: hashtag, type: 'follow', to: !data.following })
}
/>
)
})
}, [canFollowTags, data?.following, isFetching])
2022-08-07 01:18:10 +02:00
return (
<Timeline
queryKey={queryKey}
customProps={{
2022-11-20 16:14:08 +01:00
renderItem: ({ item }) => <TimelineDefault item={item} queryKey={queryKey} />
2022-08-07 01:18:10 +02:00
}}
/>
2021-02-27 16:33:54 +01:00
)
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