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.

ConnectionStatus.tsx 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import React, { useCallback, useState } from 'react';
  2. import { WithTranslation } from 'react-i18next';
  3. import { makeStyles } from 'tss-react/mui';
  4. import { IReduxState } from '../../../../app/types';
  5. import { translate } from '../../../i18n/functions';
  6. import Icon from '../../../icons/components/Icon';
  7. import { IconArrowDown, IconWifi1Bar, IconWifi2Bars, IconWifi3Bars } from '../../../icons/svg';
  8. import { connect } from '../../../redux/functions';
  9. import { withPixelLineHeight } from '../../../styles/functions.web';
  10. import { PREJOIN_DEFAULT_CONTENT_WIDTH } from '../../../ui/components/variables';
  11. import { CONNECTION_TYPE } from '../../constants';
  12. import { getConnectionData } from '../../functions';
  13. interface IProps extends WithTranslation {
  14. /**
  15. * List of strings with details about the connection.
  16. */
  17. connectionDetails?: string[];
  18. /**
  19. * The type of the connection. Can be: 'none', 'poor', 'nonOptimal' or 'good'.
  20. */
  21. connectionType?: string;
  22. }
  23. const useStyles = makeStyles()(theme => {
  24. return {
  25. connectionStatus: {
  26. color: '#fff',
  27. ...withPixelLineHeight(theme.typography.bodyShortRegular),
  28. position: 'absolute',
  29. width: '100%',
  30. [theme.breakpoints.down(400)]: {
  31. margin: 0,
  32. width: '100%'
  33. },
  34. '@media (max-width: 720px)': {
  35. margin: `${theme.spacing(4)} auto`,
  36. position: 'fixed',
  37. top: 0,
  38. width: PREJOIN_DEFAULT_CONTENT_WIDTH
  39. },
  40. // mobile phone landscape
  41. '@media (max-height: 420px)': {
  42. display: 'none'
  43. },
  44. '& .con-status-header': {
  45. backgroundColor: 'rgba(0, 0, 0, 0.7)',
  46. alignItems: 'center',
  47. display: 'flex',
  48. padding: '12px 16px',
  49. borderRadius: theme.shape.borderRadius
  50. },
  51. '& .con-status-circle': {
  52. borderRadius: '50%',
  53. display: 'inline-block',
  54. padding: theme.spacing(1),
  55. marginRight: theme.spacing(2)
  56. },
  57. '& .con-status--good': {
  58. background: '#31B76A'
  59. },
  60. '& .con-status--poor': {
  61. background: '#E12D2D'
  62. },
  63. '& .con-status--non-optimal': {
  64. background: '#E39623'
  65. },
  66. '& .con-status-arrow': {
  67. marginLeft: 'auto',
  68. transition: 'background-color 0.16s ease-out'
  69. },
  70. '& .con-status-arrow--up': {
  71. transform: 'rotate(180deg)'
  72. },
  73. '& .con-status-arrow > svg': {
  74. cursor: 'pointer'
  75. },
  76. '& .con-status-arrow:hover': {
  77. backgroundColor: 'rgba(1, 1, 1, 0.1)'
  78. },
  79. '& .con-status-text': {
  80. textAlign: 'center'
  81. },
  82. '& .con-status-details': {
  83. backgroundColor: 'rgba(0, 0, 0, 0.7)',
  84. borderTop: '1px solid #5E6D7A',
  85. padding: theme.spacing(3),
  86. transition: 'opacity 0.16s ease-out'
  87. },
  88. '& .con-status-details-visible': {
  89. opacity: 1
  90. },
  91. '& .con-status-details-hidden': {
  92. opacity: 0
  93. }
  94. }
  95. };
  96. });
  97. const CONNECTION_TYPE_MAP: {
  98. [key: string]: {
  99. connectionClass: string;
  100. connectionText: string;
  101. icon: Function;
  102. };
  103. } = {
  104. [CONNECTION_TYPE.POOR]: {
  105. connectionClass: 'con-status--poor',
  106. icon: IconWifi1Bar,
  107. connectionText: 'prejoin.connection.poor'
  108. },
  109. [CONNECTION_TYPE.NON_OPTIMAL]: {
  110. connectionClass: 'con-status--non-optimal',
  111. icon: IconWifi2Bars,
  112. connectionText: 'prejoin.connection.nonOptimal'
  113. },
  114. [CONNECTION_TYPE.GOOD]: {
  115. connectionClass: 'con-status--good',
  116. icon: IconWifi3Bars,
  117. connectionText: 'prejoin.connection.good'
  118. }
  119. };
  120. /**
  121. * Component displaying information related to the connection & audio/video quality.
  122. *
  123. * @param {IProps} props - The props of the component.
  124. * @returns {ReactElement}
  125. */
  126. function ConnectionStatus({ connectionDetails, t, connectionType }: IProps) {
  127. const { classes } = useStyles();
  128. const [ showDetails, toggleDetails ] = useState(false);
  129. const arrowClassName = showDetails
  130. ? 'con-status-arrow con-status-arrow--up'
  131. : 'con-status-arrow';
  132. const detailsText = connectionDetails?.map(d => t(d)).join(' ');
  133. const detailsClassName = showDetails
  134. ? 'con-status-details-visible'
  135. : 'con-status-details-hidden';
  136. const onToggleDetails = useCallback(e => {
  137. e.preventDefault();
  138. toggleDetails(!showDetails);
  139. }, [ showDetails, toggleDetails ]);
  140. const onKeyPressToggleDetails = useCallback(e => {
  141. if (toggleDetails && (e.key === ' ' || e.key === 'Enter')) {
  142. e.preventDefault();
  143. toggleDetails(!showDetails);
  144. }
  145. }, [ showDetails, toggleDetails ]);
  146. if (connectionType === CONNECTION_TYPE.NONE) {
  147. return null;
  148. }
  149. const { connectionClass, icon, connectionText } = CONNECTION_TYPE_MAP[connectionType ?? ''];
  150. return (
  151. <div className = { classes.connectionStatus }>
  152. <div
  153. aria-level = { 1 }
  154. className = 'con-status-header'
  155. role = 'heading'>
  156. <div className = { `con-status-circle ${connectionClass}` }>
  157. <Icon
  158. size = { 16 }
  159. src = { icon } />
  160. </div>
  161. <span
  162. aria-hidden = { !showDetails }
  163. className = 'con-status-text'
  164. id = 'connection-status-description'>{t(connectionText)}</span>
  165. <Icon
  166. ariaDescribedBy = 'connection-status-description'
  167. ariaPressed = { showDetails }
  168. className = { arrowClassName }
  169. onClick = { onToggleDetails }
  170. onKeyPress = { onKeyPressToggleDetails }
  171. role = 'button'
  172. size = { 24 }
  173. src = { IconArrowDown }
  174. tabIndex = { 0 } />
  175. </div>
  176. <div
  177. aria-level = { 2 }
  178. className = { `con-status-details ${detailsClassName}` }
  179. role = 'heading'>
  180. {detailsText}</div>
  181. </div>
  182. );
  183. }
  184. /**
  185. * Maps (parts of) the redux state to the React {@code Component} props.
  186. *
  187. * @param {Object} state - The redux state.
  188. * @returns {Object}
  189. */
  190. function mapStateToProps(state: IReduxState): Object {
  191. const { connectionDetails, connectionType } = getConnectionData(state);
  192. return {
  193. connectionDetails,
  194. connectionType
  195. };
  196. }
  197. export default translate(connect(mapStateToProps)(ConnectionStatus));