tooot/src/components/Timelines/Timeline/Shared/HeaderActions/Account.tsx

113 lines
3.0 KiB
TypeScript
Raw Normal View History

2021-01-01 23:10:47 +01:00
import haptics from '@components/haptics'
2020-12-13 14:04:25 +01:00
import { MenuContainer, MenuHeader, MenuRow } from '@components/Menu'
import { toast } from '@components/toast'
2021-01-11 21:36:57 +01:00
import {
QueryKeyTimeline,
useTimelineMutation
} from '@utils/queryHooks/timeline'
import React from 'react'
2021-01-01 23:10:47 +01:00
import { useTranslation } from 'react-i18next'
2021-01-11 21:36:57 +01:00
import { useQueryClient } from 'react-query'
2020-12-03 01:28:56 +01:00
export interface Props {
2021-01-07 19:13:09 +01:00
queryKey?: QueryKeyTimeline
2021-01-01 23:10:47 +01:00
account: Pick<Mastodon.Account, 'id' | 'acct'>
2020-12-03 01:28:56 +01:00
setBottomSheetVisible: React.Dispatch<React.SetStateAction<boolean>>
}
2021-01-12 00:12:44 +01:00
const HeaderActionsAccount: React.FC<Props> = ({
2020-12-03 01:28:56 +01:00
queryKey,
account,
setBottomSheetVisible
}) => {
2021-01-19 01:13:45 +01:00
const { t } = useTranslation('componentTimeline')
2021-01-11 21:36:57 +01:00
2020-12-18 23:58:53 +01:00
const queryClient = useQueryClient()
2021-01-11 21:36:57 +01:00
const mutateion = useTimelineMutation({
queryClient,
2021-01-19 01:13:45 +01:00
onSuccess: (_, { payload: { property } }) => {
2020-12-30 14:33:33 +01:00
haptics('Success')
2021-01-01 23:10:47 +01:00
toast({
type: 'success',
message: t('common:toastMessage.success.message', {
2021-01-19 01:13:45 +01:00
function: t(`shared.header.actions.account.${property}.function`, {
acct: account.acct
})
2021-01-01 23:10:47 +01:00
})
})
},
2021-01-19 01:13:45 +01:00
onError: (err: any, { payload: { property } }) => {
2021-01-01 23:10:47 +01:00
haptics('Error')
toast({
type: 'error',
message: t('common:toastMessage.error.message', {
2021-01-20 00:39:39 +01:00
function: t(`shared.header.actions.account.${property}.function`)
}),
...(err.status &&
typeof err.status === 'number' &&
err.data &&
err.data.error &&
typeof err.data.error === 'string' && {
description: err.data.error
})
2021-01-01 23:10:47 +01:00
})
},
onSettled: () => {
2020-12-21 21:47:15 +01:00
queryKey && queryClient.invalidateQueries(queryKey)
2020-12-03 01:28:56 +01:00
}
})
return (
<MenuContainer>
2021-01-20 00:39:39 +01:00
<MenuHeader heading={t('shared.header.actions.account.heading')} />
2020-12-03 01:28:56 +01:00
<MenuRow
onPress={() => {
setBottomSheetVisible(false)
2021-01-11 21:36:57 +01:00
mutateion.mutate({
type: 'updateAccountProperty',
queryKey,
id: account.id,
payload: { property: 'mute' }
})
2020-12-03 01:28:56 +01:00
}}
iconFront='EyeOff'
2021-01-19 01:13:45 +01:00
title={t('shared.header.actions.account.mute.button', {
2021-01-01 23:10:47 +01:00
acct: account.acct
})}
2020-12-03 01:28:56 +01:00
/>
<MenuRow
onPress={() => {
setBottomSheetVisible(false)
2021-01-11 21:36:57 +01:00
mutateion.mutate({
type: 'updateAccountProperty',
queryKey,
id: account.id,
payload: { property: 'block' }
})
2020-12-03 01:28:56 +01:00
}}
iconFront='XCircle'
2021-01-20 00:39:39 +01:00
title={t('shared.header.actions.account.block.button', {
acct: account.acct
})}
2020-12-03 01:28:56 +01:00
/>
<MenuRow
onPress={() => {
setBottomSheetVisible(false)
2021-01-11 21:36:57 +01:00
mutateion.mutate({
type: 'updateAccountProperty',
queryKey,
id: account.id,
payload: { property: 'reports' }
})
2020-12-03 01:28:56 +01:00
}}
iconFront='Flag'
2021-01-20 00:39:39 +01:00
title={t('shared.header.actions.account.reports.button', {
acct: account.acct
})}
2020-12-03 01:28:56 +01:00
/>
</MenuContainer>
)
}
2021-01-12 00:12:44 +01:00
export default HeaderActionsAccount