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

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