import React, { Component } from 'react'; import PropTypes from 'prop-types'; import ResizeObserver from 'resize-observer-polyfill'; import { connect } from 'react-redux'; import debounce from 'lodash/debounce'; import classnames from 'classnames'; import * as stateActions from '../redux/stateActions'; import Peer from './Peer'; import HiddenPeers from './HiddenPeers'; class Filmstrip extends Component { constructor(props) { super(props); this.activePeerContainer = React.createRef(); } state = { lastSpeaker : null, width : 400 }; // Find the name of the peer which is currently speaking. This is either // the latest active speaker, or the manually selected peer, or, if no // person has spoken yet, the first peer in the list of peers. getActivePeerName = () => { if (this.props.selectedPeerName) { return this.props.selectedPeerName; } if (this.state.lastSpeaker) { return this.state.lastSpeaker; } const peerNames = Object.keys(this.props.peers); if (peerNames.length > 0) { return peerNames[0]; } }; isSharingCamera = (peerName) => this.props.peers[peerName] && this.props.peers[peerName].consumers.some((consumer) => this.props.consumers[consumer].source === 'screen'); getRatio = () => { let ratio = 4 / 3; if (this.isSharingCamera(this.getActivePeerName())) { ratio *= 2; } return ratio; }; updateDimensions = debounce(() => { const container = this.activePeerContainer.current; if (container) { const ratio = this.getRatio(); let width = container.clientWidth; if (width / ratio > container.clientHeight) { width = container.clientHeight * ratio; } this.setState({ width }); } }, 200); componentDidMount() { window.addEventListener('resize', this.updateDimensions); const observer = new ResizeObserver(this.updateDimensions); observer.observe(this.activePeerContainer.current); this.updateDimensions(); } componentWillUnmount() { window.removeEventListener('resize', this.updateDimensions); } componentDidUpdate(prevProps) { if (prevProps !== this.props) { this.updateDimensions(); if (this.props.activeSpeakerName !== this.props.myName) { // eslint-disable-next-line react/no-did-update-set-state this.setState({ lastSpeaker : this.props.activeSpeakerName }); } } } render() { const { peers, advancedMode, spotlights, spotlightsLength } = this.props; const activePeerName = this.getActivePeerName(); return (