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

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