import React from 'react'; import { defineMessages, useIntl } from 'react-intl'; import { blockAccount, unblockAccount, muteAccount, unmuteAccount, authorizeFollowRequest, rejectFollowRequest, } from 'soapbox/actions/accounts'; import { openModal } from 'soapbox/actions/modals'; import { useFollow } from 'soapbox/api/hooks'; import { Button, HStack } from 'soapbox/components/ui'; import { useAppDispatch, useFeatures, useLoggedIn } from 'soapbox/hooks'; import type { Account } from 'soapbox/schemas'; const messages = defineMessages({ block: { id: 'account.block', defaultMessage: 'Block @{name}' }, blocked: { id: 'account.blocked', defaultMessage: 'Blocked' }, edit_profile: { id: 'account.edit_profile', defaultMessage: 'Edit profile' }, follow: { id: 'account.follow', defaultMessage: 'Follow' }, mute: { id: 'account.mute', defaultMessage: 'Mute @{name}' }, remote_follow: { id: 'account.remote_follow', defaultMessage: 'Remote follow' }, requested: { id: 'account.requested', defaultMessage: 'Awaiting approval. Click to cancel follow request' }, requested_small: { id: 'account.requested_small', defaultMessage: 'Awaiting approval' }, unblock: { id: 'account.unblock', defaultMessage: 'Unblock @{name}' }, unfollow: { id: 'account.unfollow', defaultMessage: 'Unfollow' }, unmute: { id: 'account.unmute', defaultMessage: 'Unmute @{name}' }, authorize: { id: 'follow_request.authorize', defaultMessage: 'Authorize' }, reject: { id: 'follow_request.reject', defaultMessage: 'Reject' }, }); interface IActionButton { /** Target account for the action. */ account: Account; /** Type of action to prioritize, eg on Blocks and Mutes pages. */ actionType?: 'muting' | 'blocking' | 'follow_request'; /** Displays shorter text on the "Awaiting approval" button. */ small?: boolean; } /** * Circumstantial action button (usually "Follow") to display on accounts. * May say "Unblock" or something else, depending on the relationship and * `actionType` prop. */ const ActionButton: React.FC = ({ account, actionType, small }) => { const dispatch = useAppDispatch(); const features = useFeatures(); const intl = useIntl(); const { isLoggedIn, me } = useLoggedIn(); const { follow, unfollow } = useFollow(); const handleFollow = () => { if (account.relationship?.following || account.relationship?.requested) { unfollow(account.id); } else { follow(account.id); } }; const handleBlock = () => { if (account.relationship?.blocking) { dispatch(unblockAccount(account.id)); } else { dispatch(blockAccount(account.id)); } }; const handleMute = () => { if (account.relationship?.muting) { dispatch(unmuteAccount(account.id)); } else { dispatch(muteAccount(account.id)); } }; const handleAuthorize = () => { dispatch(authorizeFollowRequest(account.id)); }; const handleReject = () => { dispatch(rejectFollowRequest(account.id)); }; const handleRemoteFollow = () => { dispatch(openModal('UNAUTHORIZED', { action: 'FOLLOW', account: account.id, ap_id: account.url, })); }; /** Handles actionType='muting' */ const mutingAction = () => { const isMuted = account.relationship?.muting; const messageKey = isMuted ? messages.unmute : messages.mute; const text = intl.formatMessage(messageKey, { name: account.username }); return ( ); } else if (account.relationship?.blocking) { // Unblock return (