import React from 'react'; import PropTypes from 'prop-types'; import { connect } from 'react-redux'; import { defineMessages, injectIntl, FormattedMessage } from 'react-intl'; import { Link } from 'react-router-dom'; import ImmutablePureComponent from 'react-immutable-pure-component'; import IconButton from 'soapbox/components/icon_button'; import snackbar from 'soapbox/actions/snackbar'; import { remoteInteraction } from 'soapbox/actions/interactions'; import { getFeatures } from 'soapbox/utils/features'; const messages = defineMessages({ close: { id: 'lightbox.close', defaultMessage: 'Close' }, 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' }, }); const mapStateToProps = (state, props) => { const instance = state.get('instance'); const features = getFeatures(instance); if (props.action !== 'FOLLOW') { return { features, siteTitle: state.getIn(['instance', 'title']), remoteInteractionsAPI: features.remoteInteractionsAPI, }; } const userName = state.getIn(['accounts', props.account, 'display_name']); return { features, siteTitle: state.getIn(['instance', 'title']), userName, remoteInteractionsAPI: features.remoteInteractionsAPI, }; }; const mapDispatchToProps = dispatch => ({ dispatch, onRemoteInteraction(ap_id, account) { return dispatch(remoteInteraction(ap_id, account)); }, }); class UnauthorizedModal extends ImmutablePureComponent { static propTypes = { intl: PropTypes.object.isRequired, features: PropTypes.object.isRequired, onClose: PropTypes.func.isRequired, onRemoteInteraction: PropTypes.func.isRequired, userName: PropTypes.string, }; state = { account: '', }; onAccountChange = e => { this.setState({ account: e.target.value }); } onClickClose = () => { this.props.onClose('UNAUTHORIZED'); }; onClickProceed = e => { e.preventDefault(); const { intl, ap_id, dispatch, onClose, onRemoteInteraction } = this.props; const { account } = this.state; onRemoteInteraction(ap_id, account) .then(url => { window.open(url, '_new', 'noopener,noreferrer'); onClose('UNAUTHORIZED'); }) .catch(error => { if (error.message === 'Couldn\'t find user') { dispatch(snackbar.error(intl.formatMessage(messages.userNotFoundError))); } }); } renderRemoteInteractions() { const { intl, siteTitle, userName, action } = this.props; const { account } = this.state; 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 === 'POLL_VOTE') { header = ; button = ; } return (

{header}

); } render() { const { intl, features, siteTitle, action } = this.props; if (action && features.remoteInteractionsAPI && features.federating) return this.renderRemoteInteractions(); return (

, }} />
); } } export default injectIntl(connect(mapStateToProps, mapDispatchToProps)(UnauthorizedModal));