2022-06-06 22:49:43 +02:00
|
|
|
import analytics from '@components/analytics'
|
|
|
|
import { displayMessage } from '@components/Message'
|
2022-06-14 23:32:35 +02:00
|
|
|
import { useRelationshipQuery } from '@utils/queryHooks/relationship'
|
2022-06-06 22:49:43 +02:00
|
|
|
import {
|
|
|
|
MutationVarsTimelineUpdateAccountProperty,
|
|
|
|
QueryKeyTimeline,
|
|
|
|
useTimelineMutation
|
|
|
|
} from '@utils/queryHooks/timeline'
|
|
|
|
import { getInstanceAccount } from '@utils/slices/instancesSlice'
|
|
|
|
import { useTheme } from '@utils/styles/ThemeManager'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
import { ContextMenuAction } from 'react-native-context-menu-view'
|
|
|
|
import { useQueryClient } from 'react-query'
|
|
|
|
import { useSelector } from 'react-redux'
|
|
|
|
|
|
|
|
export interface Props {
|
2022-06-07 20:07:14 +02:00
|
|
|
actions: ContextMenuAction[]
|
2022-06-14 23:32:35 +02:00
|
|
|
type: 'status' | 'account' // Do not need to fetch relationship in timeline
|
2022-06-07 20:07:14 +02:00
|
|
|
queryKey?: QueryKeyTimeline
|
2022-06-06 22:49:43 +02:00
|
|
|
rootQueryKey?: QueryKeyTimeline
|
2022-06-07 20:07:14 +02:00
|
|
|
id: Mastodon.Account['id']
|
2022-06-06 22:49:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const contextMenuAccount = ({
|
2022-06-07 20:07:14 +02:00
|
|
|
actions,
|
2022-06-14 23:32:35 +02:00
|
|
|
type,
|
2022-06-06 22:49:43 +02:00
|
|
|
queryKey,
|
2022-06-07 20:07:14 +02:00
|
|
|
rootQueryKey,
|
|
|
|
id: accountId
|
2022-06-06 22:49:43 +02:00
|
|
|
}: Props) => {
|
|
|
|
const { theme } = useTheme()
|
|
|
|
const { t } = useTranslation('componentContextMenu')
|
|
|
|
|
|
|
|
const queryClient = useQueryClient()
|
2022-08-19 11:53:16 +02:00
|
|
|
const mutation = useTimelineMutation({
|
2022-06-06 22:49:43 +02:00
|
|
|
onSuccess: (_, params) => {
|
2022-06-14 23:32:35 +02:00
|
|
|
queryClient.refetchQueries(['Relationship', { id: accountId }])
|
2022-06-06 22:49:43 +02:00
|
|
|
const theParams = params as MutationVarsTimelineUpdateAccountProperty
|
|
|
|
displayMessage({
|
|
|
|
theme,
|
|
|
|
type: 'success',
|
|
|
|
message: t('common:message.success.message', {
|
2022-06-14 23:32:35 +02:00
|
|
|
function: t(`account.${theParams.payload.property}.action`, {
|
2022-08-08 23:00:46 +02:00
|
|
|
...(theParams.payload.property !== 'reports' && {
|
|
|
|
context: (theParams.payload.currentValue || false).toString()
|
2022-06-14 23:32:35 +02:00
|
|
|
})
|
|
|
|
})
|
2022-06-06 22:49:43 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
onError: (err: any, params) => {
|
|
|
|
const theParams = params as MutationVarsTimelineUpdateAccountProperty
|
|
|
|
displayMessage({
|
|
|
|
theme,
|
|
|
|
type: 'error',
|
|
|
|
message: t('common:message.error.message', {
|
2022-06-14 23:32:35 +02:00
|
|
|
function: t(`account.${theParams.payload.property}.action`, {
|
2022-08-08 23:00:46 +02:00
|
|
|
...(theParams.payload.property !== 'reports' && {
|
|
|
|
context: (theParams.payload.currentValue || false).toString()
|
2022-06-14 23:32:35 +02:00
|
|
|
})
|
|
|
|
})
|
2022-06-06 22:49:43 +02:00
|
|
|
}),
|
|
|
|
...(err.status &&
|
|
|
|
typeof err.status === 'number' &&
|
|
|
|
err.data &&
|
|
|
|
err.data.error &&
|
|
|
|
typeof err.data.error === 'string' && {
|
2022-08-19 11:53:16 +02:00
|
|
|
description: err.data.error
|
|
|
|
})
|
2022-06-06 22:49:43 +02:00
|
|
|
})
|
|
|
|
},
|
|
|
|
onSettled: () => {
|
|
|
|
queryKey && queryClient.invalidateQueries(queryKey)
|
|
|
|
rootQueryKey && queryClient.invalidateQueries(rootQueryKey)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const instanceAccount = useSelector(
|
|
|
|
getInstanceAccount,
|
|
|
|
(prev, next) => prev.id === next.id
|
|
|
|
)
|
2022-06-07 20:07:14 +02:00
|
|
|
const ownAccount = instanceAccount?.id === accountId
|
2022-06-06 22:49:43 +02:00
|
|
|
|
2022-06-14 23:32:35 +02:00
|
|
|
const { data: relationship } = useRelationshipQuery({
|
|
|
|
id: accountId,
|
|
|
|
options: { enabled: type === 'account' }
|
|
|
|
})
|
|
|
|
|
2022-06-06 22:49:43 +02:00
|
|
|
if (!ownAccount) {
|
2022-06-14 23:32:35 +02:00
|
|
|
actions.push(
|
|
|
|
{
|
|
|
|
id: 'account-mute',
|
|
|
|
title: t('account.mute.action', {
|
|
|
|
context: (relationship?.muting || false).toString()
|
|
|
|
}),
|
|
|
|
systemIcon: 'eye.slash'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'account-block',
|
|
|
|
title: t('account.block.action', {
|
|
|
|
context: (relationship?.blocking || false).toString()
|
|
|
|
}),
|
|
|
|
systemIcon: 'xmark.circle',
|
|
|
|
destructive: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'account-reports',
|
|
|
|
title: t('account.reports.action'),
|
|
|
|
systemIcon: 'flag',
|
|
|
|
destructive: true
|
|
|
|
}
|
|
|
|
)
|
2022-06-06 22:49:43 +02:00
|
|
|
}
|
|
|
|
|
2022-06-14 23:32:35 +02:00
|
|
|
return (index: number) => {
|
|
|
|
if (actions[index].id === 'account-mute') {
|
|
|
|
analytics('timeline_shared_headeractions_account_mute_press', {
|
|
|
|
page: queryKey && queryKey[1].page
|
|
|
|
})
|
2022-08-19 11:53:16 +02:00
|
|
|
mutation.mutate({
|
2022-06-14 23:32:35 +02:00
|
|
|
type: 'updateAccountProperty',
|
|
|
|
queryKey,
|
|
|
|
id: accountId,
|
|
|
|
payload: { property: 'mute', currentValue: relationship?.muting }
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (actions[index].id === 'account-block') {
|
|
|
|
analytics('timeline_shared_headeractions_account_block_press', {
|
|
|
|
page: queryKey && queryKey[1].page
|
|
|
|
})
|
2022-08-19 11:53:16 +02:00
|
|
|
mutation.mutate({
|
2022-06-14 23:32:35 +02:00
|
|
|
type: 'updateAccountProperty',
|
|
|
|
queryKey,
|
|
|
|
id: accountId,
|
|
|
|
payload: { property: 'block', currentValue: relationship?.blocking }
|
|
|
|
})
|
|
|
|
}
|
2022-08-19 11:53:16 +02:00
|
|
|
if (actions[index].id === 'account-reports') {
|
2022-06-14 23:32:35 +02:00
|
|
|
analytics('timeline_shared_headeractions_account_reports_press', {
|
|
|
|
page: queryKey && queryKey[1].page
|
|
|
|
})
|
2022-08-19 11:53:16 +02:00
|
|
|
mutation.mutate({
|
2022-06-14 23:32:35 +02:00
|
|
|
type: 'updateAccountProperty',
|
|
|
|
queryKey,
|
|
|
|
id: accountId,
|
|
|
|
payload: { property: 'reports' }
|
|
|
|
})
|
2022-06-06 22:49:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default contextMenuAccount
|