jiti-meet/react/features/speaker-stats/components/SpeakerStats.js

252 lines
6.2 KiB
JavaScript
Raw Normal View History

2017-10-13 19:31:05 +00:00
// @flow
import React, { Component } from 'react';
import { Dialog } from '../../base/dialog';
import { translate } from '../../base/i18n';
import { getLocalParticipant } from '../../base/participants';
2019-03-21 16:38:29 +00:00
import { connect } from '../../base/redux';
import { escapeRegexp } from '../../base/util';
2019-03-21 16:38:29 +00:00
import SpeakerStatsItem from './SpeakerStatsItem';
import SpeakerStatsLabels from './SpeakerStatsLabels';
import SpeakerStatsSearch from './SpeakerStatsSearch';
2017-10-13 19:31:05 +00:00
declare var interfaceConfig: Object;
/**
* The type of the React {@code Component} props of {@link SpeakerStats}.
*/
type Props = {
/**
* The display name for the local participant obtained from the redux store.
*/
_localDisplayName: string,
/**
* The JitsiConference from which stats will be pulled.
*/
conference: Object,
/**
* The function to translate human-readable text.
*/
t: Function
};
/**
* The type of the React {@code Component} state of {@link SpeakerStats}.
*/
type State = {
/**
* The stats summary provided by the JitsiConference.
*/
stats: Object,
/**
* The search input criteria.
*/
criteria: string,
};
/**
* React component for displaying a list of speaker stats.
*
* @extends Component
*/
class SpeakerStats extends Component<Props, State> {
_updateInterval: IntervalID;
2017-10-13 19:31:05 +00:00
/**
* Initializes a new SpeakerStats instance.
*
* @param {Object} props - The read-only React Component props with which
* the new instance is to be initialized.
*/
constructor(props) {
super(props);
this.state = {
stats: this._getSpeakerStats(),
criteria: ''
};
2017-10-13 19:31:05 +00:00
// Bind event handlers so they are only bound once per instance.
this._updateStats = this._updateStats.bind(this);
this._onSearch = this._onSearch.bind(this);
}
/**
* Begin polling for speaker stats updates.
*
* @inheritdoc
*/
componentDidMount() {
this._updateInterval = setInterval(this._updateStats, 1000);
}
/**
* Stop polling for speaker stats updates.
*
* @inheritdoc
* @returns {void}
*/
componentWillUnmount() {
clearInterval(this._updateInterval);
}
/**
* Implements React's {@link Component#render()}.
*
* @inheritdoc
* @returns {ReactElement}
*/
render() {
const userIds = Object.keys(this.state.stats);
const items = userIds.map(userId => this._createStatsItem(userId));
return (
<Dialog
2019-03-06 17:23:53 +00:00
cancelKey = { 'dialog.close' }
submitDisabled = { true }
titleKey = { 'speakerStats.speakerStats' }>
<div className = 'speaker-stats'>
<SpeakerStatsSearch onSearch = { this._onSearch } />
<SpeakerStatsLabels />
{ items }
</div>
</Dialog>
);
}
/**
* Update the internal state with the latest speaker stats.
*
* @returns {void}
* @private
*/
_getSpeakerStats() {
const stats = { ...this.props.conference.getSpeakerStats() };
if (this.state?.criteria) {
const searchRegex = new RegExp(this.state.criteria, 'gi');
for (const id in stats) {
if (stats[id].hasOwnProperty('_isLocalStats')) {
const name = stats[id].isLocalStats() ? this.props._localDisplayName : stats[id].getDisplayName();
if (!name || !name.match(searchRegex)) {
delete stats[id];
}
}
}
}
return stats;
}
/**
* Create a SpeakerStatsItem instance for the passed in user id.
*
* @param {string} userId - User id used to look up the associated
* speaker stats from the jitsi library.
* @returns {SpeakerStatsItem|null}
* @private
*/
_createStatsItem(userId) {
const statsModel = this.state.stats[userId];
if (!statsModel) {
return null;
}
const isDominantSpeaker = statsModel.isDominantSpeaker();
const dominantSpeakerTime = statsModel.getTotalDominantSpeakerTime();
const hasLeft = statsModel.hasLeft();
2017-10-13 19:31:05 +00:00
let displayName;
if (statsModel.isLocalStats()) {
const { t } = this.props;
const meString = t('me');
displayName = this.props._localDisplayName;
2017-10-13 19:31:05 +00:00
displayName
= displayName ? `${displayName} (${meString})` : meString;
} else {
2017-10-13 19:31:05 +00:00
displayName
= this.state.stats[userId].getDisplayName()
|| interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME;
}
return (
<SpeakerStatsItem
displayName = { displayName }
dominantSpeakerTime = { dominantSpeakerTime }
hasLeft = { hasLeft }
isDominantSpeaker = { isDominantSpeaker }
key = { userId } />
);
}
2017-10-13 19:31:05 +00:00
_onSearch: () => void;
/**
* Search the existing participants by name.
*
* @returns {void}
* @param {string} criteria - The search parameter.
* @protected
*/
_onSearch(criteria = '') {
this.setState({
...this.state,
criteria: escapeRegexp(criteria)
});
}
2017-10-13 19:31:05 +00:00
_updateStats: () => void;
/**
* Update the internal state with the latest speaker stats.
*
* @returns {void}
* @private
*/
_updateStats() {
const stats = this._getSpeakerStats();
2017-10-13 19:31:05 +00:00
this.setState({
...this.state,
stats
});
2017-10-13 19:31:05 +00:00
}
}
/**
2017-10-13 19:31:05 +00:00
* Maps (parts of) the redux state to the associated SpeakerStats's props.
*
2017-10-13 19:31:05 +00:00
* @param {Object} state - The redux state.
* @private
* @returns {{
2017-10-13 19:31:05 +00:00
* _localDisplayName: ?string
* }}
*/
function _mapStateToProps(state) {
const localParticipant = getLocalParticipant(state);
return {
/**
* The local display name.
2017-10-13 19:31:05 +00:00
*
* @private
* @type {string|undefined}
*/
_localDisplayName: localParticipant && localParticipant.name
};
}
export default translate(connect(_mapStateToProps)(SpeakerStats));