您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SpeakerStats.js 4.8KB

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