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.

BandwidthSettingsDialog.tsx 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import React, { KeyboardEvent, useCallback, useState } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { useDispatch, useSelector } from 'react-redux';
  4. import { makeStyles } from 'tss-react/mui';
  5. // @ts-ignore
  6. import { MIN_ASSUMED_BANDWIDTH_BPS } from '../../../../../modules/API/constants';
  7. import { IReduxState } from '../../../app/types';
  8. import { setAssumedBandwidthBps as saveAssumedBandwidthBps } from '../../../base/conference/actions';
  9. import { IconInfoCircle } from '../../../base/icons/svg';
  10. import { withPixelLineHeight } from '../../../base/styles/functions.web';
  11. import Dialog from '../../../base/ui/components/web/Dialog';
  12. import Input from '../../../base/ui/components/web/Input';
  13. const useStyles = makeStyles()(theme => {
  14. return {
  15. content: {
  16. color: theme.palette.text01
  17. },
  18. info: {
  19. background: theme.palette.ui01,
  20. ...withPixelLineHeight(theme.typography.labelRegular),
  21. color: theme.palette.text02,
  22. marginTop: theme.spacing(2)
  23. },
  24. possibleValues: {
  25. margin: 0,
  26. paddingLeft: theme.spacing(4)
  27. }
  28. };
  29. });
  30. /**
  31. * Bandwidth settings dialog component.
  32. *
  33. * @returns {ReactElement}
  34. */
  35. const BandwidthSettingsDialog = () => {
  36. const { classes } = useStyles();
  37. const { t } = useTranslation();
  38. const dispatch = useDispatch();
  39. const [ showAssumedBandwidthInfo, setShowAssumedBandwidthInfo ] = useState(false);
  40. const currentAssumedBandwidthBps = useSelector(
  41. (state: IReduxState) => state['features/base/conference'].assumedBandwidthBps
  42. );
  43. const [ assumedBandwidthBps, setAssumedBandwidthBps ] = useState(
  44. currentAssumedBandwidthBps === MIN_ASSUMED_BANDWIDTH_BPS
  45. || currentAssumedBandwidthBps === undefined
  46. ? ''
  47. : currentAssumedBandwidthBps
  48. );
  49. /**
  50. * Changes the assumed bandwidth bps.
  51. *
  52. * @param {string} value - The key event to handle.
  53. *
  54. * @returns {void}
  55. */
  56. const onAssumedBandwidthBpsChange = useCallback((value: string) => {
  57. setAssumedBandwidthBps(value);
  58. }, [ setAssumedBandwidthBps ]);
  59. /**
  60. * Persists the assumed bandwidth bps.
  61. *
  62. * @param {string} value - The key event to handle.
  63. *
  64. * @returns {void}
  65. */
  66. const onAssumedBandwidthBpsSave = useCallback(() => {
  67. if (assumedBandwidthBps !== currentAssumedBandwidthBps) {
  68. dispatch(saveAssumedBandwidthBps(Number(
  69. assumedBandwidthBps === '' ? MIN_ASSUMED_BANDWIDTH_BPS : assumedBandwidthBps
  70. )));
  71. }
  72. }, [ assumedBandwidthBps, currentAssumedBandwidthBps, dispatch, saveAssumedBandwidthBps ]);
  73. /**
  74. * Validates the assumed bandwidth bps.
  75. *
  76. * @param {KeyboardEvent<any>} e - The key event to handle.
  77. *
  78. * @returns {void}
  79. */
  80. const onAssumedBandwidthBpsKeyPress = useCallback((e: KeyboardEvent<any>) => {
  81. const isValid = (e.charCode !== 8 && e.charCode === 0) || (e.charCode >= 48 && e.charCode <= 57);
  82. if (!isValid) {
  83. e.preventDefault();
  84. }
  85. }, []);
  86. /**
  87. * Callback invoked to hide or show the possible values
  88. * of the assumed bandwidth setting.
  89. *
  90. * @returns {void}
  91. */
  92. const toggleInfoPanel = useCallback(() => {
  93. setShowAssumedBandwidthInfo(!showAssumedBandwidthInfo);
  94. }, [ setShowAssumedBandwidthInfo, showAssumedBandwidthInfo ]);
  95. return (
  96. <Dialog
  97. onSubmit = { onAssumedBandwidthBpsSave }
  98. titleKey = 'bandwidthSettings.title'>
  99. <div className = { classes.content }>
  100. <Input
  101. bottomLabel = { t('bandwidthSettings.assumedBandwidthBpsWarning') }
  102. icon = { IconInfoCircle }
  103. iconClick = { toggleInfoPanel }
  104. id = 'setAssumedBandwidthBps'
  105. label = { t('bandwidthSettings.setAssumedBandwidthBps') }
  106. minValue = { 0 }
  107. name = 'assumedBandwidthBps'
  108. onChange = { onAssumedBandwidthBpsChange }
  109. onKeyPress = { onAssumedBandwidthBpsKeyPress }
  110. placeholder = { t('bandwidthSettings.assumedBandwidthBps') }
  111. type = 'number'
  112. value = { assumedBandwidthBps } />
  113. {showAssumedBandwidthInfo && (
  114. <div className = { classes.info }>
  115. <span>{t('bandwidthSettings.possibleValues')}:</span>
  116. <ul className = { classes.possibleValues }>
  117. <li>
  118. <b>{t('bandwidthSettings.leaveEmpty')}</b> {t('bandwidthSettings.leaveEmptyEffect')}
  119. </li>
  120. <li><b>0</b> {t('bandwidthSettings.zeroEffect')}</li>
  121. <li>
  122. <b>{t('bandwidthSettings.customValue')}</b> {t('bandwidthSettings.customValueEffect')}
  123. </li>
  124. </ul>
  125. </div>
  126. )}
  127. </div>
  128. </Dialog>
  129. );
  130. };
  131. export default BandwidthSettingsDialog;