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

SpeakerStats.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { Dialog } from '../../base/dialog';
  4. import { translate } from '../../base/i18n';
  5. import { getLocalParticipant } from '../../base/participants';
  6. import { connect } from '../../base/redux';
  7. import { escapeRegexp } from '../../base/util';
  8. import SpeakerStatsItem from './SpeakerStatsItem';
  9. import SpeakerStatsLabels from './SpeakerStatsLabels';
  10. import SpeakerStatsSearch from './SpeakerStatsSearch';
  11. declare var interfaceConfig: Object;
  12. /**
  13. * The type of the React {@code Component} props of {@link SpeakerStats}.
  14. */
  15. type Props = {
  16. /**
  17. * The display name for the local participant obtained from the redux store.
  18. */
  19. _localDisplayName: string,
  20. /**
  21. * The JitsiConference from which stats will be pulled.
  22. */
  23. conference: Object,
  24. /**
  25. * The function to translate human-readable text.
  26. */
  27. t: Function
  28. };
  29. /**
  30. * The type of the React {@code Component} state of {@link SpeakerStats}.
  31. */
  32. type State = {
  33. /**
  34. * The stats summary provided by the JitsiConference.
  35. */
  36. stats: Object,
  37. /**
  38. * The search input criteria.
  39. */
  40. criteria: string,
  41. };
  42. /**
  43. * React component for displaying a list of speaker stats.
  44. *
  45. * @extends Component
  46. */
  47. class SpeakerStats extends Component<Props, State> {
  48. _updateInterval: IntervalID;
  49. /**
  50. * Initializes a new SpeakerStats instance.
  51. *
  52. * @param {Object} props - The read-only React Component props with which
  53. * the new instance is to be initialized.
  54. */
  55. constructor(props) {
  56. super(props);
  57. this.state = {
  58. stats: this._getSpeakerStats(),
  59. criteria: ''
  60. };
  61. // Bind event handlers so they are only bound once per instance.
  62. this._updateStats = this._updateStats.bind(this);
  63. this._onSearch = this._onSearch.bind(this);
  64. }
  65. /**
  66. * Begin polling for speaker stats updates.
  67. *
  68. * @inheritdoc
  69. */
  70. componentDidMount() {
  71. this._updateInterval = setInterval(this._updateStats, 1000);
  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.state.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. * Update the internal state with the latest speaker stats.
  106. *
  107. * @returns {void}
  108. * @private
  109. */
  110. _getSpeakerStats() {
  111. const stats = { ...this.props.conference.getSpeakerStats() };
  112. if (this.state?.criteria) {
  113. const searchRegex = new RegExp(this.state.criteria, 'gi');
  114. for (const id in stats) {
  115. if (stats[id].hasOwnProperty('_isLocalStats')) {
  116. const name = stats[id].isLocalStats() ? this.props._localDisplayName : stats[id].getDisplayName();
  117. if (!name || !name.match(searchRegex)) {
  118. delete stats[id];
  119. }
  120. }
  121. }
  122. }
  123. return stats;
  124. }
  125. /**
  126. * Create a SpeakerStatsItem instance for the passed in user id.
  127. *
  128. * @param {string} userId - User id used to look up the associated
  129. * speaker stats from the jitsi library.
  130. * @returns {SpeakerStatsItem|null}
  131. * @private
  132. */
  133. _createStatsItem(userId) {
  134. const statsModel = this.state.stats[userId];
  135. if (!statsModel) {
  136. return null;
  137. }
  138. const isDominantSpeaker = statsModel.isDominantSpeaker();
  139. const dominantSpeakerTime = statsModel.getTotalDominantSpeakerTime();
  140. const hasLeft = statsModel.hasLeft();
  141. let displayName;
  142. if (statsModel.isLocalStats()) {
  143. const { t } = this.props;
  144. const meString = t('me');
  145. displayName = this.props._localDisplayName;
  146. displayName
  147. = displayName ? `${displayName} (${meString})` : meString;
  148. } else {
  149. displayName
  150. = this.state.stats[userId].getDisplayName()
  151. || interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME;
  152. }
  153. return (
  154. <SpeakerStatsItem
  155. displayName = { displayName }
  156. dominantSpeakerTime = { dominantSpeakerTime }
  157. hasLeft = { hasLeft }
  158. isDominantSpeaker = { isDominantSpeaker }
  159. key = { userId } />
  160. );
  161. }
  162. _onSearch: () => void;
  163. /**
  164. * Search the existing participants by name.
  165. *
  166. * @returns {void}
  167. * @param {string} criteria - The search parameter.
  168. * @protected
  169. */
  170. _onSearch(criteria = '') {
  171. this.setState({
  172. ...this.state,
  173. criteria: escapeRegexp(criteria)
  174. });
  175. }
  176. _updateStats: () => void;
  177. /**
  178. * Update the internal state with the latest speaker stats.
  179. *
  180. * @returns {void}
  181. * @private
  182. */
  183. _updateStats() {
  184. const stats = this._getSpeakerStats();
  185. this.setState({
  186. ...this.state,
  187. stats
  188. });
  189. }
  190. }
  191. /**
  192. * Maps (parts of) the redux state to the associated SpeakerStats's props.
  193. *
  194. * @param {Object} state - The redux state.
  195. * @private
  196. * @returns {{
  197. * _localDisplayName: ?string
  198. * }}
  199. */
  200. function _mapStateToProps(state) {
  201. const localParticipant = getLocalParticipant(state);
  202. return {
  203. /**
  204. * The local display name.
  205. *
  206. * @private
  207. * @type {string|undefined}
  208. */
  209. _localDisplayName: localParticipant && localParticipant.name
  210. };
  211. }
  212. export default translate(connect(_mapStateToProps)(SpeakerStats));