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.

ConferenceTimer.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // @flow
  2. import { Component } from 'react';
  3. import { connect } from '../../base/redux';
  4. import { getLocalizedDurationFormatter } from '../../base/i18n';
  5. import { getConferenceTimestamp } from '../../base/conference/functions';
  6. import { renderConferenceTimer } from '../';
  7. /**
  8. * The type of the React {@code Component} props of {@link ConferenceTimer}.
  9. */
  10. type Props = {
  11. /**
  12. * The UTC timestamp representing the time when first participant joined.
  13. */
  14. _startTimestamp: ?number,
  15. /**
  16. * The redux {@code dispatch} function.
  17. */
  18. dispatch: Function
  19. };
  20. /**
  21. * The type of the React {@code Component} state of {@link ConferenceTimer}.
  22. */
  23. type State = {
  24. /**
  25. * Value of current conference time.
  26. */
  27. timerValue: string
  28. };
  29. /**
  30. * ConferenceTimer react component.
  31. *
  32. * @class ConferenceTimer
  33. * @extends Component
  34. */
  35. class ConferenceTimer extends Component<Props, State> {
  36. /**
  37. * Handle for setInterval timer.
  38. */
  39. _interval;
  40. /**
  41. * Initializes a new {@code ConferenceTimer} instance.
  42. *
  43. * @param {Props} props - The read-only properties with which the new
  44. * instance is to be initialized.
  45. */
  46. constructor(props: Props) {
  47. super(props);
  48. this.state = {
  49. timerValue: getLocalizedDurationFormatter(0)
  50. };
  51. }
  52. /**
  53. * Starts the conference timer when component will be
  54. * mounted.
  55. *
  56. * @inheritdoc
  57. */
  58. componentDidMount() {
  59. this._startTimer();
  60. }
  61. /**
  62. * Stops the conference timer when component will be
  63. * unmounted.
  64. *
  65. * @inheritdoc
  66. */
  67. componentWillUnmount() {
  68. this._stopTimer();
  69. }
  70. /**
  71. * Implements React's {@link Component#render()}.
  72. *
  73. * @inheritdoc
  74. * @returns {ReactElement}
  75. */
  76. render() {
  77. const { timerValue } = this.state;
  78. const { _startTimestamp } = this.props;
  79. if (!_startTimestamp) {
  80. return null;
  81. }
  82. return renderConferenceTimer(timerValue);
  83. }
  84. /**
  85. * Sets the current state values that will be used to render the timer.
  86. *
  87. * @param {number} refValueUTC - The initial UTC timestamp value.
  88. * @param {number} currentValueUTC - The current UTC timestamp value.
  89. *
  90. * @returns {void}
  91. */
  92. _setStateFromUTC(refValueUTC, currentValueUTC) {
  93. if (!refValueUTC || !currentValueUTC) {
  94. return;
  95. }
  96. if (currentValueUTC < refValueUTC) {
  97. return;
  98. }
  99. const timerMsValue = currentValueUTC - refValueUTC;
  100. const localizedTime = getLocalizedDurationFormatter(timerMsValue);
  101. this.setState({
  102. timerValue: localizedTime
  103. });
  104. }
  105. /**
  106. * Start conference timer.
  107. *
  108. * @returns {void}
  109. */
  110. _startTimer() {
  111. if (!this._interval) {
  112. this._setStateFromUTC(this.props._startTimestamp, (new Date()).getTime());
  113. this._interval = setInterval(() => {
  114. this._setStateFromUTC(this.props._startTimestamp, (new Date()).getTime());
  115. }, 1000);
  116. }
  117. }
  118. /**
  119. * Stop conference timer.
  120. *
  121. * @returns {void}
  122. */
  123. _stopTimer() {
  124. if (this._interval) {
  125. clearInterval(this._interval);
  126. }
  127. this.setState({
  128. timerValue: getLocalizedDurationFormatter(0)
  129. });
  130. }
  131. }
  132. /**
  133. * Maps (parts of) the Redux state to the associated
  134. * {@code ConferenceTimer}'s props.
  135. *
  136. * @param {Object} state - The Redux state.
  137. * @private
  138. * @returns {{
  139. * _startTimestamp: number
  140. * }}
  141. */
  142. export function _mapStateToProps(state: Object) {
  143. return {
  144. _startTimestamp: getConferenceTimestamp(state)
  145. };
  146. }
  147. export default connect(_mapStateToProps)(ConferenceTimer);