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

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