2015-05-20 07:06:59 +08:00
|
|
|
_ = require 'underscore'
|
2015-03-10 02:17:22 +08:00
|
|
|
moment = require 'moment-timezone'
|
2015-02-25 08:20:57 +08:00
|
|
|
React = require 'react'
|
2015-05-15 08:08:30 +08:00
|
|
|
{Utils} = require 'nylas-exports'
|
2015-02-25 08:20:57 +08:00
|
|
|
|
2015-05-01 04:08:29 +08:00
|
|
|
class MessageTimestamp extends React.Component
|
|
|
|
@displayName: 'MessageTimestamp'
|
|
|
|
@propTypes:
|
2015-02-25 08:20:57 +08:00
|
|
|
date: React.PropTypes.object.isRequired,
|
2015-02-25 08:24:01 +08:00
|
|
|
className: React.PropTypes.string,
|
2015-03-10 02:17:22 +08:00
|
|
|
isDetailed: React.PropTypes.bool
|
|
|
|
onClick: React.PropTypes.func
|
2015-02-25 08:20:57 +08:00
|
|
|
|
2015-05-01 04:08:29 +08:00
|
|
|
shouldComponentUpdate: (nextProps, nextState) =>
|
2015-04-01 07:32:14 +08:00
|
|
|
+nextProps.date isnt +@props.date or nextProps.isDetailed isnt @props.isDetailed
|
|
|
|
|
2015-05-01 04:08:29 +08:00
|
|
|
render: =>
|
2015-08-13 05:52:03 +08:00
|
|
|
msgDate = moment.tz(@props.date, Utils.timeZone)
|
|
|
|
nowDate = @_today()
|
2015-08-20 06:50:10 +08:00
|
|
|
formattedDate = @_formattedDate(msgDate, nowDate, @props.isDetailed)
|
2015-03-10 02:17:22 +08:00
|
|
|
<div className={@props.className}
|
2015-08-13 05:52:03 +08:00
|
|
|
onClick={@props.onClick}>{formattedDate}</div>
|
2015-03-10 02:17:22 +08:00
|
|
|
|
2015-08-13 05:52:03 +08:00
|
|
|
_formattedDate: (msgDate, now, isDetailed) =>
|
|
|
|
if isDetailed
|
|
|
|
return msgDate.format "MMMM D, YYYY [at] h:mm A"
|
2015-02-25 08:20:57 +08:00
|
|
|
else
|
2015-08-13 05:52:03 +08:00
|
|
|
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"
|
2015-02-25 08:20:57 +08:00
|
|
|
|
|
|
|
# Stubbable for testing. Returns a `moment`
|
2015-04-01 07:32:14 +08:00
|
|
|
_today: -> moment.tz(Utils.timeZone)
|
2015-02-25 08:20:57 +08:00
|
|
|
|
|
|
|
|
2015-05-01 04:08:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
module.exports = MessageTimestamp
|