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

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