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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. type Props = {
  10. /**
  11. * Whether E2EE is currently enabled or not.
  12. */
  13. _enabled: boolean,
  14. /**
  15. * Indicates whether all participants in the conference currently support E2EE.
  16. */
  17. _everyoneSupportE2EE: boolean,
  18. /**
  19. * The redux {@code dispatch} function.
  20. */
  21. dispatch: Dispatch<any>,
  22. /**
  23. * Invoked to obtain translated strings.
  24. */
  25. t: Function
  26. };
  27. type State = {
  28. /**
  29. * True if the switch is toggled on.
  30. */
  31. enabled: boolean,
  32. /**
  33. * True if the section description should be expanded, false otherwise.
  34. */
  35. expand: boolean
  36. };
  37. /**
  38. * Implements a React {@code Component} for displaying a security dialog section with a field
  39. * for setting the E2EE key.
  40. *
  41. * @extends Component
  42. */
  43. class E2EESection extends Component<Props, State> {
  44. /**
  45. * Implements React's {@link Component#getDerivedStateFromProps()}.
  46. *
  47. * @inheritdoc
  48. */
  49. static getDerivedStateFromProps(props: Props, state: Object) {
  50. if (props._enabled !== state.enabled) {
  51. return {
  52. enabled: props._enabled
  53. };
  54. }
  55. return null;
  56. }
  57. /**
  58. * Instantiates a new component.
  59. *
  60. * @inheritdoc
  61. */
  62. constructor(props: Props) {
  63. super(props);
  64. this.state = {
  65. enabled: false,
  66. expand: false
  67. };
  68. // Bind event handlers so they are only bound once for every instance.
  69. this._onExpand = this._onExpand.bind(this);
  70. this._onExpandKeyPress = this._onExpandKeyPress.bind(this);
  71. this._onToggle = this._onToggle.bind(this);
  72. }
  73. /**
  74. * Implements React's {@link Component#render()}.
  75. *
  76. * @inheritdoc
  77. * @returns {ReactElement}
  78. */
  79. render() {
  80. const { _everyoneSupportE2EE, t } = this.props;
  81. const { enabled, expand } = this.state;
  82. const description = t('dialog.e2eeDescription');
  83. return (
  84. <div id = 'e2ee-section'>
  85. <p
  86. aria-live = 'polite'
  87. className = 'description'
  88. id = 'e2ee-section-description'>
  89. { expand && description }
  90. { !expand && description.substring(0, 100) }
  91. { !expand && <span
  92. aria-controls = 'e2ee-section-description'
  93. aria-expanded = { expand }
  94. className = 'read-more'
  95. onClick = { this._onExpand }
  96. onKeyPress = { this._onExpandKeyPress }
  97. role = 'button'
  98. tabIndex = { 0 }>
  99. ... { t('dialog.readMore') }
  100. </span> }
  101. </p>
  102. {
  103. !_everyoneSupportE2EE
  104. && <span className = 'warning'>
  105. { t('dialog.e2eeWarning') }
  106. </span>
  107. }
  108. <div className = 'control-row'>
  109. <label htmlFor = 'e2ee-section-switch'>
  110. { t('dialog.e2eeLabel') }
  111. </label>
  112. <Switch
  113. id = 'e2ee-section-switch'
  114. onValueChange = { this._onToggle }
  115. value = { enabled } />
  116. </div>
  117. </div>
  118. );
  119. }
  120. _onExpand: () => void;
  121. /**
  122. * Callback to be invoked when the description is expanded.
  123. *
  124. * @returns {void}
  125. */
  126. _onExpand() {
  127. this.setState({
  128. expand: true
  129. });
  130. }
  131. _onExpandKeyPress: (Object) => void;
  132. /**
  133. * KeyPress handler for accessibility.
  134. *
  135. * @param {Object} e - The key event to handle.
  136. *
  137. * @returns {void}
  138. */
  139. _onExpandKeyPress(e) {
  140. if (e.key === ' ' || e.key === 'Enter') {
  141. e.preventDefault();
  142. this._onExpand();
  143. }
  144. }
  145. _onToggle: () => void;
  146. /**
  147. * Callback to be invoked when the user toggles E2EE on or off.
  148. *
  149. * @private
  150. * @returns {void}
  151. */
  152. _onToggle() {
  153. const newValue = !this.state.enabled;
  154. this.setState({
  155. enabled: newValue
  156. });
  157. sendAnalytics(createE2EEEvent(`enabled.${String(newValue)}`));
  158. this.props.dispatch(toggleE2EE(newValue));
  159. }
  160. }
  161. /**
  162. * Maps (parts of) the Redux state to the associated props for this component.
  163. *
  164. * @param {Object} state - The Redux state.
  165. * @private
  166. * @returns {Props}
  167. */
  168. function mapStateToProps(state) {
  169. const { enabled, everyoneSupportE2EE } = state['features/e2ee'];
  170. return {
  171. _enabled: enabled,
  172. _everyoneSupportE2EE: everyoneSupportE2EE
  173. };
  174. }
  175. export default translate(connect(mapStateToProps)(E2EESection));