tooot/src/components/contextMenu/account.ts

248 lines
8.0 KiB
TypeScript
Raw Normal View History

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'
2022-12-19 23:06:39 +01:00
import { Alert, Platform } from 'react-native'
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-22 01:21:51 +01:00
status,
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-22 18:38:04 +01:00
account?: Partial<Mastodon.Account> & Pick<Mastodon.Account, 'id' | 'username' | 'acct'>
2022-12-22 01:21:51 +01:00
status?: Mastodon.Status
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-23 15:53:40 +01:00
const { t } = useTranslation(['common', 'componentContextMenu', 'componentRelationship'])
2022-12-03 01:08:38 +01:00
const menus: ContextMenu[][] = [[]]
2022-12-16 22:00:22 +01:00
const instanceAccount = useSelector(getInstanceAccount)
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', {
2022-12-23 15:53:40 +01:00
function: t(
`componentContextMenu:account.${theParams.payload.property}.action`,
theParams.payload.property !== 'reports'
? {
defaultValue: 'false',
context: (theParams.payload.currentValue || false).toString()
}
: { defaultValue: 'false' }
)
2022-12-03 01:08:38 +01:00
})
})
},
onError: (err: any, params) => {
const theParams = params as MutationVarsTimelineUpdateAccountProperty
displayMessage({
type: 'danger',
2022-12-03 01:08:38 +01:00
message: t('common:message.error.message', {
2022-12-23 15:53:40 +01:00
function: t(
`componentContextMenu:account.${theParams.payload.property}.action`,
theParams.payload.property !== 'reports'
? {
defaultValue: 'false',
context: (theParams.payload.currentValue || false).toString()
}
: { defaultValue: 'false' }
)
2022-12-03 01:08:38 +01:00
}),
...(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({
type: 'danger',
2022-12-03 01:08:38 +01:00
message: t('common:message.error.message', {
2022-12-23 15:53:40 +01:00
function: t(`componentContextMenu:${action}.function` as any)
2022-12-03 01:08:38 +01:00
}),
...(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
2022-12-23 15:53:40 +01:00
? t('componentContextMenu:account.following.action', {
defaultValue: 'false',
2022-12-03 01:08:38 +01:00
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
},
2022-12-23 15:53:40 +01:00
title: t('componentContextMenu:account.inLists'),
2022-12-03 15:50:15 +01:00
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
},
2022-12-23 15:53:40 +01:00
title: t('componentContextMenu:account.mute.action', {
defaultValue: 'false',
2022-12-03 01:08:38 +01:00
context: (data?.muting || false).toString()
}),
icon: data?.muting ? 'eye' : 'eye.slash'
})
}
!ownAccount &&
menus.push([
{
key: 'account-block',
item: {
onSelect: () =>
2022-12-23 15:53:40 +01:00
Alert.alert(
t('componentContextMenu:account.block.alert.title', { username: account.username }),
undefined,
[
{
text: t('common:buttons.confirm'),
style: 'destructive',
onPress: () =>
timelineMutation.mutate({
type: 'updateAccountProperty',
queryKey,
id: account.id,
payload: { property: 'block', currentValue: data?.blocking }
})
},
{
text: t('common:buttons.cancel')
}
]
),
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
},
2022-12-23 15:53:40 +01:00
title: t('componentContextMenu:account.block.action', {
defaultValue: 'false',
2022-12-03 01:08:38 +01:00
context: (data?.blocking || false).toString()
}),
icon: data?.blocking ? 'checkmark.circle' : 'xmark.circle'
},
{
key: 'account-reports',
item: {
2022-12-22 01:21:51 +01:00
onSelect: () => navigation.navigate('Tab-Shared-Report', { account, status }),
2022-12-03 01:08:38 +01:00
disabled: false,
destructive: true,
hidden: false
},
2022-12-23 15:53:40 +01:00
title: t('componentContextMenu:account.reports.action'),
2022-12-03 01:08:38 +01:00
icon: 'flag'
}
])
return menus
}
export default menuAccount