Mailspring/app/internal_packages/open-tracking/lib/open-tracking-message-status.jsx

72 lines
2.1 KiB
React
Raw Normal View History

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';
export default class OpenTrackingMessageStatus extends React.Component {
2017-09-27 02:33:08 +08:00
static displayName = 'OpenTrackingMessageStatus';
static propTypes = {
2017-09-27 02:33:08 +08:00
message: PropTypes.object.isRequired,
};
static containerStyles = {
paddingTop: 4,
};
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' }
);
};
_getDataFromMessage() {
const metadata = this.props.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,
};
}
render() {
const { hasMetadata, openCount, opened } = this._getDataFromMessage();
if (!hasMetadata) return false;
const noun = openCount === 1 ? localized('Open') : localized('Opens');
let count = openCount;
if (openCount > 999) {
count = '999+';
}
const text = opened ? `${count} ${noun.toLocaleLowerCase()}` : localized('No opens');
return (
<span
className={`open-tracking-message-status ${opened ? 'opened' : 'unopened'}`}
onMouseDown={opened ? this.onMouseDown : null}
>
{
<RetinaImg
className={opened ? 'opened' : 'unopened'}
style={{ position: 'relative', top: -1 }}
url="mailspring://open-tracking/assets/InMessage-opened@2x.png"
mode={RetinaImg.Mode.ContentIsMask}
/>
}&nbsp;&nbsp;{text}
</span>
2017-09-27 02:33:08 +08:00
);
}
}