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 12KB

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