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.

SpeakerStats.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* global interfaceConfig */
  2. import PropTypes from 'prop-types';
  3. import React, { Component } from 'react';
  4. import { connect } from 'react-redux';
  5. import { Dialog } from '../../base/dialog';
  6. import { translate } from '../../base/i18n';
  7. import { getLocalParticipant } from '../../base/participants';
  8. import SpeakerStatsItem from './SpeakerStatsItem';
  9. import SpeakerStatsLabels from './SpeakerStatsLabels';
  10. /**
  11. * React component for displaying a list of speaker stats.
  12. *
  13. * @extends Component
  14. */
  15. class SpeakerStats extends Component {
  16. /**
  17. * SpeakerStats component's property types.
  18. *
  19. * @static
  20. */
  21. static propTypes = {
  22. /**
  23. * The display name for the local participant obtained from the Redux
  24. * store.
  25. */
  26. _localDisplayName: PropTypes.string,
  27. /**
  28. * The JitsiConference from which stats will be pulled.
  29. */
  30. conference: PropTypes.object,
  31. /**
  32. * The function to translate human-readable text.
  33. */
  34. t: PropTypes.func
  35. };
  36. /**
  37. * Initializes a new SpeakerStats instance.
  38. *
  39. * @param {Object} props - The read-only React Component props with which
  40. * the new instance is to be initialized.
  41. */
  42. constructor(props) {
  43. super(props);
  44. this.state = {
  45. stats: {}
  46. };
  47. this._updateInterval = null;
  48. this._updateStats = this._updateStats.bind(this);
  49. }
  50. /**
  51. * Immediately request for updated speaker stats and begin
  52. * polling for speaker stats updates.
  53. *
  54. * @inheritdoc
  55. * @returns {void}
  56. */
  57. componentWillMount() {
  58. this._updateStats();
  59. this._updateInterval = setInterval(this._updateStats, 1000);
  60. }
  61. /**
  62. * Stop polling for speaker stats updates.
  63. *
  64. * @inheritdoc
  65. * @returns {void}
  66. */
  67. componentWillUnmount() {
  68. clearInterval(this._updateInterval);
  69. }
  70. /**
  71. * Implements React's {@link Component#render()}.
  72. *
  73. * @inheritdoc
  74. * @returns {ReactElement}
  75. */
  76. render() {
  77. const userIds = Object.keys(this.state.stats);
  78. const items = userIds.map(userId => this._createStatsItem(userId));
  79. return (
  80. <Dialog
  81. cancelTitleKey = { 'dialog.close' }
  82. submitDisabled = { true }
  83. titleKey = 'speakerStats.speakerStats'>
  84. <div className = 'speaker-stats'>
  85. <SpeakerStatsLabels />
  86. { items }
  87. </div>
  88. </Dialog>
  89. );
  90. }
  91. /**
  92. * Update the internal state with the latest speaker stats.
  93. *
  94. * @returns {void}
  95. * @private
  96. */
  97. _updateStats() {
  98. const stats = this.props.conference.getSpeakerStats();
  99. this.setState({ stats });
  100. }
  101. /**
  102. * Create a SpeakerStatsItem instance for the passed in user id.
  103. *
  104. * @param {string} userId - User id used to look up the associated
  105. * speaker stats from the jitsi library.
  106. * @returns {SpeakerStatsItem|null}
  107. * @private
  108. */
  109. _createStatsItem(userId) {
  110. const statsModel = this.state.stats[userId];
  111. if (!statsModel) {
  112. return null;
  113. }
  114. const isDominantSpeaker = statsModel.isDominantSpeaker();
  115. const dominantSpeakerTime = statsModel.getTotalDominantSpeakerTime();
  116. const hasLeft = statsModel.hasLeft();
  117. let displayName = '';
  118. if (statsModel.isLocalStats()) {
  119. const { t } = this.props;
  120. const meString = t('me');
  121. displayName = this.props._localDisplayName;
  122. displayName = displayName ? `${displayName} (${meString})`
  123. : meString;
  124. } else {
  125. displayName = this.state.stats[userId].getDisplayName()
  126. || interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME;
  127. }
  128. return (
  129. <SpeakerStatsItem
  130. displayName = { displayName }
  131. dominantSpeakerTime = { dominantSpeakerTime }
  132. hasLeft = { hasLeft }
  133. isDominantSpeaker = { isDominantSpeaker }
  134. key = { userId } />
  135. );
  136. }
  137. }
  138. /**
  139. * Maps (parts of) the Redux state to the associated SpeakerStats's props.
  140. *
  141. * @param {Object} state - The Redux state.
  142. * @private
  143. * @returns {{
  144. * _localDisplayName: string?
  145. * }}
  146. */
  147. function _mapStateToProps(state) {
  148. const localParticipant = getLocalParticipant(state);
  149. return {
  150. /**
  151. * The local display name.
  152. * @private
  153. * @type {string|undefined}
  154. */
  155. _localDisplayName: localParticipant && localParticipant.name
  156. };
  157. }
  158. export default translate(connect(_mapStateToProps)(SpeakerStats));