2017-09-27 02:42:18 +08:00
|
|
|
import { React, ReactDOM, PropTypes, Actions } from 'mailspring-exports';
|
2017-09-27 02:33:08 +08:00
|
|
|
import { RetinaImg } from 'nylas-component-kit';
|
2017-09-07 07:19:48 +08:00
|
|
|
import OpenTrackingMessagePopover from './open-tracking-message-popover';
|
2017-09-27 02:33:08 +08:00
|
|
|
import { PLUGIN_ID } from './open-tracking-constants';
|
2017-09-07 07:19:48 +08:00
|
|
|
|
|
|
|
export default class OpenTrackingIcon extends React.Component {
|
|
|
|
static displayName = 'OpenTrackingIcon';
|
|
|
|
|
|
|
|
static propTypes = {
|
2017-09-27 02:33:08 +08:00
|
|
|
thread: PropTypes.object.isRequired,
|
2017-09-07 07:19:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2017-09-27 02:33:08 +08:00
|
|
|
this.state = this._getStateFromThread(props.thread);
|
2017-09-07 07:19:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(newProps) {
|
|
|
|
this.setState(this._getStateFromThread(newProps.thread));
|
|
|
|
}
|
|
|
|
|
|
|
|
onMouseDown = () => {
|
|
|
|
const rect = ReactDOM.findDOMNode(this).getBoundingClientRect();
|
|
|
|
Actions.openPopover(
|
|
|
|
<OpenTrackingMessagePopover
|
|
|
|
message={this.state.message}
|
|
|
|
openMetadata={this.state.message.metadataForPluginId(PLUGIN_ID)}
|
|
|
|
/>,
|
2017-09-27 02:33:08 +08:00
|
|
|
{ originRect: rect, direction: 'down' }
|
|
|
|
);
|
|
|
|
};
|
2017-09-07 07:19:48 +08:00
|
|
|
|
|
|
|
_getStateFromThread(thread) {
|
2017-09-27 02:33:08 +08:00
|
|
|
const messages = thread.__messages || [];
|
2017-09-07 07:19:48 +08:00
|
|
|
|
|
|
|
let lastMessage = null;
|
|
|
|
for (let i = messages.length - 1; i >= 0; i--) {
|
|
|
|
if (!messages[i].draft) {
|
|
|
|
lastMessage = messages[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!lastMessage) {
|
|
|
|
return {
|
|
|
|
message: null,
|
|
|
|
opened: false,
|
|
|
|
openCount: null,
|
|
|
|
hasMetadata: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const lastMessageMeta = lastMessage.metadataForPluginId(PLUGIN_ID);
|
|
|
|
const hasMetadata = lastMessageMeta != null && lastMessageMeta.open_count != null;
|
|
|
|
|
|
|
|
return {
|
|
|
|
message: lastMessage,
|
|
|
|
opened: hasMetadata && lastMessageMeta.open_count > 0,
|
|
|
|
openCount: hasMetadata ? lastMessageMeta.open_count : null,
|
|
|
|
hasMetadata: hasMetadata,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
_renderImage() {
|
|
|
|
return (
|
|
|
|
<RetinaImg
|
2017-09-27 02:33:08 +08:00
|
|
|
className={this.state.opened ? 'opened' : 'unopened'}
|
2017-09-07 07:19:48 +08:00
|
|
|
url="mailspring://open-tracking/assets/icon-tracking-opened@2x.png"
|
|
|
|
mode={RetinaImg.Mode.ContentIsMask}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2017-09-27 02:33:08 +08:00
|
|
|
if (!this.state.hasMetadata) return <span style={{ width: '19px' }} />;
|
|
|
|
const openedTitle = `${this.state.openCount} open${this.state.openCount === 1 ? '' : 's'}`;
|
|
|
|
const title = this.state.opened ? openedTitle : 'This message has not been opened';
|
2017-09-07 07:19:48 +08:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
title={title}
|
|
|
|
className="open-tracking-icon"
|
|
|
|
onMouseDown={this.state.opened ? this.onMouseDown : null}
|
|
|
|
>
|
|
|
|
{this._renderImage()}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|