2015-04-01 07:32:14 +08:00
|
|
|
_ = require 'underscore-plus'
|
2015-03-10 02:17:22 +08:00
|
|
|
moment = require 'moment-timezone'
|
2015-02-25 08:20:57 +08:00
|
|
|
React = require 'react'
|
2015-04-01 07:32:14 +08:00
|
|
|
{Utils} = require 'inbox-exports'
|
2015-02-25 08:20:57 +08:00
|
|
|
|
|
|
|
module.exports =
|
|
|
|
MessageTimestamp = React.createClass
|
|
|
|
displayName: 'MessageTimestamp'
|
|
|
|
propTypes:
|
|
|
|
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-04-01 07:32:14 +08:00
|
|
|
shouldComponentUpdate: (nextProps, nextState) ->
|
|
|
|
+nextProps.date isnt +@props.date or nextProps.isDetailed isnt @props.isDetailed
|
|
|
|
|
2015-02-25 08:20:57 +08:00
|
|
|
render: ->
|
2015-03-10 02:17:22 +08:00
|
|
|
<div className={@props.className}
|
|
|
|
onClick={@props.onClick}>{@_formattedDate()}</div>
|
|
|
|
|
|
|
|
_formattedDate: ->
|
2015-04-01 07:32:14 +08:00
|
|
|
moment.tz(@props.date, Utils.timeZone).format(@_timeFormat())
|
2015-02-25 08:20:57 +08:00
|
|
|
|
|
|
|
_timeFormat: ->
|
2015-03-10 02:17:22 +08:00
|
|
|
if @props.isDetailed
|
2015-03-11 01:08:04 +08:00
|
|
|
return "DD / MM / YYYY h:mm a z"
|
2015-02-25 08:20:57 +08:00
|
|
|
else
|
2015-03-10 02:17:22 +08:00
|
|
|
today = moment(@_today())
|
|
|
|
dayOfEra = today.dayOfYear() + today.year() * 365
|
|
|
|
msgDate = moment(@props.date)
|
|
|
|
msgDayOfEra = msgDate.dayOfYear() + msgDate.year() * 365
|
|
|
|
diff = dayOfEra - msgDayOfEra
|
|
|
|
if diff < 1
|
|
|
|
return "h:mm a"
|
|
|
|
if diff < 4
|
|
|
|
return "MMM D, h:mm a"
|
|
|
|
else if diff > 1 and diff <= 365
|
|
|
|
return "MMM D"
|
|
|
|
else
|
|
|
|
return "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
|
|
|
|
|
|
|
|