Mailspring/internal_packages/link-tracking/lib/link-tracking-icon.jsx
Drew Regitsky fe7a894e51 feat(new-plugins): add open tracking and link tracking plugins
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
2016-02-19 12:42:56 -08:00

58 lines
1.6 KiB
JavaScript

import {React} from 'nylas-exports'
import {RetinaImg} from 'nylas-component-kit'
import plugin from '../package.json'
const sum = (array, extractFn) => array.reduce( (a, b) => a + extractFn(b), 0 );
export default class LinkTrackingIcon extends React.Component {
static displayName = 'LinkTrackingIcon';
static propTypes = {
thread: React.PropTypes.object.isRequired,
};
constructor(props) {
super(props);
this.state = this._getStateFromThread(props.thread);
}
componentWillReceiveProps(newProps) {
this.setState(this._getStateFromThread(newProps.thread));
}
_getStateFromThread(thread) {
const messages = thread.metadata;
// Pull a list of metadata for all messages
const metadataObjs = messages.map(msg => msg.metadataForPluginId(plugin.appId)).filter(meta => meta);
if (metadataObjs.length) {
// If there's metadata, return the total number of link clicks in the most recent metadata
const mostRecentMetadata = metadataObjs.pop();
return {
clicks: sum(mostRecentMetadata.links || [], link => link.click_count || 0),
};
}
return {clicks: null};
}
_renderIcon = () => {
return this.state.clicks == null ? "" : this._getIcon(this.state.clicks);
};
_getIcon(clicks) {
return (<span>
<RetinaImg
className={clicks > 0 ? "clicked" : ""}
url="nylas://link-tracking/assets/linktracking-icon@2x.png"
mode={RetinaImg.Mode.ContentIsMask} />
<span className="link-click-count">{clicks > 0 ? clicks : ""}</span>
</span>)
}
render() {
return (<div className="link-tracking-icon">
{this._renderIcon()}
</div>)
}
}