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

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