Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ConnectionIndicator.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. * Disable/enable inactive indicator.
  58. */
  59. _connectionIndicatorInactiveDisabled: boolean,
  60. /**
  61. * Wether the indicator popover is disabled.
  62. */
  63. _popoverDisabled: boolean,
  64. /**
  65. * Whether or not the component should ignore setting a visibility class for
  66. * hiding the component when the connection quality is not strong.
  67. */
  68. alwaysVisible: boolean,
  69. /**
  70. * The audio SSRC of this client.
  71. */
  72. audioSsrc: number,
  73. /**
  74. * The Redux dispatch function.
  75. */
  76. dispatch: Dispatch<any>,
  77. /**
  78. * Whether or not clicking the indicator should display a popover for more
  79. * details.
  80. */
  81. enableStatsDisplay: boolean,
  82. /**
  83. * The font-size for the icon.
  84. */
  85. iconSize: number,
  86. /**
  87. * Relative to the icon from where the popover for more connection details
  88. * should display.
  89. */
  90. statsPopoverPosition: string,
  91. /**
  92. * Invoked to obtain translated strings.
  93. */
  94. t: Function,
  95. };
  96. type State = AbstractState & {
  97. /**
  98. * Whether popover is ivisible or not.
  99. */
  100. popoverVisible: boolean
  101. }
  102. /**
  103. * Implements a React {@link Component} which displays the current connection
  104. * quality percentage and has a popover to show more detailed connection stats.
  105. *
  106. * @augments {Component}
  107. */
  108. class ConnectionIndicator extends AbstractConnectionIndicator<Props, State> {
  109. /**
  110. * Initializes a new {@code ConnectionIndicator} instance.
  111. *
  112. * @param {Object} props - The read-only properties with which the new
  113. * instance is to be initialized.
  114. */
  115. constructor(props: Props) {
  116. super(props);
  117. this.state = {
  118. showIndicator: false,
  119. stats: {},
  120. popoverVisible: false
  121. };
  122. this._onShowPopover = this._onShowPopover.bind(this);
  123. this._onHidePopover = this._onHidePopover.bind(this);
  124. }
  125. /**
  126. * Implements React's {@link Component#render()}.
  127. *
  128. * @inheritdoc
  129. * @returns {ReactElement}
  130. */
  131. render() {
  132. const { enableStatsDisplay, participantId, statsPopoverPosition } = this.props;
  133. const visibilityClass = this._getVisibilityClass();
  134. const rootClassNames = `indicator-container ${visibilityClass}`;
  135. if (this.props._popoverDisabled) {
  136. return this._renderIndicator();
  137. }
  138. return (
  139. <Popover
  140. className = { rootClassNames }
  141. content = { <ConnectionIndicatorContent
  142. inheritedStats = { this.state.stats }
  143. participantId = { participantId } /> }
  144. disablePopover = { !enableStatsDisplay }
  145. id = 'participant-connection-indicator'
  146. noPaddingContent = { true }
  147. onPopoverClose = { this._onHidePopover }
  148. onPopoverOpen = { this._onShowPopover }
  149. paddedContent = { true }
  150. position = { statsPopoverPosition }
  151. visible = { this.state.popoverVisible }>
  152. { this._renderIndicator() }
  153. </Popover>
  154. );
  155. }
  156. /**
  157. * Returns a CSS class that interprets the current connection status as a
  158. * color.
  159. *
  160. * @private
  161. * @returns {string}
  162. */
  163. _getConnectionColorClass() {
  164. const { _connectionStatus } = this.props;
  165. const { percent } = this.state.stats;
  166. const { INACTIVE, INTERRUPTED } = JitsiParticipantConnectionStatus;
  167. if (_connectionStatus === INACTIVE) {
  168. if (this.props._connectionIndicatorInactiveDisabled) {
  169. return 'status-disabled';
  170. }
  171. return 'status-other';
  172. } else if (_connectionStatus === INTERRUPTED) {
  173. return 'status-lost';
  174. } else if (typeof percent === 'undefined') {
  175. return 'status-high';
  176. }
  177. return this._getDisplayConfiguration(percent).colorClass;
  178. }
  179. /**
  180. * Get the icon configuration from QUALITY_TO_WIDTH which has a percentage
  181. * that matches or exceeds the passed in percentage. The implementation
  182. * assumes QUALITY_TO_WIDTH is already sorted by highest to lowest
  183. * percentage.
  184. *
  185. * @param {number} percent - The connection percentage, out of 100, to find
  186. * the closest matching configuration for.
  187. * @private
  188. * @returns {Object}
  189. */
  190. _getDisplayConfiguration(percent: number): Object {
  191. return QUALITY_TO_WIDTH.find(x => percent >= x.percent) || {};
  192. }
  193. /**
  194. * Returns additional class names to add to the root of the component. The
  195. * class names are intended to be used for hiding or showing the indicator.
  196. *
  197. * @private
  198. * @returns {string}
  199. */
  200. _getVisibilityClass() {
  201. const { _connectionStatus } = this.props;
  202. return this.state.showIndicator
  203. || this.props.alwaysVisible
  204. || _connectionStatus === JitsiParticipantConnectionStatus.INTERRUPTED
  205. || _connectionStatus === JitsiParticipantConnectionStatus.INACTIVE
  206. ? 'show-connection-indicator' : 'hide-connection-indicator';
  207. }
  208. _onHidePopover: () => void;
  209. /**
  210. * Hides popover.
  211. *
  212. * @private
  213. * @returns {void}
  214. */
  215. _onHidePopover() {
  216. this.setState({ popoverVisible: false });
  217. }
  218. /**
  219. * Creates a ReactElement for displaying an icon that represents the current
  220. * connection quality.
  221. *
  222. * @returns {ReactElement}
  223. */
  224. _renderIcon() {
  225. if (this.props._connectionStatus === JitsiParticipantConnectionStatus.INACTIVE) {
  226. if (this.props._connectionIndicatorInactiveDisabled) {
  227. return null;
  228. }
  229. return (
  230. <span className = 'connection_ninja'>
  231. <Icon
  232. className = 'icon-ninja'
  233. size = '1.5em'
  234. src = { IconConnectionInactive } />
  235. </span>
  236. );
  237. }
  238. let iconWidth;
  239. let emptyIconWrapperClassName = 'connection_empty';
  240. if (this.props._connectionStatus
  241. === JitsiParticipantConnectionStatus.INTERRUPTED) {
  242. // emptyIconWrapperClassName is used by the torture tests to
  243. // identify lost connection status handling.
  244. emptyIconWrapperClassName = 'connection_lost';
  245. iconWidth = '0%';
  246. } else if (typeof this.state.stats.percent === 'undefined') {
  247. iconWidth = '100%';
  248. } else {
  249. const { percent } = this.state.stats;
  250. iconWidth = this._getDisplayConfiguration(percent).width;
  251. }
  252. return [
  253. <span
  254. className = { emptyIconWrapperClassName }
  255. key = 'icon-empty'>
  256. <Icon
  257. className = 'icon-gsm-bars'
  258. size = '1em'
  259. src = { IconConnectionActive } />
  260. </span>,
  261. <span
  262. className = 'connection_full'
  263. key = 'icon-full'
  264. style = {{ width: iconWidth }}>
  265. <Icon
  266. className = 'icon-gsm-bars'
  267. size = '1em'
  268. src = { IconConnectionActive } />
  269. </span>
  270. ];
  271. }
  272. _onShowPopover: () => void;
  273. /**
  274. * Shows popover.
  275. *
  276. * @private
  277. * @returns {void}
  278. */
  279. _onShowPopover() {
  280. this.setState({ popoverVisible: true });
  281. }
  282. /**
  283. * Creates a ReactElement for displaying the indicator (GSM bar).
  284. *
  285. * @returns {ReactElement}
  286. */
  287. _renderIndicator() {
  288. const colorClass = this._getConnectionColorClass();
  289. const indicatorContainerClassNames
  290. = `connection-indicator indicator ${colorClass}`;
  291. return (
  292. <div className = 'popover-trigger'>
  293. <div
  294. className = { indicatorContainerClassNames }
  295. style = {{ fontSize: this.props.iconSize }}>
  296. <div className = 'connection indicatoricon'>
  297. { this._renderIcon() }
  298. </div>
  299. </div>
  300. </div>
  301. );
  302. }
  303. }
  304. /**
  305. * Maps part of the Redux state to the props of this component.
  306. *
  307. * @param {Object} state - The Redux state.
  308. * @param {Props} ownProps - The own props of the component.
  309. * @returns {Props}
  310. */
  311. export function _mapStateToProps(state: Object, ownProps: Props) {
  312. const { participantId } = ownProps;
  313. const participant
  314. = participantId ? getParticipantById(state, participantId) : getLocalParticipant(state);
  315. return {
  316. _connectionIndicatorInactiveDisabled:
  317. Boolean(state['features/base/config'].connectionIndicators?.inactiveDisabled),
  318. _popoverDisabled: state['features/base/config'].connectionIndicators?.disableDetails,
  319. _connectionStatus: participant?.connectionStatus
  320. };
  321. }
  322. export default translate(connect(_mapStateToProps)(ConnectionIndicator));