您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ConnectionStatus.tsx 6.9KB

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