2022-12-03 01:08:38 +01:00
|
|
|
import haptics from '@components/haptics'
|
|
|
|
import { displayMessage } from '@components/Message'
|
2022-12-03 15:50:15 +01:00
|
|
|
import { useNavigation } from '@react-navigation/native'
|
|
|
|
import { NativeStackNavigationProp } from '@react-navigation/native-stack'
|
|
|
|
import { TabSharedStackParamList } from '@utils/navigation/navigators'
|
2022-12-03 01:08:38 +01:00
|
|
|
import {
|
|
|
|
QueryKeyRelationship,
|
|
|
|
useRelationshipMutation,
|
|
|
|
useRelationshipQuery
|
|
|
|
} from '@utils/queryHooks/relationship'
|
|
|
|
import {
|
|
|
|
MutationVarsTimelineUpdateAccountProperty,
|
|
|
|
QueryKeyTimeline,
|
|
|
|
useTimelineMutation
|
|
|
|
} from '@utils/queryHooks/timeline'
|
|
|
|
import { getInstanceAccount } from '@utils/slices/instancesSlice'
|
|
|
|
import { useEffect, useState } from 'react'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
import { Platform } from 'react-native'
|
2022-12-10 14:47:19 +01:00
|
|
|
import { useQueryClient } from '@tanstack/react-query'
|
2022-12-03 01:08:38 +01:00
|
|
|
import { useSelector } from 'react-redux'
|
|
|
|
|
|
|
|
const menuAccount = ({
|
2022-12-03 15:50:15 +01:00
|
|
|
type,
|
2022-12-03 01:08:38 +01:00
|
|
|
openChange,
|
2022-12-03 15:50:15 +01:00
|
|
|
account,
|
2022-12-03 01:08:38 +01:00
|
|
|
queryKey,
|
|
|
|
rootQueryKey
|
|
|
|
}: {
|
2022-12-03 15:50:15 +01:00
|
|
|
type: 'status' | 'account' // Where the action is coming from
|
2022-12-03 01:08:38 +01:00
|
|
|
openChange: boolean
|
2022-12-03 15:50:15 +01:00
|
|
|
account?: Pick<Mastodon.Account, 'id' | 'username'>
|
2022-12-03 01:08:38 +01:00
|
|
|
queryKey?: QueryKeyTimeline
|
|
|
|
rootQueryKey?: QueryKeyTimeline
|
|
|
|
}): ContextMenu[][] => {
|
2022-12-03 15:50:15 +01:00
|
|
|
if (!account) return []
|
2022-12-03 01:08:38 +01:00
|
|
|
|
2022-12-03 15:50:15 +01:00
|
|
|
const navigation =
|
|
|
|
useNavigation<NativeStackNavigationProp<TabSharedStackParamList, any, undefined>>()
|
2022-12-03 01:08:38 +01:00
|
|
|
const { t } = useTranslation('componentContextMenu')
|
|
|
|
|
|
|
|
const menus: ContextMenu[][] = [[]]
|
|
|
|
|
|
|
|
const instanceAccount = useSelector(getInstanceAccount, (prev, next) => prev.id === next.id)
|
2022-12-03 15:50:15 +01:00
|
|
|
const ownAccount = instanceAccount?.id === account.id
|
2022-12-03 01:08:38 +01:00
|
|
|
|
|
|
|
const [enabled, setEnabled] = useState(openChange)
|
|
|
|
useEffect(() => {
|
|
|
|
if (!ownAccount && enabled === false && openChange === true) {
|
|
|
|
setEnabled(true)
|
|
|
|
}
|
|
|
|
}, [openChange, enabled])
|
2022-12-14 23:37:41 +01:00
|
|
|
const { data, isFetched } = useRelationshipQuery({ id: account.id, options: { enabled } })
|
2022-12-03 01:08:38 +01:00
|
|
|
|
|
|
|
const queryClient = useQueryClient()
|
|
|
|
const timelineMutation = useTimelineMutation({
|
|
|
|
onSuccess: (_, params) => {
|
2022-12-03 15:50:15 +01:00
|
|
|
queryClient.refetchQueries(['Relationship', { id: account.id }])
|
2022-12-03 01:08:38 +01:00
|
|
|
const theParams = params as MutationVarsTimelineUpdateAccountProperty
|
|
|
|
displayMessage({
|
|
|
|
type: 'success',
|
|
|
|
message: t('common:message.success.message', {
|
|
|
|
function: t(`account.${theParams.payload.property}.action`, {
|
|
|
|
...(theParams.payload.property !== 'reports' && {
|
|
|
|
context: (theParams.payload.currentValue || false).toString()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
onError: (err: any, params) => {
|
|
|
|
const theParams = params as MutationVarsTimelineUpdateAccountProperty
|
|
|
|
displayMessage({
|
2022-12-05 23:15:07 +01:00
|
|
|
type: 'danger',
|
2022-12-03 01:08:38 +01:00
|
|
|
message: t('common:message.error.message', {
|
|
|
|
function: t(`account.${theParams.payload.property}.action`, {
|
|
|
|
...(theParams.payload.property !== 'reports' && {
|
|
|
|
context: (theParams.payload.currentValue || false).toString()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}),
|
|
|
|
...(err.status &&
|
|
|
|
typeof err.status === 'number' &&
|
|
|
|
err.data &&
|
|
|
|
err.data.error &&
|
|
|
|
typeof err.data.error === 'string' && {
|
|
|
|
description: err.data.error
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
onSettled: () => {
|
|
|
|
queryKey && queryClient.invalidateQueries(queryKey)
|
|
|
|
rootQueryKey && queryClient.invalidateQueries(rootQueryKey)
|
|
|
|
}
|
|
|
|
})
|
2022-12-03 15:50:15 +01:00
|
|
|
const queryKeyRelationship: QueryKeyRelationship = ['Relationship', { id: account.id }]
|
2022-12-03 01:08:38 +01:00
|
|
|
const relationshipMutation = useRelationshipMutation({
|
|
|
|
onSuccess: (res, { payload: { action } }) => {
|
|
|
|
haptics('Success')
|
|
|
|
queryClient.setQueryData<Mastodon.Relationship[]>(queryKeyRelationship, [res])
|
|
|
|
if (action === 'block') {
|
2022-12-14 23:37:41 +01:00
|
|
|
const queryKey = ['Timeline', { page: 'Following' }]
|
|
|
|
queryClient.invalidateQueries({ queryKey, exact: false })
|
2022-12-03 01:08:38 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
onError: (err: any, { payload: { action } }) => {
|
|
|
|
displayMessage({
|
2022-12-05 23:15:07 +01:00
|
|
|
type: 'danger',
|
2022-12-03 01:08:38 +01:00
|
|
|
message: t('common:message.error.message', {
|
|
|
|
function: t(`${action}.function`)
|
|
|
|
}),
|
|
|
|
...(err.status &&
|
|
|
|
typeof err.status === 'number' &&
|
|
|
|
err.data &&
|
|
|
|
err.data.error &&
|
|
|
|
typeof err.data.error === 'string' && {
|
|
|
|
description: err.data.error
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-12-03 15:50:15 +01:00
|
|
|
if (!ownAccount && Platform.OS !== 'android' && type !== 'account') {
|
2022-12-03 01:08:38 +01:00
|
|
|
menus[0].push({
|
|
|
|
key: 'account-following',
|
|
|
|
item: {
|
|
|
|
onSelect: () =>
|
|
|
|
data &&
|
|
|
|
relationshipMutation.mutate({
|
2022-12-03 15:50:15 +01:00
|
|
|
id: account.id,
|
2022-12-03 01:08:38 +01:00
|
|
|
type: 'outgoing',
|
|
|
|
payload: { action: 'follow', state: !data?.requested ? data.following : true }
|
|
|
|
}),
|
2022-12-14 23:37:41 +01:00
|
|
|
disabled: !data || !isFetched,
|
2022-12-03 01:08:38 +01:00
|
|
|
destructive: false,
|
|
|
|
hidden: false
|
|
|
|
},
|
|
|
|
title: !data?.requested
|
|
|
|
? t('account.following.action', {
|
|
|
|
context: (data?.following || false).toString()
|
|
|
|
})
|
|
|
|
: t('componentRelationship:button.requested'),
|
|
|
|
icon: !data?.requested
|
|
|
|
? data?.following
|
|
|
|
? 'person.badge.minus'
|
|
|
|
: 'person.badge.plus'
|
|
|
|
: 'person.badge.minus'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (!ownAccount) {
|
2022-12-03 15:50:15 +01:00
|
|
|
menus[0].push({
|
|
|
|
key: 'account-list',
|
|
|
|
item: {
|
|
|
|
onSelect: () => navigation.navigate('Tab-Shared-Account-In-Lists', { account }),
|
2022-12-14 23:37:41 +01:00
|
|
|
disabled: Platform.OS !== 'android' ? !data || !isFetched : false,
|
2022-12-03 15:50:15 +01:00
|
|
|
destructive: false,
|
2022-12-14 23:37:41 +01:00
|
|
|
hidden: !isFetched || !data?.following
|
2022-12-03 15:50:15 +01:00
|
|
|
},
|
|
|
|
title: t('account.inLists'),
|
|
|
|
icon: 'checklist'
|
|
|
|
})
|
2022-12-03 01:08:38 +01:00
|
|
|
menus[0].push({
|
|
|
|
key: 'account-mute',
|
|
|
|
item: {
|
|
|
|
onSelect: () =>
|
|
|
|
timelineMutation.mutate({
|
|
|
|
type: 'updateAccountProperty',
|
|
|
|
queryKey,
|
2022-12-03 15:50:15 +01:00
|
|
|
id: account.id,
|
2022-12-03 01:08:38 +01:00
|
|
|
payload: { property: 'mute', currentValue: data?.muting }
|
|
|
|
}),
|
2022-12-14 23:37:41 +01:00
|
|
|
disabled: Platform.OS !== 'android' ? !data || !isFetched : false,
|
2022-12-03 01:08:38 +01:00
|
|
|
destructive: false,
|
|
|
|
hidden: false
|
|
|
|
},
|
|
|
|
title: t('account.mute.action', {
|
|
|
|
context: (data?.muting || false).toString()
|
|
|
|
}),
|
|
|
|
icon: data?.muting ? 'eye' : 'eye.slash'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
!ownAccount &&
|
|
|
|
menus.push([
|
|
|
|
{
|
|
|
|
key: 'account-block',
|
|
|
|
item: {
|
|
|
|
onSelect: () =>
|
|
|
|
timelineMutation.mutate({
|
|
|
|
type: 'updateAccountProperty',
|
|
|
|
queryKey,
|
2022-12-03 15:50:15 +01:00
|
|
|
id: account.id,
|
2022-12-03 01:08:38 +01:00
|
|
|
payload: { property: 'block', currentValue: data?.blocking }
|
|
|
|
}),
|
2022-12-14 23:37:41 +01:00
|
|
|
disabled: Platform.OS !== 'android' ? !data || !isFetched : false,
|
2022-12-03 01:08:38 +01:00
|
|
|
destructive: !data?.blocking,
|
|
|
|
hidden: false
|
|
|
|
},
|
|
|
|
title: t('account.block.action', {
|
|
|
|
context: (data?.blocking || false).toString()
|
|
|
|
}),
|
|
|
|
icon: data?.blocking ? 'checkmark.circle' : 'xmark.circle'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'account-reports',
|
|
|
|
item: {
|
|
|
|
onSelect: () => {
|
|
|
|
timelineMutation.mutate({
|
|
|
|
type: 'updateAccountProperty',
|
|
|
|
queryKey,
|
2022-12-03 15:50:15 +01:00
|
|
|
id: account.id,
|
2022-12-03 01:08:38 +01:00
|
|
|
payload: { property: 'reports' }
|
|
|
|
})
|
|
|
|
timelineMutation.mutate({
|
|
|
|
type: 'updateAccountProperty',
|
|
|
|
queryKey,
|
2022-12-03 15:50:15 +01:00
|
|
|
id: account.id,
|
2022-12-03 01:08:38 +01:00
|
|
|
payload: { property: 'block', currentValue: false }
|
|
|
|
})
|
|
|
|
},
|
|
|
|
disabled: false,
|
|
|
|
destructive: true,
|
|
|
|
hidden: false
|
|
|
|
},
|
|
|
|
title: t('account.reports.action'),
|
|
|
|
icon: 'flag'
|
|
|
|
}
|
|
|
|
])
|
|
|
|
|
|
|
|
return menus
|
|
|
|
}
|
|
|
|
|
|
|
|
export default menuAccount
|