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.3KB

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