2021-01-24 02:25:43 +01:00
|
|
|
import analytics from '@components/analytics'
|
2021-01-04 10:50:24 +01:00
|
|
|
import Button from '@components/Button'
|
|
|
|
import haptics from '@components/haptics'
|
2021-02-28 22:49:55 +01:00
|
|
|
import { displayMessage } from '@components/Message'
|
2021-01-11 21:36:57 +01:00
|
|
|
import {
|
|
|
|
QueryKeyRelationship,
|
|
|
|
useRelationshipMutation,
|
|
|
|
useRelationshipQuery
|
2021-01-10 02:12:14 +01:00
|
|
|
} from '@utils/queryHooks/relationship'
|
2021-01-20 00:39:39 +01:00
|
|
|
import { QueryKeyTimeline } from '@utils/queryHooks/timeline'
|
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'
|
2021-01-04 10:50:24 +01:00
|
|
|
import { useTranslation } from 'react-i18next'
|
2021-01-11 21:36:57 +01:00
|
|
|
import { useQueryClient } from 'react-query'
|
2021-01-04 10:50:24 +01:00
|
|
|
|
|
|
|
export interface Props {
|
|
|
|
id: Mastodon.Account['id']
|
|
|
|
}
|
|
|
|
|
2021-01-11 21:36:57 +01:00
|
|
|
const RelationshipOutgoing = React.memo(
|
|
|
|
({ id }: Props) => {
|
2021-02-28 22:49:55 +01:00
|
|
|
const { mode } = useTheme()
|
2021-01-19 01:13:45 +01:00
|
|
|
const { t } = useTranslation('componentRelationship')
|
2021-01-04 10:50:24 +01:00
|
|
|
|
2021-01-11 21:36:57 +01:00
|
|
|
const query = useRelationshipQuery({ id })
|
2021-01-04 10:50:24 +01:00
|
|
|
|
2021-01-11 21:36:57 +01:00
|
|
|
const queryKeyRelationship: QueryKeyRelationship = ['Relationship', { id }]
|
|
|
|
const queryClient = useQueryClient()
|
|
|
|
const mutation = useRelationshipMutation({
|
2021-01-20 00:39:39 +01:00
|
|
|
onSuccess: (res, { payload: { action } }) => {
|
2021-01-11 21:36:57 +01:00
|
|
|
haptics('Success')
|
|
|
|
queryClient.setQueryData<Mastodon.Relationship[]>(
|
|
|
|
queryKeyRelationship,
|
|
|
|
[res]
|
|
|
|
)
|
2021-01-20 00:39:39 +01:00
|
|
|
if (action === 'follow' || action === 'block') {
|
|
|
|
const queryKey: QueryKeyTimeline = ['Timeline', { page: 'Following' }]
|
|
|
|
queryClient.invalidateQueries(queryKey)
|
|
|
|
}
|
2021-01-11 21:36:57 +01:00
|
|
|
},
|
2021-01-19 01:13:45 +01:00
|
|
|
onError: (err: any, { payload: { action } }) => {
|
2021-02-28 22:49:55 +01:00
|
|
|
displayMessage({
|
|
|
|
mode,
|
2021-01-11 21:36:57 +01:00
|
|
|
type: 'error',
|
|
|
|
message: t('common:toastMessage.error.message', {
|
2021-01-24 02:25:43 +01:00
|
|
|
function: t(`${action}.function`)
|
2021-01-11 21:36:57 +01:00
|
|
|
}),
|
|
|
|
...(err.status &&
|
|
|
|
typeof err.status === 'number' &&
|
|
|
|
err.data &&
|
|
|
|
err.data.error &&
|
|
|
|
typeof err.data.error === 'string' && {
|
|
|
|
description: err.data.error
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2021-01-04 10:50:24 +01:00
|
|
|
|
2021-01-11 21:36:57 +01:00
|
|
|
let content: string
|
|
|
|
let onPress: () => void
|
2021-01-04 10:50:24 +01:00
|
|
|
|
2021-01-11 21:36:57 +01:00
|
|
|
if (query.isError) {
|
2021-01-19 01:13:45 +01:00
|
|
|
content = t('button.error')
|
2021-01-11 21:36:57 +01:00
|
|
|
onPress = () => {}
|
2021-01-04 10:50:24 +01:00
|
|
|
} else {
|
2021-01-11 21:36:57 +01:00
|
|
|
if (query.data?.blocked_by) {
|
2021-01-24 02:25:43 +01:00
|
|
|
analytics('relationship_outgoing_blocked_by')
|
2021-01-19 01:13:45 +01:00
|
|
|
content = t('button.blocked_by')
|
2021-01-24 02:25:43 +01:00
|
|
|
onPress = () => {
|
|
|
|
analytics('relationship_outgoing_blocked_by_press')
|
|
|
|
}
|
2021-01-04 10:50:24 +01:00
|
|
|
} else {
|
2021-01-11 21:36:57 +01:00
|
|
|
if (query.data?.blocking) {
|
2021-01-24 02:25:43 +01:00
|
|
|
analytics('relationship_outgoing_blocking')
|
2021-01-19 01:13:45 +01:00
|
|
|
content = t('button.blocking')
|
2021-01-24 02:25:43 +01:00
|
|
|
onPress = () => {
|
|
|
|
analytics('relationship_outgoing_blocking_press')
|
2021-01-04 10:50:24 +01:00
|
|
|
mutation.mutate({
|
2021-01-11 21:36:57 +01:00
|
|
|
id,
|
|
|
|
type: 'outgoing',
|
|
|
|
payload: {
|
|
|
|
action: 'block',
|
|
|
|
state: query.data?.blocking
|
|
|
|
}
|
2021-01-04 10:50:24 +01:00
|
|
|
})
|
2021-01-24 02:25:43 +01:00
|
|
|
}
|
2021-01-04 10:50:24 +01:00
|
|
|
} else {
|
2021-01-11 21:36:57 +01:00
|
|
|
if (query.data?.following) {
|
2021-01-24 02:25:43 +01:00
|
|
|
analytics('relationship_outgoing_following')
|
2021-01-19 01:13:45 +01:00
|
|
|
content = t('button.following')
|
2021-01-24 02:25:43 +01:00
|
|
|
onPress = () => {
|
|
|
|
analytics('relationship_outgoing_following_press')
|
2021-01-04 10:50:24 +01:00
|
|
|
mutation.mutate({
|
2021-01-11 21:36:57 +01:00
|
|
|
id,
|
|
|
|
type: 'outgoing',
|
|
|
|
payload: {
|
|
|
|
action: 'follow',
|
|
|
|
state: query.data?.following
|
|
|
|
}
|
2021-01-04 10:50:24 +01:00
|
|
|
})
|
2021-01-24 02:25:43 +01:00
|
|
|
}
|
2021-01-04 10:50:24 +01:00
|
|
|
} else {
|
2021-01-11 21:36:57 +01:00
|
|
|
if (query.data?.requested) {
|
2021-01-24 02:25:43 +01:00
|
|
|
analytics('relationship_outgoing_requested')
|
2021-01-19 01:13:45 +01:00
|
|
|
content = t('button.requested')
|
2021-01-24 02:25:43 +01:00
|
|
|
onPress = () => {
|
|
|
|
analytics('relationship_outgoing_requested_press')
|
2021-01-11 21:36:57 +01:00
|
|
|
mutation.mutate({
|
|
|
|
id,
|
|
|
|
type: 'outgoing',
|
|
|
|
payload: {
|
|
|
|
action: 'follow',
|
|
|
|
state: query.data?.requested
|
|
|
|
}
|
|
|
|
})
|
2021-01-24 02:25:43 +01:00
|
|
|
}
|
2021-01-11 21:36:57 +01:00
|
|
|
} else {
|
2021-01-24 02:25:43 +01:00
|
|
|
analytics('relationship_outgoing_default')
|
2021-01-19 01:13:45 +01:00
|
|
|
content = t('button.default')
|
2021-01-24 02:25:43 +01:00
|
|
|
onPress = () => {
|
|
|
|
analytics('relationship_outgoing_default_press')
|
2021-01-11 21:36:57 +01:00
|
|
|
mutation.mutate({
|
|
|
|
id,
|
|
|
|
type: 'outgoing',
|
|
|
|
payload: {
|
|
|
|
action: 'follow',
|
|
|
|
state: false
|
|
|
|
}
|
|
|
|
})
|
2021-01-24 02:25:43 +01:00
|
|
|
}
|
2021-01-11 21:36:57 +01:00
|
|
|
}
|
2021-01-04 10:50:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-11 21:36:57 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Button
|
|
|
|
type='text'
|
|
|
|
content={content}
|
|
|
|
onPress={onPress}
|
|
|
|
loading={query.isLoading || mutation.isLoading}
|
|
|
|
disabled={query.isError || query.data?.blocked_by}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
},
|
|
|
|
() => true
|
|
|
|
)
|
2021-01-04 10:50:24 +01:00
|
|
|
|
|
|
|
export default RelationshipOutgoing
|