You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
992 B

  1. import moment from 'moment';
  2. import PureRenderMixin from 'react-addons-pure-render-mixin';
  3. moment.updateLocale('en', {
  4. relativeTime : {
  5. future: "in %s",
  6. past: "%s",
  7. s: "%ds",
  8. m: "1m",
  9. mm: "%dm",
  10. h: "1h",
  11. hh: "%dh",
  12. d: "1d",
  13. dd: "%dd",
  14. M: "1mo",
  15. MM: "%dmo",
  16. y: "1y",
  17. yy: "%dy"
  18. }
  19. });
  20. const RelativeTimestamp = React.createClass({
  21. getInitialState () {
  22. return {
  23. text: ''
  24. };
  25. },
  26. propTypes: {
  27. timestamp: React.PropTypes.string.isRequired
  28. },
  29. mixins: [PureRenderMixin],
  30. componentWillMount () {
  31. this._updateMomentText();
  32. this.interval = setInterval(this._updateMomentText, 6000);
  33. },
  34. componentWillUnmount () {
  35. clearInterval(this.interval);
  36. },
  37. _updateMomentText () {
  38. this.setState({ text: moment(this.props.timestamp).fromNow() });
  39. },
  40. render () {
  41. return (
  42. <span>
  43. {this.state.text}
  44. </span>
  45. );
  46. }
  47. });
  48. export default RelativeTimestamp;