import React, { useState } from 'react'; import { defineMessages, useIntl, FormattedMessage } from 'react-intl'; import { useHistory } from 'react-router-dom'; import { remoteInteraction } from 'soapbox/actions/interactions'; import { Button, Form, Input, Modal, Stack, Text } from 'soapbox/components/ui'; import { useAppSelector, useAppDispatch, useFeatures, useInstance, useRegistrationStatus } from 'soapbox/hooks'; import { selectAccount } from 'soapbox/selectors'; import toast from 'soapbox/toast'; const messages = defineMessages({ accountPlaceholder: { id: 'remote_interaction.account_placeholder', defaultMessage: 'Enter your username@domain you want to act from' }, userNotFoundError: { id: 'remote_interaction.user_not_found_error', defaultMessage: 'Couldn\'t find given user' }, }); interface IUnauthorizedModal { /** Unauthorized action type. */ action: 'FOLLOW' | 'REPLY' | 'REBLOG' | 'FAVOURITE' | 'DISLIKE' | 'POLL_VOTE' | 'JOIN'; /** Close event handler. */ onClose: (modalType: string) => void; /** ActivityPub ID of the account OR status being acted upon. */ ap_id?: string; /** Account ID of the account being acted upon. */ account?: string; } /** Modal to display when a logged-out user tries to do something that requires login. */ const UnauthorizedModal: React.FC = ({ action, onClose, account: accountId, ap_id: apId }) => { const intl = useIntl(); const history = useHistory(); const dispatch = useAppDispatch(); const instance = useInstance(); const { isOpen } = useRegistrationStatus(); const username = useAppSelector(state => selectAccount(state, accountId!)?.display_name); const features = useFeatures(); const [account, setAccount] = useState(''); const onAccountChange: React.ChangeEventHandler = e => { setAccount(e.target.value); }; const onClickClose = () => { onClose('UNAUTHORIZED'); }; const onSubmit: React.FormEventHandler = e => { e.preventDefault(); dispatch(remoteInteraction(apId!, account)) .then(url => { window.open(url, '_new', 'noopener,noreferrer'); onClose('UNAUTHORIZED'); }) .catch(error => { if (error.message === 'Couldn\'t find user') { toast.error(intl.formatMessage(messages.userNotFoundError)); } }); }; const onLogin = () => { history.push('/login'); onClickClose(); }; const onRegister = () => { history.push('/signup'); onClickClose(); }; const renderRemoteInteractions = () => { let header; let button; if (action === 'FOLLOW') { header = ; button = ; } else if (action === 'REPLY') { header = ; button = ; } else if (action === 'REBLOG') { header = ; button = ; } else if (action === 'FAVOURITE') { header = ; button = ; } else if (action === 'DISLIKE') { header = ; button = ; } else if (action === 'POLL_VOTE') { header = ; button = ; } else if (action === 'JOIN') { header = ; button = ; } return ( } secondaryAction={isOpen ? onRegister : undefined} secondaryText={isOpen ? : undefined} >
{isOpen && ( )}
); }; if (action && features.remoteInteractions && features.federating) { return renderRemoteInteractions(); } return ( } onClose={onClickClose} confirmationAction={onLogin} confirmationText={} secondaryAction={isOpen ? onRegister : undefined} secondaryText={isOpen ? : undefined} > ); }; export default UnauthorizedModal;