about summary refs log tree commit diff
path: root/app/javascript/mastodon/components/relative_timestamp.js
blob: 2717d2326d7ce294be2653190a003462795c2bfd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import React from 'react';
import { injectIntl, FormattedRelative } from 'react-intl';
import PropTypes from 'prop-types';

const dateFormatOptions = {
  hour12: false,
  year: 'numeric',
  month: 'short',
  day: '2-digit',
  hour: '2-digit',
  minute: '2-digit',
};

@injectIntl
export default class RelativeTimestamp extends React.Component {

  static propTypes = {
    intl: PropTypes.object.isRequired,
    timestamp: PropTypes.string.isRequired,
  };

  shouldComponentUpdate (nextProps) {
    // As of right now the locale doesn't change without a new page load,
    // but we might as well check in case that ever changes.
    return this.props.timestamp !== nextProps.timestamp ||
      this.props.intl.locale !== nextProps.intl.locale;
  }

  render () {
    const { timestamp, intl } = this.props;
    const date = new Date(timestamp);

    return (
      <time dateTime={timestamp} title={intl.formatDate(date, dateFormatOptions)}>
        <FormattedRelative value={date} />
      </time>
    );
  }

}