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.

E2EESection.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* @flow */
  2. import React, { Component } from 'react';
  3. import type { Dispatch } from 'redux';
  4. import { createE2EEEvent, sendAnalytics } from '../../analytics';
  5. import { translate } from '../../base/i18n';
  6. import { Switch } from '../../base/react';
  7. import { connect } from '../../base/redux';
  8. import { toggleE2EE } from '../actions';
  9. import { MAX_MODE } from '../constants';
  10. import { doesEveryoneSupportE2EE } from '../functions';
  11. type Props = {
  12. /**
  13. * The resource for the description, computed based on the maxMode and whether the switch is toggled or not.
  14. */
  15. _descriptionResource: string,
  16. /**
  17. * Custom e2ee labels.
  18. */
  19. _e2eeLabels: Object,
  20. /**
  21. * Whether the switch is currently enabled or not.
  22. */
  23. _enabled: boolean,
  24. /**
  25. * Indicates whether all participants in the conference currently support E2EE.
  26. */
  27. _everyoneSupportE2EE: boolean,
  28. /**
  29. * Whether E2EE is currently enabled or not.
  30. */
  31. _toggled: boolean,
  32. /**
  33. * The redux {@code dispatch} function.
  34. */
  35. dispatch: Dispatch<any>,
  36. /**
  37. * Invoked to obtain translated strings.
  38. */
  39. t: Function
  40. };
  41. type State = {
  42. /**
  43. * True if the switch is toggled on.
  44. */
  45. toggled: boolean
  46. };
  47. /**
  48. * Implements a React {@code Component} for displaying a security dialog section with a field
  49. * for setting the E2EE key.
  50. *
  51. * @extends Component
  52. */
  53. class E2EESection extends Component<Props, State> {
  54. /**
  55. * Implements React's {@link Component#getDerivedStateFromProps()}.
  56. *
  57. * @inheritdoc
  58. */
  59. static getDerivedStateFromProps(props: Props, state: Object) {
  60. if (props._toggled !== state.toggled) {
  61. return {
  62. toggled: props._toggled
  63. };
  64. }
  65. return null;
  66. }
  67. /**
  68. * Instantiates a new component.
  69. *
  70. * @inheritdoc
  71. */
  72. constructor(props: Props) {
  73. super(props);
  74. this.state = {
  75. toggled: false
  76. };
  77. // Bind event handlers so they are only bound once for every instance.
  78. this._onToggle = this._onToggle.bind(this);
  79. }
  80. /**
  81. * Implements React's {@link Component#render()}.
  82. *
  83. * @inheritdoc
  84. * @returns {ReactElement}
  85. */
  86. render() {
  87. const { _descriptionResource, _enabled, _e2eeLabels, _everyoneSupportE2EE, t } = this.props;
  88. const { toggled } = this.state;
  89. const description = _e2eeLabels?.description || t(_descriptionResource);
  90. const label = _e2eeLabels?.label || t('dialog.e2eeLabel');
  91. const warning = _e2eeLabels?.warning || t('dialog.e2eeWarning');
  92. return (
  93. <div id = 'e2ee-section'>
  94. <p
  95. aria-live = 'polite'
  96. className = 'description'
  97. id = 'e2ee-section-description'>
  98. { description }
  99. { !_everyoneSupportE2EE && <br /> }
  100. { !_everyoneSupportE2EE && warning }
  101. </p>
  102. <div className = 'control-row'>
  103. <label htmlFor = 'e2ee-section-switch'>
  104. { label }
  105. </label>
  106. <Switch
  107. disabled = { !_enabled }
  108. id = 'e2ee-section-switch'
  109. onValueChange = { this._onToggle }
  110. value = { toggled } />
  111. </div>
  112. </div>
  113. );
  114. }
  115. _onToggle: () => void;
  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) {
  139. const { enabled: e2eeEnabled, maxMode } = state['features/e2ee'];
  140. const { e2eeLabels } = state['features/base/config'];
  141. let descriptionResource = '';
  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));