Mailspring/internal_packages/message-list/lib/message-timestamp.cjsx
dillon 24b94a25df fix(message-list): show relative time if email is less than 22 hours ago. fixes T2308.
Summary: refactored existing code and rewrite come tests to make the logic simpler

Test Plan: new tests to test final outcome rather than intermediate formats, which i figured was more useful

Reviewers: bengotow

Reviewed By: bengotow

Maniphest Tasks: T3219, T2308

Differential Revision: https://phab.nylas.com/D1876
2015-08-12 14:52:03 -07:00

48 lines
1.5 KiB
CoffeeScript

_ = require 'underscore'
moment = require 'moment-timezone'
React = require 'react'
{Utils} = require 'nylas-exports'
class MessageTimestamp extends React.Component
@displayName: 'MessageTimestamp'
@propTypes:
date: React.PropTypes.object.isRequired,
className: React.PropTypes.string,
isDetailed: React.PropTypes.bool
onClick: React.PropTypes.func
shouldComponentUpdate: (nextProps, nextState) =>
+nextProps.date isnt +@props.date or nextProps.isDetailed isnt @props.isDetailed
render: =>
msgDate = moment.tz(@props.date, Utils.timeZone)
nowDate = @_today()
formattedDate = @_formattedDate(nowDate, msgDate, @props.isDetailed)
<div className={@props.className}
onClick={@props.onClick}>{formattedDate}</div>
_formattedDate: (msgDate, now, isDetailed) =>
if isDetailed
return msgDate.format "MMMM D, YYYY [at] h:mm A"
else
diff = now.diff(msgDate, 'days', true)
isSameDay = now.isSame(msgDate, 'days')
if diff < 1 and isSameDay
return msgDate.format "h:mm a"
if diff < 1.5 and not isSameDay
timeAgo = msgDate.from now
monthAndDay = msgDate.format "h:mm a"
return monthAndDay + " (" + timeAgo + ")"
if diff >= 1.5 and diff < 365
return msgDate.format "MMM D"
if diff >= 365
return msgDate.format "MMM D, YYYY"
# Stubbable for testing. Returns a `moment`
_today: -> moment.tz(Utils.timeZone)
module.exports = MessageTimestamp