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