Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

E2EESection.tsx 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* eslint-disable lines-around-comment */
  2. import React, { Component } from 'react';
  3. import { WithTranslation } from 'react-i18next';
  4. import type { Dispatch } from 'redux';
  5. import { createE2EEEvent } from '../../analytics/AnalyticsEvents';
  6. import { sendAnalytics } from '../../analytics/functions';
  7. import { IState } from '../../app/types';
  8. import { translate } from '../../base/i18n/functions';
  9. import { connect } from '../../base/redux/functions';
  10. import Switch from '../../base/ui/components/web/Switch';
  11. // @ts-ignore
  12. import { toggleE2EE } from '../actions';
  13. import { MAX_MODE } from '../constants';
  14. // @ts-ignore
  15. import { doesEveryoneSupportE2EE } from '../functions';
  16. interface Props extends WithTranslation {
  17. /**
  18. * The resource for the description, computed based on the maxMode and whether the switch is toggled or not.
  19. */
  20. _descriptionResource: string;
  21. /**
  22. * Custom e2ee labels.
  23. */
  24. _e2eeLabels: any;
  25. /**
  26. * Whether the switch is currently enabled or not.
  27. */
  28. _enabled: boolean;
  29. /**
  30. * Indicates whether all participants in the conference currently support E2EE.
  31. */
  32. _everyoneSupportE2EE: boolean;
  33. /**
  34. * Whether E2EE is currently enabled or not.
  35. */
  36. _toggled: boolean;
  37. /**
  38. * The redux {@code dispatch} function.
  39. */
  40. dispatch: Dispatch<any>;
  41. }
  42. type State = {
  43. /**
  44. * True if the switch is toggled on.
  45. */
  46. toggled: boolean;
  47. };
  48. /**
  49. * Implements a React {@code Component} for displaying a security dialog section with a field
  50. * for setting the E2EE key.
  51. *
  52. * @augments Component
  53. */
  54. class E2EESection extends Component<Props, State> {
  55. /**
  56. * Implements React's {@link Component#getDerivedStateFromProps()}.
  57. *
  58. * @inheritdoc
  59. */
  60. static getDerivedStateFromProps(props: Props, state: State) {
  61. if (props._toggled !== state.toggled) {
  62. return {
  63. toggled: props._toggled
  64. };
  65. }
  66. return null;
  67. }
  68. /**
  69. * Instantiates a new component.
  70. *
  71. * @inheritdoc
  72. */
  73. constructor(props: Props) {
  74. super(props);
  75. this.state = {
  76. toggled: false
  77. };
  78. // Bind event handlers so they are only bound once for every instance.
  79. this._onToggle = this._onToggle.bind(this);
  80. }
  81. /**
  82. * Implements React's {@link Component#render()}.
  83. *
  84. * @inheritdoc
  85. * @returns {ReactElement}
  86. */
  87. render() {
  88. const { _descriptionResource, _enabled, _e2eeLabels, _everyoneSupportE2EE, t } = this.props;
  89. const { toggled } = this.state;
  90. const description = _e2eeLabels?.description || t(_descriptionResource);
  91. const label = _e2eeLabels?.label || t('dialog.e2eeLabel');
  92. const warning = _e2eeLabels?.warning || t('dialog.e2eeWarning');
  93. return (
  94. <div id = 'e2ee-section'>
  95. <p
  96. aria-live = 'polite'
  97. className = 'description'
  98. id = 'e2ee-section-description'>
  99. { description }
  100. { !_everyoneSupportE2EE && <br /> }
  101. { !_everyoneSupportE2EE && warning }
  102. </p>
  103. <div className = 'control-row'>
  104. <label htmlFor = 'e2ee-section-switch'>
  105. { label }
  106. </label>
  107. <Switch
  108. checked = { toggled }
  109. disabled = { !_enabled }
  110. id = 'e2ee-section-switch'
  111. onChange = { this._onToggle } />
  112. </div>
  113. </div>
  114. );
  115. }
  116. /**
  117. * Callback to be invoked when the user toggles E2EE on or off.
  118. *
  119. * @private
  120. * @returns {void}
  121. */
  122. _onToggle() {
  123. const newValue = !this.state.toggled;
  124. this.setState({
  125. toggled: newValue
  126. });
  127. sendAnalytics(createE2EEEvent(`enabled.${String(newValue)}`));
  128. this.props.dispatch(toggleE2EE(newValue));
  129. }
  130. }
  131. /**
  132. * Maps (parts of) the Redux state to the associated props for this component.
  133. *
  134. * @param {Object} state - The Redux state.
  135. * @private
  136. * @returns {Props}
  137. */
  138. function mapStateToProps(state: IState) {
  139. const { enabled: e2eeEnabled, maxMode } = state['features/e2ee'];
  140. const { e2eeLabels } = state['features/base/config'];
  141. let descriptionResource: string | undefined = '';
  142. if (e2eeLabels) {
  143. // When e2eeLabels are present, the descriptionResouse is ignored.
  144. descriptionResource = undefined;
  145. } else if (maxMode === MAX_MODE.THRESHOLD_EXCEEDED) {
  146. descriptionResource = 'dialog.e2eeDisabledDueToMaxModeDescription';
  147. } else if (maxMode === MAX_MODE.ENABLED) {
  148. descriptionResource = e2eeEnabled
  149. ? 'dialog.e2eeWillDisableDueToMaxModeDescription' : 'dialog.e2eeDisabledDueToMaxModeDescription';
  150. } else {
  151. descriptionResource = 'dialog.e2eeDescription';
  152. }
  153. return {
  154. _descriptionResource: descriptionResource,
  155. _e2eeLabels: e2eeLabels,
  156. _enabled: maxMode === MAX_MODE.DISABLED || e2eeEnabled,
  157. _toggled: e2eeEnabled,
  158. _everyoneSupportE2EE: doesEveryoneSupportE2EE(state)
  159. };
  160. }
  161. export default translate(connect(mapStateToProps)(E2EESection));