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

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