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

SpeakerStats.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // @flow
  2. import React, { Component } from 'react';
  3. import type { Dispatch } from 'redux';
  4. import { Dialog } from '../../base/dialog';
  5. import { translate } from '../../base/i18n';
  6. import { getLocalParticipant } from '../../base/participants';
  7. import { connect } from '../../base/redux';
  8. import { escapeRegexp } from '../../base/util';
  9. import { initUpdateStats, initSearch } from '../actions';
  10. import { SPEAKER_STATS_RELOAD_INTERVAL } from '../constants';
  11. import { getSpeakerStats, getSearchCriteria } from '../functions';
  12. import SpeakerStatsItem from './SpeakerStatsItem';
  13. import SpeakerStatsLabels from './SpeakerStatsLabels';
  14. import SpeakerStatsSearch from './SpeakerStatsSearch';
  15. declare var interfaceConfig: Object;
  16. /**
  17. * The type of the React {@code Component} props of {@link SpeakerStats}.
  18. */
  19. type Props = {
  20. /**
  21. * The display name for the local participant obtained from the redux store.
  22. */
  23. _localDisplayName: string,
  24. /**
  25. * The speaker paricipant stats.
  26. */
  27. _stats: Object,
  28. /**
  29. * The search criteria.
  30. */
  31. _criteria: string,
  32. /**
  33. * The JitsiConference from which stats will be pulled.
  34. */
  35. conference: Object,
  36. /**
  37. * Redux store dispatch method.
  38. */
  39. dispatch: Dispatch<any>,
  40. /**
  41. * The function to translate human-readable text.
  42. */
  43. t: Function
  44. };
  45. /**
  46. * React component for displaying a list of speaker stats.
  47. *
  48. * @extends Component
  49. */
  50. class SpeakerStats extends Component<Props> {
  51. _updateInterval: IntervalID;
  52. /**
  53. * Initializes a new SpeakerStats instance.
  54. *
  55. * @param {Object} props - The read-only React Component props with which
  56. * the new instance is to be initialized.
  57. */
  58. constructor(props) {
  59. super(props);
  60. // Bind event handlers so they are only bound once per instance.
  61. this._updateStats = this._updateStats.bind(this);
  62. this._onSearch = this._onSearch.bind(this);
  63. this._updateStats();
  64. }
  65. /**
  66. * Begin polling for speaker stats updates.
  67. *
  68. * @inheritdoc
  69. */
  70. componentDidMount() {
  71. this._updateInterval = setInterval(() => this._updateStats(), SPEAKER_STATS_RELOAD_INTERVAL);
  72. }
  73. /**
  74. * Stop polling for speaker stats updates.
  75. *
  76. * @inheritdoc
  77. * @returns {void}
  78. */
  79. componentWillUnmount() {
  80. clearInterval(this._updateInterval);
  81. }
  82. /**
  83. * Implements React's {@link Component#render()}.
  84. *
  85. * @inheritdoc
  86. * @returns {ReactElement}
  87. */
  88. render() {
  89. const userIds = Object.keys(this.props._stats);
  90. const items = userIds.map(userId => this._createStatsItem(userId));
  91. return (
  92. <Dialog
  93. cancelKey = 'dialog.close'
  94. submitDisabled = { true }
  95. titleKey = 'speakerStats.speakerStats'>
  96. <div className = 'speaker-stats'>
  97. <SpeakerStatsSearch onSearch = { this._onSearch } />
  98. <SpeakerStatsLabels />
  99. { items }
  100. </div>
  101. </Dialog>
  102. );
  103. }
  104. /**
  105. * Create a SpeakerStatsItem instance for the passed in user id.
  106. *
  107. * @param {string} userId - User id used to look up the associated
  108. * speaker stats from the jitsi library.
  109. * @returns {SpeakerStatsItem|null}
  110. * @private
  111. */
  112. _createStatsItem(userId) {
  113. const statsModel = this.props._stats[userId];
  114. if (!statsModel || statsModel.hidden) {
  115. return null;
  116. }
  117. const isDominantSpeaker = statsModel.isDominantSpeaker();
  118. const dominantSpeakerTime = statsModel.getTotalDominantSpeakerTime();
  119. const hasLeft = statsModel.hasLeft();
  120. let displayName;
  121. if (statsModel.isLocalStats()) {
  122. const { t } = this.props;
  123. const meString = t('me');
  124. displayName = this.props._localDisplayName;
  125. displayName
  126. = displayName ? `${displayName} (${meString})` : meString;
  127. } else {
  128. displayName
  129. = this.props._stats[userId].getDisplayName()
  130. || interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME;
  131. }
  132. return (
  133. <SpeakerStatsItem
  134. displayName = { displayName }
  135. dominantSpeakerTime = { dominantSpeakerTime }
  136. hasLeft = { hasLeft }
  137. isDominantSpeaker = { isDominantSpeaker }
  138. key = { userId } />
  139. );
  140. }
  141. _onSearch: () => void;
  142. /**
  143. * Search the existing participants by name.
  144. *
  145. * @returns {void}
  146. * @param {string} criteria - The search parameter.
  147. * @protected
  148. */
  149. _onSearch(criteria = '') {
  150. this.props.dispatch(initSearch(escapeRegexp(criteria)));
  151. }
  152. _updateStats: () => void;
  153. /**
  154. * Update the internal state with the latest speaker stats.
  155. *
  156. * @returns {void}
  157. * @private
  158. */
  159. _updateStats() {
  160. this.props.dispatch(initUpdateStats(() => this.props.conference.getSpeakerStats()));
  161. }
  162. }
  163. /**
  164. * Maps (parts of) the redux state to the associated SpeakerStats's props.
  165. *
  166. * @param {Object} state - The redux state.
  167. * @private
  168. * @returns {{
  169. * _localDisplayName: ?string,
  170. * _stats: Object,
  171. * _criteria: string,
  172. * }}
  173. */
  174. function _mapStateToProps(state) {
  175. const localParticipant = getLocalParticipant(state);
  176. return {
  177. /**
  178. * The local display name.
  179. *
  180. * @private
  181. * @type {string|undefined}
  182. */
  183. _localDisplayName: localParticipant && localParticipant.name,
  184. _stats: getSpeakerStats(state),
  185. _criteria: getSearchCriteria(state)
  186. };
  187. }
  188. export default translate(connect(_mapStateToProps)(SpeakerStats));