mirror of
https://github.com/Foundry376/Mailspring.git
synced 2024-09-22 16:26:08 +08:00
24b94a25df
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
48 lines
1.5 KiB
CoffeeScript
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
|