2021-11-10 17:49:53 +00:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import { useCallback, useEffect } from 'react';
|
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
|
|
|
|
|
|
|
import { getLocalParticipant } from '../../base/participants';
|
|
|
|
import { initUpdateStats } from '../actions';
|
|
|
|
import {
|
|
|
|
SPEAKER_STATS_RELOAD_INTERVAL
|
|
|
|
} from '../constants';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Component that renders the list of speaker stats.
|
|
|
|
*
|
|
|
|
* @param {Function} speakerStatsItem - React element tu use when rendering.
|
2021-12-28 14:35:21 +00:00
|
|
|
* @param {Object} itemStyles - Styles for the speaker stats item.
|
2021-11-10 17:49:53 +00:00
|
|
|
* @returns {Function}
|
|
|
|
*/
|
2021-12-28 14:35:21 +00:00
|
|
|
const abstractSpeakerStatsList = (speakerStatsItem: Function, itemStyles?: Object): Function[] => {
|
2021-11-10 17:49:53 +00:00
|
|
|
const dispatch = useDispatch();
|
|
|
|
const { t } = useTranslation();
|
|
|
|
const conference = useSelector(state => state['features/base/conference'].conference);
|
2021-12-28 14:35:21 +00:00
|
|
|
const { stats: speakerStats, showFacialExpressions } = useSelector(state => state['features/speaker-stats']);
|
2021-11-10 17:49:53 +00:00
|
|
|
const localParticipant = useSelector(getLocalParticipant);
|
2021-12-28 14:35:21 +00:00
|
|
|
const { defaultRemoteDisplayName } = useSelector(
|
2021-12-14 15:02:22 +00:00
|
|
|
state => state['features/base/config']) || {};
|
2021-12-28 14:35:21 +00:00
|
|
|
const { enableDisplayFacialExpressions } = useSelector(state => state['features/base/config']) || {};
|
2021-11-29 09:33:38 +00:00
|
|
|
const { facialExpressions: localFacialExpressions } = useSelector(
|
|
|
|
state => state['features/facial-recognition']) || {};
|
2021-11-10 17:49:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the internal state with the latest speaker stats.
|
|
|
|
*
|
|
|
|
* @returns {Object}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
const getLocalSpeakerStats = useCallback(() => {
|
|
|
|
const stats = conference.getSpeakerStats();
|
|
|
|
|
|
|
|
for (const userId in stats) {
|
|
|
|
if (stats[userId]) {
|
|
|
|
if (stats[userId].isLocalStats()) {
|
|
|
|
const meString = t('me');
|
|
|
|
|
|
|
|
stats[userId].setDisplayName(
|
|
|
|
localParticipant.name
|
|
|
|
? `${localParticipant.name} (${meString})`
|
|
|
|
: meString
|
|
|
|
);
|
2021-12-28 14:35:21 +00:00
|
|
|
if (enableDisplayFacialExpressions) {
|
2021-11-10 17:49:53 +00:00
|
|
|
stats[userId].setFacialExpressions(localFacialExpressions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!stats[userId].getDisplayName()) {
|
|
|
|
stats[userId].setDisplayName(
|
|
|
|
conference.getParticipantById(userId)?.name
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stats;
|
|
|
|
});
|
|
|
|
|
|
|
|
const updateStats = useCallback(
|
2021-11-29 09:33:38 +00:00
|
|
|
() => dispatch(initUpdateStats(getLocalSpeakerStats)),
|
|
|
|
[ dispatch, initUpdateStats ]);
|
2021-11-10 17:49:53 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2021-11-29 09:33:38 +00:00
|
|
|
const intervalId = setInterval(() => {
|
|
|
|
updateStats();
|
|
|
|
}, SPEAKER_STATS_RELOAD_INTERVAL);
|
2021-11-10 17:49:53 +00:00
|
|
|
|
2021-11-29 09:33:38 +00:00
|
|
|
return () => clearInterval(intervalId);
|
|
|
|
}, []);
|
2021-11-10 17:49:53 +00:00
|
|
|
|
2021-11-29 09:33:38 +00:00
|
|
|
const localSpeakerStats = Object.keys(speakerStats).length === 0 ? getLocalSpeakerStats() : speakerStats;
|
2021-12-28 14:35:21 +00:00
|
|
|
const userIds = Object.keys(localSpeakerStats).filter(id => localSpeakerStats[id] && !localSpeakerStats[id].hidden);
|
2021-11-10 17:49:53 +00:00
|
|
|
|
|
|
|
return userIds.map(userId => {
|
2021-11-29 09:33:38 +00:00
|
|
|
const statsModel = localSpeakerStats[userId];
|
2021-11-10 17:49:53 +00:00
|
|
|
const props = {};
|
|
|
|
|
|
|
|
props.isDominantSpeaker = statsModel.isDominantSpeaker();
|
|
|
|
props.dominantSpeakerTime = statsModel.getTotalDominantSpeakerTime();
|
2021-11-29 09:33:38 +00:00
|
|
|
props.participantId = userId;
|
2021-11-10 17:49:53 +00:00
|
|
|
props.hasLeft = statsModel.hasLeft();
|
2021-12-28 14:35:21 +00:00
|
|
|
if (showFacialExpressions) {
|
2021-11-10 17:49:53 +00:00
|
|
|
props.facialExpressions = statsModel.getFacialExpressions();
|
|
|
|
}
|
2021-12-28 14:35:21 +00:00
|
|
|
props.hidden = statsModel.hidden;
|
|
|
|
props.showFacialExpressions = showFacialExpressions;
|
2021-12-14 15:02:22 +00:00
|
|
|
props.displayName = statsModel.getDisplayName() || defaultRemoteDisplayName;
|
2021-12-28 14:35:21 +00:00
|
|
|
if (itemStyles) {
|
|
|
|
props.styles = itemStyles;
|
|
|
|
}
|
2021-11-10 17:49:53 +00:00
|
|
|
props.t = t;
|
|
|
|
|
|
|
|
return speakerStatsItem(props);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export default abstractSpeakerStatsList;
|