2019-03-05 03:03:12 +08:00
|
|
|
import React from 'react';
|
|
|
|
import ReactDOM from 'react-dom';
|
|
|
|
import { localized, Actions, PropTypes, Message } 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
|
|
|
|
2019-03-05 03:03:12 +08:00
|
|
|
export default class OpenTrackingMessageStatus extends React.Component<{ message: Message }> {
|
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,
|
|
|
|
};
|
|
|
|
|
|
|
|
onMouseDown = () => {
|
2019-03-05 03:03:12 +08:00
|
|
|
const rect = (ReactDOM.findDOMNode(this) as HTMLElement).getBoundingClientRect();
|
2017-09-07 07:19:48 +08:00
|
|
|
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
|
|
|
|
2019-02-25 02:17:15 +08:00
|
|
|
_getDataFromMessage() {
|
|
|
|
const metadata = this.props.message.metadataForPluginId(PLUGIN_ID);
|
2017-09-07 07:19:48 +08:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-02-25 02:17:15 +08:00
|
|
|
const { hasMetadata, openCount, opened } = this._getDataFromMessage();
|
|
|
|
|
|
|
|
if (!hasMetadata) return false;
|
|
|
|
const noun = openCount === 1 ? localized('Open') : localized('Opens');
|
|
|
|
let count = openCount;
|
|
|
|
if (openCount > 999) {
|
2018-10-07 14:27:27 +08:00
|
|
|
count = '999+';
|
|
|
|
}
|
|
|
|
|
2019-02-25 02:17:15 +08:00
|
|
|
const text = opened ? `${count} ${noun.toLocaleLowerCase()}` : localized('No opens');
|
2017-09-07 07:19:48 +08:00
|
|
|
return (
|
|
|
|
<span
|
2019-02-25 02:17:15 +08:00
|
|
|
className={`open-tracking-message-status ${opened ? 'opened' : 'unopened'}`}
|
|
|
|
onMouseDown={opened ? this.onMouseDown : null}
|
2017-09-07 07:19:48 +08:00
|
|
|
>
|
2019-02-25 02:17:15 +08:00
|
|
|
{
|
|
|
|
<RetinaImg
|
|
|
|
className={opened ? 'opened' : 'unopened'}
|
|
|
|
style={{ position: 'relative', top: -1 }}
|
|
|
|
url="mailspring://open-tracking/assets/InMessage-opened@2x.png"
|
|
|
|
mode={RetinaImg.Mode.ContentIsMask}
|
|
|
|
/>
|
|
|
|
} {text}
|
2017-09-07 07:19:48 +08:00
|
|
|
</span>
|
2017-09-27 02:33:08 +08:00
|
|
|
);
|
2017-09-07 07:19:48 +08:00
|
|
|
}
|
|
|
|
}
|