2018-10-07 14:27:27 +08:00
|
|
|
import { localized, React, ReactDOM, Actions, PropTypes } from 'mailspring-exports';
|
2017-09-27 02:46:00 +08:00
|
|
|
import { RetinaImg } from 'mailspring-component-kit';
|
2017-09-27 02:33:08 +08:00
|
|
|
import OpenTrackingMessagePopover from './open-tracking-message-popover';
|
|
|
|
import { PLUGIN_ID } from './open-tracking-constants';
|
2017-09-07 07:19:48 +08:00
|
|
|
|
|
|
|
export default class OpenTrackingMessageStatus extends React.Component {
|
2017-09-27 02:33:08 +08:00
|
|
|
static displayName = 'OpenTrackingMessageStatus';
|
2017-09-07 07:19:48 +08:00
|
|
|
|
|
|
|
static propTypes = {
|
2017-09-27 02:33:08 +08:00
|
|
|
message: PropTypes.object.isRequired,
|
2017-09-07 07:19:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static containerStyles = {
|
|
|
|
paddingTop: 4,
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2017-09-27 02:33:08 +08:00
|
|
|
this.state = this._getStateFromMessage(props.message);
|
2017-09-07 07:19:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
2017-09-27 02:33:08 +08:00
|
|
|
this.setState(this._getStateFromMessage(nextProps.message));
|
2017-09-07 07:19:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
onMouseDown = () => {
|
|
|
|
const rect = ReactDOM.findDOMNode(this).getBoundingClientRect();
|
|
|
|
Actions.openPopover(
|
|
|
|
<OpenTrackingMessagePopover
|
|
|
|
message={this.props.message}
|
|
|
|
openMetadata={this.props.message.metadataForPluginId(PLUGIN_ID)}
|
|
|
|
/>,
|
2017-09-27 02:33:08 +08:00
|
|
|
{ originRect: rect, direction: 'down' }
|
|
|
|
);
|
|
|
|
};
|
2017-09-07 07:19:48 +08:00
|
|
|
|
|
|
|
_getStateFromMessage(message) {
|
|
|
|
const metadata = message.metadataForPluginId(PLUGIN_ID);
|
|
|
|
if (!metadata || metadata.open_count == null) {
|
|
|
|
return {
|
|
|
|
hasMetadata: false,
|
|
|
|
openCount: null,
|
|
|
|
opened: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
hasMetadata: true,
|
|
|
|
openCount: metadata.open_count,
|
|
|
|
opened: metadata.open_count > 0,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
renderImage() {
|
|
|
|
return (
|
|
|
|
<RetinaImg
|
2017-09-27 02:33:08 +08:00
|
|
|
className={this.state.opened ? 'opened' : 'unopened'}
|
|
|
|
style={{ position: 'relative', top: -1 }}
|
2017-09-07 07:19:48 +08:00
|
|
|
url="mailspring://open-tracking/assets/InMessage-opened@2x.png"
|
|
|
|
mode={RetinaImg.Mode.ContentIsMask}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
if (!this.state.hasMetadata) return false;
|
2018-10-07 14:27:27 +08:00
|
|
|
const noun = this.state.openCount === 1 ? localized('Open') : localized('Opens');
|
|
|
|
let count = this.state.openCount;
|
|
|
|
if (this.state.openCount > 999) {
|
|
|
|
count = '999+';
|
|
|
|
}
|
|
|
|
|
|
|
|
const text = this.state.opened ? `${count} ${noun.toLocaleLowerCase()}` : localized('No opens');
|
2017-09-07 07:19:48 +08:00
|
|
|
return (
|
|
|
|
<span
|
2017-09-27 02:33:08 +08:00
|
|
|
className={`open-tracking-message-status ${this.state.opened ? 'opened' : 'unopened'}`}
|
2017-09-07 07:19:48 +08:00
|
|
|
onMouseDown={this.state.opened ? this.onMouseDown : null}
|
|
|
|
>
|
|
|
|
{this.renderImage()} {text}
|
|
|
|
</span>
|
2017-09-27 02:33:08 +08:00
|
|
|
);
|
2017-09-07 07:19:48 +08:00
|
|
|
}
|
|
|
|
}
|