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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. * Relative to the icon from where the popover for more connection details
  80. * should display.
  81. */
  82. statsPopoverPosition: string,
  83. /**
  84. * Invoked to obtain translated strings.
  85. */
  86. t: Function,
  87. };
  88. /**
  89. * Implements a React {@link Component} which displays the current connection
  90. * quality percentage and has a popover to show more detailed connection stats.
  91. *
  92. * @extends {Component}
  93. */
  94. class ConnectionIndicator extends AbstractConnectionIndicator<Props, AbstractState> {
  95. /**
  96. * Initializes a new {@code ConnectionIndicator} instance.
  97. *
  98. * @param {Object} props - The read-only properties with which the new
  99. * instance is to be initialized.
  100. */
  101. constructor(props: Props) {
  102. super(props);
  103. this.state = {
  104. autoHideTimeout: undefined,
  105. showIndicator: false,
  106. stats: {}
  107. };
  108. }
  109. /**
  110. * Implements React's {@link Component#render()}.
  111. *
  112. * @inheritdoc
  113. * @returns {ReactElement}
  114. */
  115. render() {
  116. const visibilityClass = this._getVisibilityClass();
  117. const rootClassNames = `indicator-container ${visibilityClass}`;
  118. const colorClass = this._getConnectionColorClass();
  119. const indicatorContainerClassNames
  120. = `connection-indicator indicator ${colorClass}`;
  121. return (
  122. <Popover
  123. className = { rootClassNames }
  124. content = { <ConnectionIndicatorContent
  125. inheritedStats = { this.state.stats }
  126. participantId = { this.props.participantId } /> }
  127. disablePopover = { !this.props.enableStatsDisplay }
  128. position = { this.props.statsPopoverPosition }>
  129. <div className = 'popover-trigger'>
  130. <div
  131. className = { indicatorContainerClassNames }
  132. style = {{ fontSize: this.props.iconSize }}>
  133. <div className = 'connection indicatoricon'>
  134. { this._renderIcon() }
  135. </div>
  136. </div>
  137. </div>
  138. </Popover>
  139. );
  140. }
  141. /**
  142. * Returns a CSS class that interprets the current connection status as a
  143. * color.
  144. *
  145. * @private
  146. * @returns {string}
  147. */
  148. _getConnectionColorClass() {
  149. const { _connectionStatus } = this.props;
  150. const { percent } = this.state.stats;
  151. const { INACTIVE, INTERRUPTED } = JitsiParticipantConnectionStatus;
  152. if (_connectionStatus === INACTIVE) {
  153. return 'status-other';
  154. } else if (_connectionStatus === INTERRUPTED) {
  155. return 'status-lost';
  156. } else if (typeof percent === 'undefined') {
  157. return 'status-high';
  158. }
  159. return this._getDisplayConfiguration(percent).colorClass;
  160. }
  161. /**
  162. * Get the icon configuration from QUALITY_TO_WIDTH which has a percentage
  163. * that matches or exceeds the passed in percentage. The implementation
  164. * assumes QUALITY_TO_WIDTH is already sorted by highest to lowest
  165. * percentage.
  166. *
  167. * @param {number} percent - The connection percentage, out of 100, to find
  168. * the closest matching configuration for.
  169. * @private
  170. * @returns {Object}
  171. */
  172. _getDisplayConfiguration(percent: number): Object {
  173. return QUALITY_TO_WIDTH.find(x => percent >= x.percent) || {};
  174. }
  175. /**
  176. * Returns additional class names to add to the root of the component. The
  177. * class names are intended to be used for hiding or showing the indicator.
  178. *
  179. * @private
  180. * @returns {string}
  181. */
  182. _getVisibilityClass() {
  183. const { _connectionStatus } = this.props;
  184. return this.state.showIndicator
  185. || this.props.alwaysVisible
  186. || _connectionStatus === JitsiParticipantConnectionStatus.INTERRUPTED
  187. || _connectionStatus === JitsiParticipantConnectionStatus.INACTIVE
  188. ? 'show-connection-indicator' : 'hide-connection-indicator';
  189. }
  190. /**
  191. * Creates a ReactElement for displaying an icon that represents the current
  192. * connection quality.
  193. *
  194. * @returns {ReactElement}
  195. */
  196. _renderIcon() {
  197. if (this.props._connectionStatus
  198. === JitsiParticipantConnectionStatus.INACTIVE) {
  199. return (
  200. <span className = 'connection_ninja'>
  201. <Icon
  202. className = 'icon-ninja'
  203. size = '1.5em'
  204. src = { IconConnectionInactive } />
  205. </span>
  206. );
  207. }
  208. let iconWidth;
  209. let emptyIconWrapperClassName = 'connection_empty';
  210. if (this.props._connectionStatus
  211. === JitsiParticipantConnectionStatus.INTERRUPTED) {
  212. // emptyIconWrapperClassName is used by the torture tests to
  213. // identify lost connection status handling.
  214. emptyIconWrapperClassName = 'connection_lost';
  215. iconWidth = '0%';
  216. } else if (typeof this.state.stats.percent === 'undefined') {
  217. iconWidth = '100%';
  218. } else {
  219. const { percent } = this.state.stats;
  220. iconWidth = this._getDisplayConfiguration(percent).width;
  221. }
  222. return [
  223. <span
  224. className = { emptyIconWrapperClassName }
  225. key = 'icon-empty'>
  226. <Icon
  227. className = 'icon-gsm-bars'
  228. size = '1em'
  229. src = { IconConnectionActive } />
  230. </span>,
  231. <span
  232. className = 'connection_full'
  233. key = 'icon-full'
  234. style = {{ width: iconWidth }}>
  235. <Icon
  236. className = 'icon-gsm-bars'
  237. size = '1em'
  238. src = { IconConnectionActive } />
  239. </span>
  240. ];
  241. }
  242. }
  243. /**
  244. * Maps part of the Redux state to the props of this component.
  245. *
  246. * @param {Object} state - The Redux state.
  247. * @param {Props} ownProps - The own props of the component.
  248. * @returns {Props}
  249. */
  250. export function _mapStateToProps(state: Object, ownProps: Props) {
  251. const { participantId } = ownProps;
  252. const participant
  253. = participantId ? getParticipantById(state, participantId) : getLocalParticipant(state);
  254. return {
  255. _connectionStatus: participant?.connectionStatus
  256. };
  257. }
  258. export default translate(connect(_mapStateToProps)(ConnectionIndicator));