2016-03-26 03:37:42 +08:00
|
|
|
import {React, FocusedPerspectiveStore} from 'nylas-exports'
|
2016-02-20 04:30:24 +08:00
|
|
|
import {RetinaImg} from 'nylas-component-kit'
|
2016-02-24 14:55:47 +08:00
|
|
|
import {PLUGIN_ID} from './open-tracking-constants'
|
|
|
|
|
2016-02-20 04:30:24 +08:00
|
|
|
|
|
|
|
export default class OpenTrackingIcon extends React.Component {
|
|
|
|
static displayName = 'OpenTrackingIcon';
|
|
|
|
|
|
|
|
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;
|
2016-02-24 10:20:26 +08:00
|
|
|
if ((messages || []).length === 0) { return {opened: false, hasMetadata: false} }
|
2016-03-08 12:54:43 +08:00
|
|
|
const metadataObjs = messages
|
|
|
|
.map(msg => msg.metadataForPluginId(PLUGIN_ID))
|
|
|
|
.filter(meta => meta && meta.open_count != null);
|
2016-02-24 10:20:26 +08:00
|
|
|
return {
|
|
|
|
hasMetadata: metadataObjs.length > 0,
|
2016-02-24 10:51:05 +08:00
|
|
|
opened: metadataObjs.length > 0 && metadataObjs.every(m => m.open_count > 0),
|
2016-02-24 10:20:26 +08:00
|
|
|
};
|
2016-02-20 04:30:24 +08:00
|
|
|
}
|
|
|
|
|
2016-02-24 10:51:05 +08:00
|
|
|
_renderImage() {
|
2016-02-20 04:30:24 +08:00
|
|
|
return (
|
|
|
|
<RetinaImg
|
2016-02-24 05:42:10 +08:00
|
|
|
className={this.state.opened ? "opened" : "unopened"}
|
2016-02-24 10:51:05 +08:00
|
|
|
url="nylas://open-tracking/assets/icon-tracking-read@2x.png"
|
2016-02-20 04:30:24 +08:00
|
|
|
mode={RetinaImg.Mode.ContentIsMask} />
|
|
|
|
);
|
2016-02-24 07:22:55 +08:00
|
|
|
}
|
2016-02-20 04:30:24 +08:00
|
|
|
|
|
|
|
render() {
|
2016-03-26 03:37:42 +08:00
|
|
|
const perspective = FocusedPerspectiveStore.current()
|
|
|
|
if (perspective.categoriesSharedName() !== 'sent') {
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-24 10:51:05 +08:00
|
|
|
const title = this.state.opened ? "This message has been read at least once" : "This message has not been read";
|
2016-02-20 04:30:24 +08:00
|
|
|
return (
|
2016-02-24 10:51:05 +08:00
|
|
|
<div title={title} className="open-tracking-icon">
|
2016-02-25 05:02:18 +08:00
|
|
|
{this.state.hasMetadata ? this._renderImage() : ""}
|
2016-02-20 04:30:24 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|