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

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