tooot/src/components/ContextMenu/instance.ts

98 lines
2.8 KiB
TypeScript
Raw Normal View History

2022-06-06 22:49:43 +02:00
import { displayMessage } from '@components/Message'
2022-11-22 21:39:25 +01:00
import { QueryKeyTimeline, useTimelineMutation } from '@utils/queryHooks/timeline'
2022-06-06 22:49:43 +02:00
import { getInstanceUrl } from '@utils/slices/instancesSlice'
import { useTheme } from '@utils/styles/ThemeManager'
import { useTranslation } from 'react-i18next'
import { Alert, Platform } from 'react-native'
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-06 22:49:43 +02:00
status: Mastodon.Status
queryKey: QueryKeyTimeline
rootQueryKey?: QueryKeyTimeline
}
2022-11-22 21:39:25 +01:00
const contextMenuInstance = ({ actions, status, queryKey, rootQueryKey }: Props) => {
2022-06-06 22:49:43 +02:00
const { t } = useTranslation('componentContextMenu')
const { theme } = useTheme()
const currentInstance = useSelector(getInstanceUrl)
2022-06-19 12:51:44 +02:00
const instance = status?.uri && status.uri.split(new RegExp(/\/\/(.*?)\//))[1]
2022-06-06 22:49:43 +02:00
const queryClient = useQueryClient()
const mutation = useTimelineMutation({
onSettled: () => {
displayMessage({
theme,
type: 'success',
message: t('common:message.success.message', {
function: t(`instance.block.action`, { instance })
})
})
queryClient.invalidateQueries(queryKey)
rootQueryKey && queryClient.invalidateQueries(rootQueryKey)
}
})
if (currentInstance !== instance && instance) {
switch (Platform.OS) {
case 'ios':
2022-06-07 20:07:14 +02:00
actions.push({
2022-06-06 22:49:43 +02:00
id: 'instance',
title: t('instance.title'),
actions: [
{
id: 'instance-block',
title: t('instance.block.action', { instance }),
destructive: true
}
]
})
break
default:
2022-06-07 20:07:14 +02:00
actions.push({
2022-06-06 22:49:43 +02:00
id: 'instance-block',
title: t('instance.block.action', { instance }),
destructive: true
})
break
}
}
return (index: number) => {
2022-11-22 21:39:25 +01:00
if (typeof index !== 'number' || !actions[index]) {
return // For Android
}
if (
actions[index].id === 'instance-block' ||
2022-11-22 21:39:25 +01:00
(actions[index].id === 'instance' && actions[index].actions?.[0].id === 'instance-block')
) {
Alert.alert(
t('instance.block.alert.title', { instance }),
t('instance.block.alert.message'),
[
{
text: t('instance.block.alert.buttons.confirm'),
style: 'destructive',
onPress: () => {
mutation.mutate({
type: 'domainBlock',
queryKey,
domain: instance
})
2022-06-06 22:49:43 +02:00
}
},
{
text: t('common:buttons.cancel')
}
]
)
2022-06-06 22:49:43 +02:00
}
}
}
export default contextMenuInstance