/* eslint jsx-a11y/tabindex-no-positive: 0 */ import { Rx, React, ReactDOM, PropTypes, Thread, DatabaseStore, FeatureUsageStore, } from 'mailspring-exports'; import { RetinaImg } from 'mailspring-component-kit'; import CopyButton from './copy-button'; import { sharingURLForThread, syncThreadToWeb, unsyncThread } from './main'; export default class ThreadSharingPopover extends React.Component { static propTypes = { thread: PropTypes.object, accountId: PropTypes.string, }; constructor(props) { super(props); this.state = { url: sharingURLForThread(props.thread), saving: false, }; this._disposable = { dispose: () => {} }; } componentDidMount() { const { thread } = this.props; this._mounted = true; this._disposable = Rx.Observable.fromQuery(DatabaseStore.find(Thread, thread.id)).subscribe(t => this.setState({ url: sharingURLForThread(t) }) ); } componentDidUpdate() { ReactDOM.findDOMNode(this).focus(); } componentWillUnmount() { this._disposable.dispose(); this._mounted = false; } _onToggleShared = async () => { this.setState({ saving: true }); let nextUrl = null; try { if (!this.state.url) { try { await FeatureUsageStore.markUsedOrUpgrade('thread-sharing', { usedUpHeader: 'All Sharing Links Used', usagePhrase: 'share', iconUrl: 'mailspring://thread-sharing/assets/ic-modal-image@2x.png', }); } catch (error) { if (error instanceof FeatureUsageStore.NoProAccessError) { if (this._mounted) this.setState({ saving: false }); return; } } await syncThreadToWeb(this.props.thread); nextUrl = sharingURLForThread(this.props.thread); } else { await unsyncThread(this.props.thread); nextUrl = null; } } catch (error) { AppEnv.reportError(error); AppEnv.showErrorDialog( `Sorry, we were unable to contact the Mailspring servers to share this thread.\n\n${ error.message }` ); } if (!this._mounted) { return; } this.setState({ saving: false, url: nextUrl }); }; _onClickInput = event => { event.target.select(); }; render() { const { url, saving } = this.state; // tabIndex is necessary for the popover's onBlur events to work properly return (
); } }