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.

ConnectionIndicator.tsx 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. import { Theme } from '@mui/material';
  2. import { withStyles } from '@mui/styles';
  3. import clsx from 'clsx';
  4. import React from 'react';
  5. import { WithTranslation } from 'react-i18next';
  6. import { connect } from 'react-redux';
  7. import { IReduxState, IStore } from '../../../app/types';
  8. import { translate } from '../../../base/i18n/functions';
  9. import { MEDIA_TYPE } from '../../../base/media/constants';
  10. import {
  11. getLocalParticipant,
  12. getParticipantById,
  13. isScreenShareParticipant
  14. } from '../../../base/participants/functions';
  15. import Popover from '../../../base/popover/components/Popover.web';
  16. import {
  17. getTrackByMediaTypeAndParticipant,
  18. getVirtualScreenshareParticipantTrack
  19. } from '../../../base/tracks/functions';
  20. import {
  21. isTrackStreamingStatusInactive,
  22. isTrackStreamingStatusInterrupted
  23. } from '../../functions';
  24. import AbstractConnectionIndicator, {
  25. type Props as AbstractProps,
  26. type State as AbstractState,
  27. INDICATOR_DISPLAY_THRESHOLD,
  28. mapStateToProps as _abstractMapStateToProps
  29. // @ts-ignore
  30. } from '../AbstractConnectionIndicator';
  31. // @ts-ignore
  32. import ConnectionIndicatorContent from './ConnectionIndicatorContent';
  33. // eslint-disable-next-line lines-around-comment
  34. // @ts-ignore
  35. import { ConnectionIndicatorIcon } from './ConnectionIndicatorIcon';
  36. /**
  37. * An array of display configurations for the connection indicator and its bars.
  38. * The ordering is done specifically for faster iteration to find a matching
  39. * configuration to the current connection strength percentage.
  40. *
  41. * @type {Object[]}
  42. */
  43. const QUALITY_TO_WIDTH: Array<{
  44. colorClass: string;
  45. percent: number;
  46. tip: string;
  47. }> = [
  48. // Full (3 bars)
  49. {
  50. colorClass: 'status-high',
  51. percent: INDICATOR_DISPLAY_THRESHOLD,
  52. tip: 'connectionindicator.quality.good'
  53. },
  54. // 2 bars
  55. {
  56. colorClass: 'status-med',
  57. percent: 10,
  58. tip: 'connectionindicator.quality.nonoptimal'
  59. },
  60. // 1 bar
  61. {
  62. colorClass: 'status-low',
  63. percent: 0,
  64. tip: 'connectionindicator.quality.poor'
  65. }
  66. // Note: we never show 0 bars as long as there is a connection.
  67. ];
  68. /**
  69. * The type of the React {@code Component} props of {@link ConnectionIndicator}.
  70. */
  71. type Props = AbstractProps & WithTranslation & {
  72. /**
  73. * Disable/enable inactive indicator.
  74. */
  75. _connectionIndicatorInactiveDisabled: boolean;
  76. /**
  77. * Whether the indicator popover is disabled.
  78. */
  79. _popoverDisabled: boolean;
  80. /**
  81. * Whether or not the component should ignore setting a visibility class for
  82. * hiding the component when the connection quality is not strong.
  83. */
  84. alwaysVisible: boolean;
  85. /**
  86. * The audio SSRC of this client.
  87. */
  88. audioSsrc: number;
  89. /**
  90. * An object containing the CSS classes.
  91. */
  92. classes: any;
  93. /**
  94. * The Redux dispatch function.
  95. */
  96. dispatch: IStore['dispatch'];
  97. /**
  98. * Whether or not clicking the indicator should display a popover for more
  99. * details.
  100. */
  101. enableStatsDisplay: boolean;
  102. /**
  103. * The font-size for the icon.
  104. */
  105. iconSize: number;
  106. /**
  107. * Relative to the icon from where the popover for more connection details
  108. * should display.
  109. */
  110. statsPopoverPosition: string;
  111. };
  112. interface IState extends AbstractState {
  113. /**
  114. * Whether popover is ivisible or not.
  115. */
  116. popoverVisible: boolean;
  117. }
  118. const styles = (theme: Theme) => {
  119. return {
  120. container: {
  121. display: 'inline-block'
  122. },
  123. hidden: {
  124. display: 'none'
  125. },
  126. icon: {
  127. padding: '4px',
  128. borderRadius: '4px',
  129. '&.status-high': {
  130. backgroundColor: theme.palette.success01
  131. },
  132. '&.status-med': {
  133. backgroundColor: theme.palette.warning01
  134. },
  135. '&.status-low': {
  136. backgroundColor: theme.palette.iconError
  137. },
  138. '&.status-disabled': {
  139. background: 'transparent'
  140. },
  141. '&.status-lost': {
  142. backgroundColor: theme.palette.ui05
  143. },
  144. '&.status-other': {
  145. backgroundColor: theme.palette.action01
  146. }
  147. },
  148. inactiveIcon: {
  149. padding: 0,
  150. borderRadius: '50%'
  151. }
  152. };
  153. };
  154. /**
  155. * Implements a React {@link Component} which displays the current connection
  156. * quality percentage and has a popover to show more detailed connection stats.
  157. *
  158. * @augments {Component}
  159. */
  160. class ConnectionIndicator extends AbstractConnectionIndicator<Props, IState> {
  161. /**
  162. * Initializes a new {@code ConnectionIndicator} instance.
  163. *
  164. * @param {Object} props - The read-only properties with which the new
  165. * instance is to be initialized.
  166. */
  167. constructor(props: Props) {
  168. super(props);
  169. // @ts-ignore
  170. this.state = {
  171. showIndicator: false,
  172. stats: {},
  173. popoverVisible: false
  174. };
  175. this._onShowPopover = this._onShowPopover.bind(this);
  176. this._onHidePopover = this._onHidePopover.bind(this);
  177. }
  178. /**
  179. * Implements React's {@link Component#render()}.
  180. *
  181. * @inheritdoc
  182. * @returns {ReactElement}
  183. */
  184. render() {
  185. // @ts-ignore
  186. const { enableStatsDisplay, participantId, statsPopoverPosition, classes } = this.props;
  187. const visibilityClass = this._getVisibilityClass();
  188. // @ts-ignore
  189. if (this.props._popoverDisabled) {
  190. return this._renderIndicator();
  191. }
  192. return (
  193. <Popover
  194. className = { clsx(classes.container, visibilityClass) }
  195. content = { <ConnectionIndicatorContent
  196. // @ts-ignore
  197. inheritedStats = { this.state.stats }
  198. participantId = { participantId } /> }
  199. disablePopover = { !enableStatsDisplay }
  200. id = 'participant-connection-indicator'
  201. onPopoverClose = { this._onHidePopover }
  202. onPopoverOpen = { this._onShowPopover }
  203. position = { statsPopoverPosition }
  204. // @ts-ignore
  205. visible = { this.state.popoverVisible }>
  206. { this._renderIndicator() }
  207. </Popover>
  208. );
  209. }
  210. /**
  211. * Returns a CSS class that interprets the current connection status as a
  212. * color.
  213. *
  214. * @private
  215. * @returns {string}
  216. */
  217. _getConnectionColorClass() {
  218. // TODO We currently do not have logic to emit and handle stats changes for tracks.
  219. // @ts-ignore
  220. const { percent } = this.state.stats;
  221. const {
  222. _isConnectionStatusInactive,
  223. _isConnectionStatusInterrupted,
  224. _connectionIndicatorInactiveDisabled
  225. // @ts-ignore
  226. } = this.props;
  227. if (_isConnectionStatusInactive) {
  228. if (_connectionIndicatorInactiveDisabled) {
  229. return 'status-disabled';
  230. }
  231. return 'status-other';
  232. } else if (_isConnectionStatusInterrupted) {
  233. return 'status-lost';
  234. } else if (typeof percent === 'undefined') {
  235. return 'status-high';
  236. }
  237. return this._getDisplayConfiguration(percent).colorClass;
  238. }
  239. /**
  240. * Get the icon configuration from QUALITY_TO_WIDTH which has a percentage
  241. * that matches or exceeds the passed in percentage. The implementation
  242. * assumes QUALITY_TO_WIDTH is already sorted by highest to lowest
  243. * percentage.
  244. *
  245. * @param {number} percent - The connection percentage, out of 100, to find
  246. * the closest matching configuration for.
  247. * @private
  248. * @returns {Object}
  249. */
  250. _getDisplayConfiguration(percent: number): any {
  251. return QUALITY_TO_WIDTH.find(x => percent >= x.percent) || {};
  252. }
  253. /**
  254. * Returns additional class names to add to the root of the component. The
  255. * class names are intended to be used for hiding or showing the indicator.
  256. *
  257. * @private
  258. * @returns {string}
  259. */
  260. _getVisibilityClass() {
  261. // @ts-ignore
  262. const { _isConnectionStatusInactive, _isConnectionStatusInterrupted, classes } = this.props;
  263. // @ts-ignore
  264. return this.state.showIndicator
  265. // @ts-ignore
  266. || this.props.alwaysVisible
  267. || _isConnectionStatusInterrupted
  268. || _isConnectionStatusInactive
  269. ? '' : classes.hidden;
  270. }
  271. /**
  272. * Hides popover.
  273. *
  274. * @private
  275. * @returns {void}
  276. */
  277. _onHidePopover() {
  278. // @ts-ignore
  279. this.setState({ popoverVisible: false });
  280. }
  281. /**
  282. * Shows popover.
  283. *
  284. * @private
  285. * @returns {void}
  286. */
  287. _onShowPopover() {
  288. // @ts-ignore
  289. this.setState({ popoverVisible: true });
  290. }
  291. /**
  292. * Creates a ReactElement for displaying the indicator (GSM bar).
  293. *
  294. * @returns {ReactElement}
  295. */
  296. _renderIndicator() {
  297. const {
  298. _isConnectionStatusInactive,
  299. _isConnectionStatusInterrupted,
  300. _connectionIndicatorInactiveDisabled,
  301. _videoTrack,
  302. classes,
  303. iconSize
  304. // @ts-ignore
  305. } = this.props;
  306. return (
  307. <div
  308. style = {{ fontSize: iconSize }}>
  309. <ConnectionIndicatorIcon
  310. classes = { classes }
  311. colorClass = { this._getConnectionColorClass() }
  312. connectionIndicatorInactiveDisabled = { _connectionIndicatorInactiveDisabled }
  313. isConnectionStatusInactive = { _isConnectionStatusInactive }
  314. isConnectionStatusInterrupted = { _isConnectionStatusInterrupted }
  315. track = { _videoTrack } />
  316. </div>
  317. );
  318. }
  319. }
  320. /**
  321. * Maps part of the Redux state to the props of this component.
  322. *
  323. * @param {Object} state - The Redux state.
  324. * @param {Props} ownProps - The own props of the component.
  325. * @returns {Props}
  326. */
  327. export function _mapStateToProps(state: IReduxState, ownProps: Props) {
  328. const { participantId } = ownProps;
  329. const tracks = state['features/base/tracks'];
  330. const participant = participantId ? getParticipantById(state, participantId) : getLocalParticipant(state);
  331. let _videoTrack = getTrackByMediaTypeAndParticipant(tracks, MEDIA_TYPE.VIDEO, participantId);
  332. if (isScreenShareParticipant(participant)) {
  333. _videoTrack = getVirtualScreenshareParticipantTrack(tracks, participantId);
  334. }
  335. const _isConnectionStatusInactive = isTrackStreamingStatusInactive(_videoTrack);
  336. const _isConnectionStatusInterrupted = isTrackStreamingStatusInterrupted(_videoTrack);
  337. return {
  338. ..._abstractMapStateToProps(state),
  339. _connectionIndicatorInactiveDisabled:
  340. Boolean(state['features/base/config'].connectionIndicators?.inactiveDisabled),
  341. _isVirtualScreenshareParticipant: isScreenShareParticipant(participant),
  342. _popoverDisabled: state['features/base/config'].connectionIndicators?.disableDetails,
  343. _isConnectionStatusInactive,
  344. _isConnectionStatusInterrupted,
  345. _videoTrack
  346. };
  347. }
  348. export default translate(connect(_mapStateToProps)(
  349. // @ts-ignore
  350. withStyles(styles)(ConnectionIndicator)));