mirror of
https://github.com/Foundry376/Mailspring.git
synced 2024-09-24 01:06:07 +08:00
fe7a894e51
Summary: Adds two (very similar) plugins - Open Tracking and Link Tracking. Both can be enabled via a button in the composer. Open tracking inserts a tracking pixel right before send, and link tracking replaces all links with tracked redirects. Both plugins use the new Metadata service to store their open/click counts, and have backend servers to respectively serve the pixel image or handle the redirects. Requests also trigger a metadata update to increment the open/click counters. Test Plan: Manual for now Reviewers: evan, bengotow, drew Reviewed By: bengotow Differential Revision: https://phab.nylas.com/D2583
60 lines
1.7 KiB
JavaScript
60 lines
1.7 KiB
JavaScript
import {DraftStore, React, Actions, NylasAPI, DatabaseStore, Message, Rx} from 'nylas-exports'
|
|
import {RetinaImg} from 'nylas-component-kit'
|
|
import plugin from '../package.json'
|
|
const PLUGIN_ID = plugin.appId;
|
|
|
|
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});
|
|
this._subscription = Rx.Observable.fromQuery(query).subscribe(this.setStateFromDraft)
|
|
}
|
|
|
|
componentWillUnmount() {
|
|
this._subscription.dispose();
|
|
}
|
|
|
|
setStateFromDraft =(draft)=> {
|
|
if (!draft) return;
|
|
const metadata = draft.metadataForPluginId(PLUGIN_ID);
|
|
this.setState({enabled: metadata ? metadata.tracked : false});
|
|
};
|
|
|
|
_onClick=()=> {
|
|
const currentlyEnabled = this.state.enabled;
|
|
|
|
// write metadata into the draft to indicate tracked state
|
|
DraftStore.sessionForClientId(this.props.draftClientId)
|
|
.then(session => session.draft())
|
|
.then(draft => {
|
|
return NylasAPI.authPlugin(PLUGIN_ID, plugin.title, draft.accountId).then(() => {
|
|
Actions.setMetadata(draft, PLUGIN_ID, currentlyEnabled ? null : {tracked: true});
|
|
});
|
|
});
|
|
};
|
|
|
|
render() {
|
|
return (
|
|
<button
|
|
title="Link Tracking"
|
|
className={`btn btn-toolbar ${this.state.enabled ? "btn-action" : ""}`}
|
|
onClick={this._onClick}>
|
|
<RetinaImg
|
|
url="nylas://link-tracking/assets/linktracking-icon@2x.png"
|
|
mode={RetinaImg.Mode.ContentIsMask} />
|
|
</button>
|
|
)
|
|
}
|
|
}
|
|
|