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.

TimeElapsed.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import React, { Component } from 'react';
  2. import { translate } from '../../base/i18n';
  3. /**
  4. * React component for displaying total time elapsed. Converts a total count of
  5. * milliseconds into a more humanized form: "# hours, # minutes, # seconds".
  6. * With a time of 0, "0s" will be displayed.
  7. *
  8. * @extends Component
  9. */
  10. class TimeElapsed extends Component {
  11. /**
  12. * TimeElapsed component's property types.
  13. *
  14. * @static
  15. */
  16. static propTypes = {
  17. /**
  18. * The function to translate human-readable text.
  19. */
  20. t: React.PropTypes.func,
  21. /**
  22. * The milliseconds to be converted into a human-readable format.
  23. */
  24. time: React.PropTypes.number
  25. };
  26. /**
  27. * Implements React's {@link Component#render()}.
  28. *
  29. * @inheritdoc
  30. * @returns {ReactElement}
  31. */
  32. render() {
  33. const { time } = this.props;
  34. const hours = _getHoursCount(time);
  35. const minutes = _getMinutesCount(time);
  36. const seconds = _getSecondsCount(time);
  37. const timeElapsed = [];
  38. if (hours) {
  39. const hourPassed
  40. = this._createTimeDisplay(hours, 'speakerStats.hours', 'hours');
  41. timeElapsed.push(hourPassed);
  42. }
  43. if (hours || minutes) {
  44. const minutesPassed
  45. = this._createTimeDisplay(
  46. minutes,
  47. 'speakerStats.minutes',
  48. 'minutes');
  49. timeElapsed.push(minutesPassed);
  50. }
  51. const secondsPassed
  52. = this._createTimeDisplay(
  53. seconds,
  54. 'speakerStats.seconds',
  55. 'seconds');
  56. timeElapsed.push(secondsPassed);
  57. return (
  58. <div>
  59. { timeElapsed }
  60. </div>
  61. );
  62. }
  63. /**
  64. * Returns a ReactElement to display the passed in count and a count noun.
  65. *
  66. * @private
  67. * @param {number} count - The number used for display and to check for
  68. * count noun plurality.
  69. * @param {string} countNounKey - Translation key for the time's count noun.
  70. * @param {string} countType - What is being counted. Used as the element's
  71. * key for react to iterate upon.
  72. * @returns {ReactElement}
  73. */
  74. _createTimeDisplay(count, countNounKey, countType) {
  75. const { t } = this.props;
  76. return (
  77. <span key = { countType } >
  78. { t(countNounKey, { count }) }
  79. </span>
  80. );
  81. }
  82. }
  83. export default translate(TimeElapsed);
  84. /**
  85. * Counts how many whole hours are included in the given time total.
  86. *
  87. * @param {number} milliseconds - The millisecond total to get hours from.
  88. * @private
  89. * @returns {number}
  90. */
  91. function _getHoursCount(milliseconds) {
  92. return Math.floor(milliseconds / (60 * 60 * 1000));
  93. }
  94. /**
  95. * Counts how many whole minutes are included in the given time total.
  96. *
  97. * @param {number} milliseconds - The millisecond total to get minutes from.
  98. * @private
  99. * @returns {number}
  100. */
  101. function _getMinutesCount(milliseconds) {
  102. return Math.floor(milliseconds / (60 * 1000) % 60);
  103. }
  104. /**
  105. * Counts how many whole seconds are included in the given time total.
  106. *
  107. * @param {number} milliseconds - The millisecond total to get seconds from.
  108. * @private
  109. * @returns {number}
  110. */
  111. function _getSecondsCount(milliseconds) {
  112. return Math.floor(milliseconds / 1000 % 60);
  113. }