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 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 | null,
  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. return (
  121. <SpeakerStatsItem
  122. displayName = { statsModel.getDisplayName() }
  123. dominantSpeakerTime = { dominantSpeakerTime }
  124. hasLeft = { hasLeft }
  125. isDominantSpeaker = { isDominantSpeaker }
  126. key = { userId } />
  127. );
  128. }
  129. _onSearch: () => void;
  130. /**
  131. * Search the existing participants by name.
  132. *
  133. * @returns {void}
  134. * @param {string} criteria - The search parameter.
  135. * @protected
  136. */
  137. _onSearch(criteria = '') {
  138. this.props.dispatch(initSearch(escapeRegexp(criteria)));
  139. }
  140. _updateStats: () => void;
  141. /**
  142. * Update the internal state with the latest speaker stats.
  143. *
  144. * @returns {void}
  145. * @private
  146. */
  147. _updateStats() {
  148. this.props.dispatch(initUpdateStats(() => this._getSpeakerStats()));
  149. }
  150. /**
  151. * Update the internal state with the latest speaker stats.
  152. *
  153. * @returns {Object}
  154. * @private
  155. */
  156. _getSpeakerStats() {
  157. const stats = { ...this.props.conference.getSpeakerStats() };
  158. for (const userId in stats) {
  159. if (stats[userId]) {
  160. if (stats[userId].isLocalStats()) {
  161. const { t } = this.props;
  162. const meString = t('me');
  163. stats[userId].setDisplayName(
  164. this.props._localDisplayName
  165. ? `${this.props._localDisplayName} (${meString})`
  166. : meString
  167. );
  168. }
  169. if (!stats[userId].getDisplayName()) {
  170. stats[userId].setDisplayName(
  171. interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME
  172. );
  173. }
  174. }
  175. }
  176. return stats;
  177. }
  178. }
  179. /**
  180. * Maps (parts of) the redux state to the associated SpeakerStats's props.
  181. *
  182. * @param {Object} state - The redux state.
  183. * @private
  184. * @returns {{
  185. * _localDisplayName: ?string,
  186. * _stats: Object,
  187. * _criteria: string,
  188. * }}
  189. */
  190. function _mapStateToProps(state) {
  191. const localParticipant = getLocalParticipant(state);
  192. return {
  193. /**
  194. * The local display name.
  195. *
  196. * @private
  197. * @type {string|undefined}
  198. */
  199. _localDisplayName: localParticipant && localParticipant.name,
  200. _stats: getSpeakerStats(state),
  201. _criteria: getSearchCriteria(state)
  202. };
  203. }
  204. export default translate(connect(_mapStateToProps)(SpeakerStats));