tooot/src/components/Timelines/Timeline/Shared/HeaderDefault/ActionsAccount.tsx

123 lines
3.2 KiB
TypeScript
Raw Normal View History

2020-12-13 14:04:25 +01:00
import client from '@api/client'
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-01 23:10:47 +01:00
import React, { useCallback } from 'react'
import { useTranslation } from 'react-i18next'
import { useMutation, useQueryClient } from 'react-query'
2020-12-03 01:28:56 +01:00
export interface Props {
2020-12-21 21:47:15 +01:00
queryKey?: QueryKey.Timeline
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>>
}
const HeaderDefaultActionsAccount: React.FC<Props> = ({
queryKey,
account,
setBottomSheetVisible
}) => {
2021-01-01 23:10:47 +01:00
const { t } = useTranslation()
2020-12-18 23:58:53 +01:00
const queryClient = useQueryClient()
2021-01-01 23:10:47 +01:00
const fireMutation = useCallback(
async ({ type }: { type: 'mute' | 'block' | 'reports' }) => {
switch (type) {
case 'mute':
case 'block':
return client({
method: 'post',
instance: 'local',
url: `accounts/${account.id}/${type}`
})
break
case 'reports':
return client({
method: 'post',
instance: 'local',
url: `reports`,
params: {
account_id: account.id!
}
})
break
}
},
[]
)
2020-12-18 23:58:53 +01:00
const { mutate } = useMutation(fireMutation, {
2021-01-01 23:10:47 +01:00
onSuccess: (_, { type }) => {
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', {
function: t(
`timeline:shared.header.default.actions.account.${type}.function`,
{ acct: account.acct }
)
})
})
},
onError: (_, { type }) => {
haptics('Error')
toast({
type: 'error',
message: t('common:toastMessage.error.message', {
function: t(
`timeline:shared.header.default.actions.account.${type}.function`,
{ acct: account.acct }
)
})
})
},
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-01 23:10:47 +01:00
<MenuHeader
heading={t('timeline:shared.header.default.actions.account.heading')}
/>
2020-12-03 01:28:56 +01:00
<MenuRow
onPress={() => {
setBottomSheetVisible(false)
2021-01-01 23:10:47 +01:00
mutate({ type: 'mute' })
2020-12-03 01:28:56 +01:00
}}
iconFront='EyeOff'
2021-01-01 23:10:47 +01:00
title={t('timeline:shared.header.default.actions.account.mute.button', {
acct: account.acct
})}
2020-12-03 01:28:56 +01:00
/>
<MenuRow
onPress={() => {
setBottomSheetVisible(false)
2021-01-01 23:10:47 +01:00
mutate({ type: 'block' })
2020-12-03 01:28:56 +01:00
}}
iconFront='XCircle'
2021-01-01 23:10:47 +01:00
title={t(
'timeline:shared.header.default.actions.account.block.button',
{
acct: account.acct
}
)}
2020-12-03 01:28:56 +01:00
/>
<MenuRow
onPress={() => {
setBottomSheetVisible(false)
2021-01-01 23:10:47 +01:00
mutate({ type: 'reports' })
2020-12-03 01:28:56 +01:00
}}
iconFront='Flag'
2021-01-01 23:10:47 +01:00
title={t(
'timeline:shared.header.default.actions.account.report.button',
{
acct: account.acct
}
)}
2020-12-03 01:28:56 +01:00
/>
</MenuContainer>
)
}
export default HeaderDefaultActionsAccount