tooot/src/components/Relationship/Outgoing.tsx

162 lines
4.6 KiB
TypeScript
Raw Normal View History

import Button from '@components/Button'
import haptics from '@components/haptics'
2021-02-28 22:49:55 +01:00
import { displayMessage } from '@components/Message'
import { useRoute } from '@react-navigation/native'
import { useQueryClient } from '@tanstack/react-query'
import { featureCheck } from '@utils/helpers/featureCheck'
2021-01-11 21:36:57 +01:00
import {
QueryKeyRelationship,
useRelationshipMutation,
useRelationshipQuery
2021-01-10 02:12:14 +01:00
} from '@utils/queryHooks/relationship'
import { StyleConstants } from '@utils/styles/constants'
2021-02-28 22:49:55 +01:00
import { useTheme } from '@utils/styles/ThemeManager'
2021-01-11 21:36:57 +01:00
import React from 'react'
import { useTranslation } from 'react-i18next'
export interface Props {
id: Mastodon.Account['id']
}
2022-12-18 01:12:58 +01:00
const RelationshipOutgoing: React.FC<Props> = ({ id }: Props) => {
const { theme } = useTheme()
2022-12-23 15:53:40 +01:00
const { t } = useTranslation(['common', 'componentRelationship'])
const canFollowNotify = featureCheck('account_follow_notify')
2022-12-18 01:12:58 +01:00
const query = useRelationshipQuery({ id })
const queryKeyRelationship: QueryKeyRelationship = ['Relationship', { id }]
const queryClient = useQueryClient()
const mutation = useRelationshipMutation({
2023-02-12 19:47:28 +01:00
onSuccess: (res, vars) => {
2022-12-18 01:12:58 +01:00
haptics('Success')
queryClient.setQueryData<Mastodon.Relationship[]>(queryKeyRelationship, [res])
2023-02-12 19:47:28 +01:00
if (vars.type === 'outgoing' && vars.payload.action === 'block') {
2022-12-18 01:12:58 +01:00
const queryKey = ['Timeline', { page: 'Following' }]
queryClient.invalidateQueries({ queryKey, exact: false })
2021-01-11 21:36:57 +01:00
}
2022-12-18 01:12:58 +01:00
},
2023-02-12 19:47:28 +01:00
onError: (err: any, vars) => {
2022-12-18 01:12:58 +01:00
displayMessage({
theme,
type: 'error',
message: t('common:message.error.message', {
2023-02-12 19:47:28 +01:00
function: t(`componentRelationship:${(vars.payload as any).action}.function` as any)
2022-12-18 01:12:58 +01:00
}),
...(err.status &&
typeof err.status === 'number' &&
err.data &&
err.data.error &&
typeof err.data.error === 'string' && {
description: err.data.error
})
})
}
})
2022-12-18 01:12:58 +01:00
let content: string
let onPress: () => void
2022-12-18 01:12:58 +01:00
if (query.isError) {
2022-12-23 15:53:40 +01:00
content = t('componentRelationship:button.error')
2022-12-18 01:12:58 +01:00
onPress = () => {}
} else {
if (query.data?.blocked_by) {
2022-12-23 15:53:40 +01:00
content = t('componentRelationship:button.blocked_by')
2021-01-11 21:36:57 +01:00
onPress = () => {}
} else {
2022-12-18 01:12:58 +01:00
if (query.data?.blocking) {
2022-12-23 15:53:40 +01:00
content = t('componentRelationship:button.blocking')
2022-12-18 01:12:58 +01:00
onPress = () => {
mutation.mutate({
id,
type: 'outgoing',
payload: {
action: 'block',
state: query.data?.blocking
}
})
}
} else {
2022-12-18 01:12:58 +01:00
if (query.data?.following) {
2022-12-23 15:53:40 +01:00
content = t('componentRelationship:button.following')
2021-01-24 02:25:43 +01:00
onPress = () => {
mutation.mutate({
2021-01-11 21:36:57 +01:00
id,
type: 'outgoing',
payload: {
2022-12-18 01:12:58 +01:00
action: 'follow',
state: query.data?.following
2021-01-11 21:36:57 +01:00
}
})
2021-01-24 02:25:43 +01:00
}
} else {
2022-12-18 01:12:58 +01:00
if (query.data?.requested) {
2022-12-23 15:53:40 +01:00
content = t('componentRelationship:button.requested')
2021-01-24 02:25:43 +01:00
onPress = () => {
mutation.mutate({
2021-01-11 21:36:57 +01:00
id,
type: 'outgoing',
payload: {
action: 'follow',
2022-12-18 01:12:58 +01:00
state: query.data?.requested
2021-01-11 21:36:57 +01:00
}
})
2021-01-24 02:25:43 +01:00
}
} else {
2022-12-23 15:53:40 +01:00
content = t('componentRelationship:button.default')
2022-12-18 01:12:58 +01:00
onPress = () => {
mutation.mutate({
id,
type: 'outgoing',
payload: {
action: 'follow',
state: false
}
})
2021-01-11 21:36:57 +01:00
}
}
}
}
}
2022-12-18 01:12:58 +01:00
}
2021-01-11 21:36:57 +01:00
const { name } = useRoute()
const isPageNotifications = name === 'Tab-Notifications-Root'
2022-12-18 01:12:58 +01:00
return (
<>
{!isPageNotifications && canFollowNotify && query.data?.following ? (
2022-12-18 01:12:58 +01:00
<Button
type='icon'
content={query.data.notifying ? 'bell-off' : 'bell'}
2022-12-18 01:12:58 +01:00
round
onPress={() =>
mutation.mutate({
id,
type: 'outgoing',
payload: {
action: 'follow',
state: false,
notify: !query.data.notifying
}
})
}
loading={query.isLoading || mutation.isLoading}
style={{ marginRight: StyleConstants.Spacing.S }}
/>
) : null}
2021-01-11 21:36:57 +01:00
<Button
type='text'
content={content}
onPress={onPress}
loading={query.isLoading || mutation.isLoading}
disabled={query.isError || query.data?.blocked_by}
/>
</>
2022-12-18 01:12:58 +01:00
)
}
export default RelationshipOutgoing