/* eslint jsx-a11y/tabindex-no-positive: 0 */ import { Rx, React, ReactDOM, PropTypes, Thread, DatabaseStore } from 'mailspring-exports'; import { RetinaImg } from 'mailspring-component-kit'; import CopyButton from './copy-button'; import { isShared, 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 () => { const { thread } = this.props; this.setState({ saving: true }); try { if (!isShared(thread)) { await syncThreadToWeb(thread); } else { await unsyncThread(thread); } } 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: sharingURLForThread(thread) }); }; _onClickInput = event => { event.target.select(); }; render() { const { url, saving } = this.state; // tabIndex is necessary for the popover's onBlur events to work properly return (
{saving ? ( ) : ( )}
Learn More
); } }