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.js 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // @flow
  2. import React from 'react';
  3. import type { Dispatch } from 'redux';
  4. import { translate } from '../../../base/i18n';
  5. import { Icon, IconConnectionActive, IconConnectionInactive } from '../../../base/icons';
  6. import { JitsiParticipantConnectionStatus } from '../../../base/lib-jitsi-meet';
  7. import { getLocalParticipant, getParticipantById } from '../../../base/participants';
  8. import { Popover } from '../../../base/popover';
  9. import { connect } from '../../../base/redux';
  10. import AbstractConnectionIndicator, {
  11. INDICATOR_DISPLAY_THRESHOLD,
  12. type Props as AbstractProps,
  13. type State as AbstractState
  14. } from '../AbstractConnectionIndicator';
  15. import ConnectionIndicatorContent from './ConnectionIndicatorContent';
  16. /**
  17. * An array of display configurations for the connection indicator and its bars.
  18. * The ordering is done specifically for faster iteration to find a matching
  19. * configuration to the current connection strength percentage.
  20. *
  21. * @type {Object[]}
  22. */
  23. const QUALITY_TO_WIDTH: Array<Object> = [
  24. // Full (3 bars)
  25. {
  26. colorClass: 'status-high',
  27. percent: INDICATOR_DISPLAY_THRESHOLD,
  28. tip: 'connectionindicator.quality.good',
  29. width: '100%'
  30. },
  31. // 2 bars
  32. {
  33. colorClass: 'status-med',
  34. percent: 10,
  35. tip: 'connectionindicator.quality.nonoptimal',
  36. width: '66%'
  37. },
  38. // 1 bar
  39. {
  40. colorClass: 'status-low',
  41. percent: 0,
  42. tip: 'connectionindicator.quality.poor',
  43. width: '33%'
  44. }
  45. // Note: we never show 0 bars as long as there is a connection.
  46. ];
  47. /**
  48. * The type of the React {@code Component} props of {@link ConnectionIndicator}.
  49. */
  50. type Props = AbstractProps & {
  51. /**
  52. * The current condition of the user's connection, matching one of the
  53. * enumerated values in the library.
  54. */
  55. _connectionStatus: string,
  56. /**
  57. * Whether or not the component should ignore setting a visibility class for
  58. * hiding the component when the connection quality is not strong.
  59. */
  60. alwaysVisible: boolean,
  61. /**
  62. * The audio SSRC of this client.
  63. */
  64. audioSsrc: number,
  65. /**
  66. * The Redux dispatch function.
  67. */
  68. dispatch: Dispatch<any>,
  69. /**
  70. * Whether or not clicking the indicator should display a popover for more
  71. * details.
  72. */
  73. enableStatsDisplay: boolean,
  74. /**
  75. * The font-size for the icon.
  76. */
  77. iconSize: number,
  78. /**
  79. * Whether or not the displays stats are for local video.
  80. */
  81. isLocalVideo: boolean,
  82. /**
  83. * Relative to the icon from where the popover for more connection details
  84. * should display.
  85. */
  86. statsPopoverPosition: string,
  87. /**
  88. * Invoked to obtain translated strings.
  89. */
  90. t: Function,
  91. };
  92. /**
  93. * Implements a React {@link Component} which displays the current connection
  94. * quality percentage and has a popover to show more detailed connection stats.
  95. *
  96. * @extends {Component}
  97. */
  98. class ConnectionIndicator extends AbstractConnectionIndicator<Props, AbstractState> {
  99. /**
  100. * Initializes a new {@code ConnectionIndicator} instance.
  101. *
  102. * @param {Object} props - The read-only properties with which the new
  103. * instance is to be initialized.
  104. */
  105. constructor(props: Props) {
  106. super(props);
  107. this.state = {
  108. autoHideTimeout: undefined,
  109. showIndicator: false,
  110. stats: {}
  111. };
  112. }
  113. /**
  114. * Implements React's {@link Component#render()}.
  115. *
  116. * @inheritdoc
  117. * @returns {ReactElement}
  118. */
  119. render() {
  120. const visibilityClass = this._getVisibilityClass();
  121. const rootClassNames = `indicator-container ${visibilityClass}`;
  122. const colorClass = this._getConnectionColorClass();
  123. const indicatorContainerClassNames
  124. = `connection-indicator indicator ${colorClass}`;
  125. return (
  126. <Popover
  127. className = { rootClassNames }
  128. content = { <ConnectionIndicatorContent participantId = { this.props.participantId } /> }
  129. disablePopover = { !this.props.enableStatsDisplay }
  130. position = { this.props.statsPopoverPosition }>
  131. <div className = 'popover-trigger'>
  132. <div
  133. className = { indicatorContainerClassNames }
  134. style = {{ fontSize: this.props.iconSize }}>
  135. <div className = 'connection indicatoricon'>
  136. { this._renderIcon() }
  137. </div>
  138. </div>
  139. </div>
  140. </Popover>
  141. );
  142. }
  143. /**
  144. * Returns a CSS class that interprets the current connection status as a
  145. * color.
  146. *
  147. * @private
  148. * @returns {string}
  149. */
  150. _getConnectionColorClass() {
  151. const { _connectionStatus } = this.props;
  152. const { percent } = this.state.stats;
  153. const { INACTIVE, INTERRUPTED } = JitsiParticipantConnectionStatus;
  154. if (_connectionStatus === INACTIVE) {
  155. return 'status-other';
  156. } else if (_connectionStatus === INTERRUPTED) {
  157. return 'status-lost';
  158. } else if (typeof percent === 'undefined') {
  159. return 'status-high';
  160. }
  161. return this._getDisplayConfiguration(percent).colorClass;
  162. }
  163. /**
  164. * Get the icon configuration from QUALITY_TO_WIDTH which has a percentage
  165. * that matches or exceeds the passed in percentage. The implementation
  166. * assumes QUALITY_TO_WIDTH is already sorted by highest to lowest
  167. * percentage.
  168. *
  169. * @param {number} percent - The connection percentage, out of 100, to find
  170. * the closest matching configuration for.
  171. * @private
  172. * @returns {Object}
  173. */
  174. _getDisplayConfiguration(percent: number): Object {
  175. return QUALITY_TO_WIDTH.find(x => percent >= x.percent) || {};
  176. }
  177. /**
  178. * Returns additional class names to add to the root of the component. The
  179. * class names are intended to be used for hiding or showing the indicator.
  180. *
  181. * @private
  182. * @returns {string}
  183. */
  184. _getVisibilityClass() {
  185. const { _connectionStatus } = this.props;
  186. return this.state.showIndicator
  187. || this.props.alwaysVisible
  188. || _connectionStatus === JitsiParticipantConnectionStatus.INTERRUPTED
  189. || _connectionStatus === JitsiParticipantConnectionStatus.INACTIVE
  190. ? 'show-connection-indicator' : 'hide-connection-indicator';
  191. }
  192. /**
  193. * Creates a ReactElement for displaying an icon that represents the current
  194. * connection quality.
  195. *
  196. * @returns {ReactElement}
  197. */
  198. _renderIcon() {
  199. if (this.props._connectionStatus
  200. === JitsiParticipantConnectionStatus.INACTIVE) {
  201. return (
  202. <span className = 'connection_ninja'>
  203. <Icon
  204. className = 'icon-ninja'
  205. size = '1.5em'
  206. src = { IconConnectionInactive } />
  207. </span>
  208. );
  209. }
  210. let iconWidth;
  211. let emptyIconWrapperClassName = 'connection_empty';
  212. if (this.props._connectionStatus
  213. === JitsiParticipantConnectionStatus.INTERRUPTED) {
  214. // emptyIconWrapperClassName is used by the torture tests to
  215. // identify lost connection status handling.
  216. emptyIconWrapperClassName = 'connection_lost';
  217. iconWidth = '0%';
  218. } else if (typeof this.state.stats.percent === 'undefined') {
  219. iconWidth = '100%';
  220. } else {
  221. const { percent } = this.state.stats;
  222. iconWidth = this._getDisplayConfiguration(percent).width;
  223. }
  224. return [
  225. <span
  226. className = { emptyIconWrapperClassName }
  227. key = 'icon-empty'>
  228. <Icon
  229. className = 'icon-gsm-bars'
  230. size = '1em'
  231. src = { IconConnectionActive } />
  232. </span>,
  233. <span
  234. className = 'connection_full'
  235. key = 'icon-full'
  236. style = {{ width: iconWidth }}>
  237. <Icon
  238. className = 'icon-gsm-bars'
  239. size = '1em'
  240. src = { IconConnectionActive } />
  241. </span>
  242. ];
  243. }
  244. }
  245. /**
  246. * Maps part of the Redux state to the props of this component.
  247. *
  248. * @param {Object} state - The Redux state.
  249. * @param {Props} ownProps - The own props of the component.
  250. * @returns {Props}
  251. */
  252. export function _mapStateToProps(state: Object, ownProps: Props) {
  253. const { participantId } = ownProps;
  254. const participant
  255. = participantId ? getParticipantById(state, participantId) : getLocalParticipant(state);
  256. return {
  257. _connectionStatus: participant?.connectionStatus
  258. };
  259. }
  260. export default translate(connect(_mapStateToProps)(ConnectionIndicator));