Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

E2EESection.tsx 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import React, { useCallback, useEffect, useState } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { connect } from 'react-redux';
  4. import { makeStyles } from 'tss-react/mui';
  5. import { createE2EEEvent } from '../../analytics/AnalyticsEvents';
  6. import { sendAnalytics } from '../../analytics/functions';
  7. import { IReduxState, IStore } from '../../app/types';
  8. import Switch from '../../base/ui/components/web/Switch';
  9. import { toggleE2EE } from '../actions';
  10. import { MAX_MODE } from '../constants';
  11. import { doesEveryoneSupportE2EE } from '../functions';
  12. interface IProps {
  13. /**
  14. * The resource for the description, computed based on the maxMode and whether the switch is toggled or not.
  15. */
  16. _descriptionResource?: string;
  17. /**
  18. * Custom e2ee labels.
  19. */
  20. _e2eeLabels: any;
  21. /**
  22. * Whether the switch is currently enabled or not.
  23. */
  24. _enabled: boolean;
  25. /**
  26. * Indicates whether all participants in the conference currently support E2EE.
  27. */
  28. _everyoneSupportE2EE: boolean;
  29. /**
  30. * Whether E2EE is currently enabled or not.
  31. */
  32. _toggled: boolean;
  33. /**
  34. * The redux {@code dispatch} function.
  35. */
  36. dispatch: IStore['dispatch'];
  37. }
  38. const useStyles = makeStyles()(() => {
  39. return {
  40. e2eeSection: {
  41. display: 'flex',
  42. flexDirection: 'column'
  43. },
  44. description: {
  45. fontSize: '13px',
  46. margin: '15px 0'
  47. },
  48. controlRow: {
  49. display: 'flex',
  50. justifyContent: 'space-between',
  51. marginTop: '15px',
  52. '& label': {
  53. fontSize: '14px',
  54. fontWeight: 'bold'
  55. }
  56. }
  57. };
  58. });
  59. /**
  60. * Implements a React {@code Component} for displaying a security dialog section with a field
  61. * for setting the E2EE key.
  62. *
  63. * @param {IProps} props - Component's props.
  64. * @returns {JSX}
  65. */
  66. const E2EESection = ({
  67. _descriptionResource,
  68. _enabled,
  69. _e2eeLabels,
  70. _everyoneSupportE2EE,
  71. _toggled,
  72. dispatch
  73. }: IProps) => {
  74. const { classes } = useStyles();
  75. const { t } = useTranslation();
  76. const [ toggled, setToggled ] = useState(_toggled ?? false);
  77. useEffect(() => {
  78. setToggled(_toggled);
  79. }, [ _toggled ]);
  80. /**
  81. * Callback to be invoked when the user toggles E2EE on or off.
  82. *
  83. * @private
  84. * @returns {void}
  85. */
  86. const _onToggle = useCallback(() => {
  87. const newValue = !toggled;
  88. setToggled(newValue);
  89. sendAnalytics(createE2EEEvent(`enabled.${String(newValue)}`));
  90. dispatch(toggleE2EE(newValue));
  91. }, [ toggled ]);
  92. const description = _e2eeLabels?.description || t(_descriptionResource ?? '');
  93. const label = _e2eeLabels?.label || t('dialog.e2eeLabel');
  94. const warning = _e2eeLabels?.warning || t('dialog.e2eeWarning');
  95. return (
  96. <div
  97. className = { classes.e2eeSection }
  98. id = 'e2ee-section'>
  99. <p
  100. aria-live = 'polite'
  101. className = { classes.description }
  102. id = 'e2ee-section-description'>
  103. {description}
  104. {!_everyoneSupportE2EE && <br />}
  105. {!_everyoneSupportE2EE && warning}
  106. </p>
  107. <div className = { classes.controlRow }>
  108. <label htmlFor = 'e2ee-section-switch'>
  109. {label}
  110. </label>
  111. <Switch
  112. checked = { toggled }
  113. disabled = { !_enabled }
  114. id = 'e2ee-section-switch'
  115. onChange = { _onToggle } />
  116. </div>
  117. </div>
  118. );
  119. };
  120. /**
  121. * Maps (parts of) the Redux state to the associated props for this component.
  122. *
  123. * @param {Object} state - The Redux state.
  124. * @private
  125. * @returns {IProps}
  126. */
  127. function mapStateToProps(state: IReduxState) {
  128. const { enabled: e2eeEnabled, maxMode } = state['features/e2ee'];
  129. const { e2ee = {} } = state['features/base/config'];
  130. let descriptionResource: string | undefined = '';
  131. if (e2ee.labels) {
  132. // When e2eeLabels are present, the description resource is ignored.
  133. descriptionResource = undefined;
  134. } else if (maxMode === MAX_MODE.THRESHOLD_EXCEEDED) {
  135. descriptionResource = 'dialog.e2eeDisabledDueToMaxModeDescription';
  136. } else if (maxMode === MAX_MODE.ENABLED) {
  137. descriptionResource = e2eeEnabled
  138. ? 'dialog.e2eeWillDisableDueToMaxModeDescription' : 'dialog.e2eeDisabledDueToMaxModeDescription';
  139. } else {
  140. descriptionResource = 'dialog.e2eeDescription';
  141. }
  142. return {
  143. _descriptionResource: descriptionResource,
  144. _e2eeLabels: e2ee.labels,
  145. _enabled: maxMode === MAX_MODE.DISABLED || e2eeEnabled,
  146. _toggled: e2eeEnabled,
  147. _everyoneSupportE2EE: Boolean(doesEveryoneSupportE2EE(state))
  148. };
  149. }
  150. export default connect(mapStateToProps)(E2EESection);