2016-02-20 04:30:24 +08:00
|
|
|
import {DraftStore, React, Actions, NylasAPI, DatabaseStore, Message, Rx} from 'nylas-exports'
|
|
|
|
import {RetinaImg} from 'nylas-component-kit'
|
|
|
|
import plugin from '../package.json'
|
2016-02-24 11:19:43 +08:00
|
|
|
const PLUGIN_ID = plugin.appId[NylasEnv.config.get("env")];
|
2016-02-20 04:30:24 +08:00
|
|
|
|
|
|
|
export default class LinkTrackingButton extends React.Component {
|
|
|
|
static displayName = 'LinkTrackingButton';
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
draftClientId: React.PropTypes.string.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {enabled: false};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
const query = DatabaseStore.findBy(Message, {clientId: this.props.draftClientId});
|
2016-02-24 09:51:10 +08:00
|
|
|
this._subscription = Rx.Observable.fromQuery(query).subscribe(this.setStateFromDraft);
|
2016-02-20 04:30:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this._subscription.dispose();
|
|
|
|
}
|
|
|
|
|
2016-02-24 09:51:10 +08:00
|
|
|
setStateFromDraft = (draft)=> {
|
2016-02-20 04:30:24 +08:00
|
|
|
if (!draft) return;
|
|
|
|
const metadata = draft.metadataForPluginId(PLUGIN_ID);
|
|
|
|
this.setState({enabled: metadata ? metadata.tracked : false});
|
|
|
|
};
|
|
|
|
|
2016-02-24 09:51:10 +08:00
|
|
|
_onClick = ()=> {
|
2016-02-20 04:30:24 +08:00
|
|
|
const currentlyEnabled = this.state.enabled;
|
|
|
|
|
|
|
|
// write metadata into the draft to indicate tracked state
|
2016-02-24 09:51:10 +08:00
|
|
|
DraftStore.sessionForClientId(this.props.draftClientId).then((session) => {
|
|
|
|
const draft = session.draft();
|
|
|
|
|
|
|
|
NylasAPI.authPlugin(PLUGIN_ID, plugin.title, draft.accountId)
|
|
|
|
.then(() => {
|
|
|
|
Actions.setMetadata(draft, PLUGIN_ID, currentlyEnabled ? null : {tracked: true});
|
|
|
|
})
|
|
|
|
.catch((error)=> {
|
|
|
|
NylasEnv.reportError(error);
|
|
|
|
NylasEnv.showErrorDialog(`Sorry, we were unable to save your link tracking settings. ${error.message}`);
|
2016-02-20 04:30:24 +08:00
|
|
|
});
|
2016-02-24 09:51:10 +08:00
|
|
|
});
|
2016-02-20 04:30:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
2016-02-24 05:42:10 +08:00
|
|
|
const title = this.state.enabled ? "Disable" : "Enable"
|
2016-02-20 04:30:24 +08:00
|
|
|
return (
|
|
|
|
<button
|
2016-02-24 05:42:10 +08:00
|
|
|
title={`${title} link tracking`}
|
|
|
|
className={`btn btn-toolbar ${this.state.enabled ? "btn-enabled" : ""}`}
|
2016-02-20 04:30:24 +08:00
|
|
|
onClick={this._onClick}>
|
|
|
|
<RetinaImg
|
2016-02-24 09:51:10 +08:00
|
|
|
name="icon-composer-linktracking.png"
|
2016-02-20 04:30:24 +08:00
|
|
|
mode={RetinaImg.Mode.ContentIsMask} />
|
|
|
|
</button>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|