tooot/src/components/contextMenu/status.ts

258 lines
8.0 KiB
TypeScript
Raw Normal View History

2022-06-06 22:49:43 +02:00
import { displayMessage } from '@components/Message'
import { useNavigation } from '@react-navigation/native'
import { NativeStackNavigationProp } from '@react-navigation/native-stack'
import { useQueryClient } from '@tanstack/react-query'
import apiInstance from '@utils/api/instance'
import { featureCheck } from '@utils/helpers/featureCheck'
import { checkIsMyAccount } from '@utils/helpers/isMyAccount'
2023-01-04 22:39:29 +01:00
import { RootStackParamList, useNavState } from '@utils/navigation/navigators'
2022-06-06 22:49:43 +02:00
import {
MutationVarsTimelineUpdateStatusProperty,
QueryKeyTimeline,
useTimelineMutation
} from '@utils/queryHooks/timeline'
import { useAccountStorage } from '@utils/storage/actions'
2022-06-06 22:49:43 +02:00
import { useTheme } from '@utils/styles/ThemeManager'
import { useTranslation } from 'react-i18next'
import { Alert } from 'react-native'
2022-06-06 22:49:43 +02:00
2022-12-03 01:08:38 +01:00
const menuStatus = ({
status,
2023-01-04 22:39:29 +01:00
queryKey
2022-12-03 01:08:38 +01:00
}: {
status?: Mastodon.Status
queryKey?: QueryKeyTimeline
2023-01-08 16:59:35 +01:00
}): ContextMenu => {
2022-12-03 01:08:38 +01:00
if (!status || !queryKey) return []
2022-06-06 22:49:43 +02:00
2022-11-22 21:39:25 +01:00
const navigation = useNavigation<NativeStackNavigationProp<RootStackParamList, 'Screen-Tabs'>>()
2022-06-06 22:49:43 +02:00
const { theme } = useTheme()
2022-12-23 15:53:40 +01:00
const { t } = useTranslation(['common', 'componentContextMenu'])
2022-06-06 22:49:43 +02:00
2023-01-04 22:39:29 +01:00
const navigationState = useNavState()
2022-06-06 22:49:43 +02:00
const queryClient = useQueryClient()
const mutation = useTimelineMutation({
onMutate: true,
onError: (err: any, params, oldData) => {
2022-11-22 21:39:25 +01:00
const theFunction = (params as MutationVarsTimelineUpdateStatusProperty).payload
2023-01-01 18:37:05 +01:00
? (params as MutationVarsTimelineUpdateStatusProperty).payload.type
2022-06-06 22:49:43 +02:00
: 'delete'
displayMessage({
theme,
type: 'error',
message: t('common:message.error.message', {
2022-12-23 15:53:40 +01:00
function: t(`componentContextMenu:status.${theFunction}.action` as any)
2022-06-06 22:49:43 +02:00
}),
2022-08-19 11:58:49 +02:00
...(err?.status &&
2022-06-06 22:49:43 +02:00
typeof err.status === 'number' &&
err.data &&
err.data.error &&
typeof err.data.error === 'string' && {
2022-11-22 21:39:25 +01:00
description: err.data.error
})
2022-06-06 22:49:43 +02:00
})
queryClient.setQueryData(queryKey, oldData)
}
})
2023-01-08 16:59:35 +01:00
const menus: ContextMenu = []
2022-12-03 01:08:38 +01:00
2023-01-17 12:57:37 +01:00
const [accountAcct] = useAccountStorage.string('auth.account.acct')
const isMyAccount = checkIsMyAccount(status.account.id)
2022-12-03 01:08:38 +01:00
const canEditPost = featureCheck('edit_post')
2022-06-06 22:49:43 +02:00
2022-12-24 01:59:18 +01:00
menus.push([
{
2023-01-08 16:59:35 +01:00
type: 'item',
2022-12-24 01:59:18 +01:00
key: 'status-edit',
2023-01-08 16:59:35 +01:00
props: {
2022-12-24 01:59:18 +01:00
onSelect: async () => {
let replyToStatus: Mastodon.Status | undefined = undefined
if (status.in_reply_to_id) {
replyToStatus = await apiInstance<Mastodon.Status>({
2022-12-03 01:08:38 +01:00
method: 'get',
2022-12-24 01:59:18 +01:00
url: `statuses/${status.in_reply_to_id}`
}).then(res => res.body)
}
apiInstance<{
id: Mastodon.Status['id']
text: NonNullable<Mastodon.Status['text']>
spoiler_text: Mastodon.Status['spoiler_text']
}>({
method: 'get',
url: `statuses/${status.id}/source`
}).then(res => {
navigation.navigate('Screen-Compose', {
type: 'edit',
incomingStatus: {
...status,
text: res.body.text,
spoiler_text: res.body.spoiler_text
},
...(replyToStatus && { replyToStatus }),
2023-01-04 22:39:29 +01:00
navigationState
2022-12-03 01:08:38 +01:00
})
2022-12-24 01:59:18 +01:00
})
2022-12-03 01:08:38 +01:00
},
2022-12-24 01:59:18 +01:00
disabled: false,
destructive: false,
hidden: !isMyAccount || !canEditPost
2022-06-06 22:49:43 +02:00
},
2022-12-24 01:59:18 +01:00
title: t('componentContextMenu:status.edit.action'),
icon: 'square.and.pencil'
},
{
2023-01-08 16:59:35 +01:00
type: 'item',
2022-12-24 01:59:18 +01:00
key: 'status-delete-edit',
2023-01-08 16:59:35 +01:00
props: {
2022-12-24 01:59:18 +01:00
onSelect: () =>
Alert.alert(
t('componentContextMenu:status.deleteEdit.alert.title'),
t('componentContextMenu:status.deleteEdit.alert.message'),
[
{
text: t('common:buttons.confirm'),
style: 'destructive',
onPress: async () => {
let replyToStatus: Mastodon.Status | undefined = undefined
if (status.in_reply_to_id) {
replyToStatus = await apiInstance<Mastodon.Status>({
method: 'get',
url: `statuses/${status.in_reply_to_id}`
}).then(res => res.body)
2022-12-23 15:53:40 +01:00
}
2022-12-24 01:59:18 +01:00
mutation
2023-01-04 22:39:29 +01:00
.mutateAsync({ type: 'deleteItem', source: 'statuses', id: status.id })
2022-12-24 01:59:18 +01:00
.then(res => {
navigation.navigate('Screen-Compose', {
type: 'deleteEdit',
incomingStatus: res.body as Mastodon.Status,
...(replyToStatus && { replyToStatus }),
2023-01-04 22:39:29 +01:00
navigationState
2022-12-24 01:59:18 +01:00
})
})
2022-12-03 01:08:38 +01:00
}
2022-12-24 01:59:18 +01:00
},
{
text: t('common:buttons.cancel')
2022-12-03 01:08:38 +01:00
}
2022-12-24 01:59:18 +01:00
]
),
disabled: false,
destructive: true,
hidden: !isMyAccount
2022-12-03 01:08:38 +01:00
},
2022-12-24 01:59:18 +01:00
title: t('componentContextMenu:status.deleteEdit.action'),
icon: 'pencil.and.outline'
},
{
2023-01-08 16:59:35 +01:00
type: 'item',
2022-12-24 01:59:18 +01:00
key: 'status-delete',
2023-01-08 16:59:35 +01:00
props: {
2022-12-24 01:59:18 +01:00
onSelect: () =>
Alert.alert(
t('componentContextMenu:status.delete.alert.title'),
t('componentContextMenu:status.delete.alert.message'),
[
{
text: t('common:buttons.confirm'),
style: 'destructive',
onPress: async () => {
2023-01-04 22:39:29 +01:00
mutation.mutate({ type: 'deleteItem', source: 'statuses', id: status.id })
2022-12-24 01:59:18 +01:00
}
},
{
text: t('common:buttons.cancel'),
style: 'default'
2022-12-03 01:08:38 +01:00
}
2022-12-24 01:59:18 +01:00
]
),
disabled: false,
destructive: true,
hidden: !isMyAccount
2022-12-24 01:59:18 +01:00
},
title: t('componentContextMenu:status.delete.action'),
icon: 'trash'
}
])
menus.push([
{
2023-01-08 16:59:35 +01:00
type: 'item',
2022-12-24 01:59:18 +01:00
key: 'status-mute',
2023-01-08 16:59:35 +01:00
props: {
2022-12-24 01:59:18 +01:00
onSelect: () =>
mutation.mutate({
type: 'updateStatusProperty',
2023-01-01 18:37:05 +01:00
status,
2022-12-24 01:59:18 +01:00
payload: {
2023-01-02 23:18:22 +01:00
type: 'muted',
to: !status.muted
2022-12-24 01:59:18 +01:00
}
}),
disabled: false,
destructive: false,
2023-01-17 12:57:37 +01:00
hidden:
!isMyAccount &&
2023-01-17 12:57:37 +01:00
queryKey[1].page !== 'Notifications' &&
2023-01-30 12:55:35 +01:00
!status.mentions?.find(
2023-01-17 12:57:37 +01:00
mention => mention.acct === accountAcct && mention.username === accountAcct
) &&
!status.muted
2022-12-24 01:59:18 +01:00
},
title: t('componentContextMenu:status.mute.action', {
defaultValue: 'false',
context: (status.muted || false).toString()
}),
icon: status.muted ? 'speaker' : 'speaker.slash'
},
{
2023-01-08 16:59:35 +01:00
type: 'item',
2022-12-24 01:59:18 +01:00
key: 'status-pin',
2023-01-08 16:59:35 +01:00
props: {
2022-12-24 01:59:18 +01:00
onSelect: () =>
// Also note that reblogs cannot be pinned.
mutation.mutate({
type: 'updateStatusProperty',
2023-01-01 18:37:05 +01:00
status,
2022-12-24 01:59:18 +01:00
payload: {
2023-01-02 23:18:22 +01:00
type: 'pinned',
to: !status.pinned
2022-12-24 01:59:18 +01:00
}
}),
2023-01-01 18:54:42 +01:00
disabled: status.visibility !== 'public' && status.visibility !== 'unlisted',
2022-12-24 01:59:18 +01:00
destructive: false,
hidden: !isMyAccount
2022-12-24 01:59:18 +01:00
},
title: t('componentContextMenu:status.pin.action', {
defaultValue: 'false',
context: (status.pinned || false).toString()
}),
icon: status.pinned ? 'pin.slash' : 'pin'
2023-01-26 23:07:13 +01:00
},
{
type: 'item',
key: 'status-filter',
props: {
onSelect: () =>
// @ts-ignore
navigation.navigate('Tab-Shared-Filter', { source: 'status', status, queryKey }),
2023-01-26 23:07:13 +01:00
disabled: false,
destructive: false,
hidden: !('filtered' in status)
},
title: t('componentContextMenu:status.filter.action', {
defaultValue: 'false',
context: (!!status.filtered?.length).toString()
}),
icon: status.pinned ? 'rectangle.badge.checkmark' : 'rectangle.badge.xmark'
2022-12-24 01:59:18 +01:00
}
])
2022-12-03 01:08:38 +01:00
return menus
2022-06-06 22:49:43 +02:00
}
2022-12-03 01:08:38 +01:00
export default menuStatus