2016-02-20 04:30:24 +08:00
|
|
|
import {React} from 'nylas-exports'
|
|
|
|
import {RetinaImg} from 'nylas-component-kit'
|
2016-02-24 14:55:47 +08:00
|
|
|
import {PLUGIN_ID} from './link-tracking-constants'
|
2016-02-24 11:19:43 +08:00
|
|
|
|
2016-02-20 04:30:24 +08:00
|
|
|
const sum = (array, extractFn) => array.reduce( (a, b) => a + extractFn(b), 0 );
|
|
|
|
|
2016-02-24 14:55:47 +08:00
|
|
|
|
2016-02-20 04:30:24 +08:00
|
|
|
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
|
2016-02-24 11:19:43 +08:00
|
|
|
const metadataObjs = messages.map(msg => msg.metadataForPluginId(PLUGIN_ID)).filter(meta => meta);
|
2016-02-20 04:30:24 +08:00
|
|
|
if (metadataObjs.length) {
|
|
|
|
// If there's metadata, return the total number of link clicks in the most recent metadata
|
|
|
|
const mostRecentMetadata = metadataObjs.pop();
|
|
|
|
return {
|
2016-02-24 10:51:05 +08:00
|
|
|
clicks: sum(mostRecentMetadata.links || [], link => link.click_count || 0),
|
2016-02-20 04:30:24 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
return {clicks: null};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_renderIcon = () => {
|
|
|
|
return this.state.clicks == null ? "" : this._getIcon(this.state.clicks);
|
|
|
|
};
|
|
|
|
|
|
|
|
_getIcon(clicks) {
|
|
|
|
return (<span>
|
|
|
|
<RetinaImg
|
|
|
|
className={clicks > 0 ? "clicked" : ""}
|
2016-02-24 10:51:05 +08:00
|
|
|
name="icon-composer-linktracking.png"
|
2016-02-20 04:30:24 +08:00
|
|
|
mode={RetinaImg.Mode.ContentIsMask} />
|
|
|
|
<span className="link-click-count">{clicks > 0 ? clicks : ""}</span>
|
|
|
|
</span>)
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (<div className="link-tracking-icon">
|
|
|
|
{this._renderIcon()}
|
|
|
|
</div>)
|
|
|
|
}
|
|
|
|
}
|