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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 { connect } from '../../base/redux';
  8. import { setE2EEKey } from '../actions';
  9. type Props = {
  10. /**
  11. * Indicates whether all participants in the conference currently support E2EE.
  12. */
  13. _everyoneSupportsE2EE: boolean,
  14. /**
  15. * The current E2EE key.
  16. */
  17. _key: string,
  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 key is being edited.
  30. */
  31. editing: boolean,
  32. /**
  33. * True if the section description should be expanded, false otherwise.
  34. */
  35. expand: boolean,
  36. /**
  37. * The current E2EE key.
  38. */
  39. key: string
  40. };
  41. /**
  42. * Implements a React {@code Component} for displaying a security dialog section with a field
  43. * for setting the E2EE key.
  44. *
  45. * @extends Component
  46. */
  47. class E2EESection extends Component<Props, State> {
  48. fieldRef: Object;
  49. /**
  50. * Initializes a new {@code E2EEDialog } instance.
  51. *
  52. * @param {Object} props - The read-only properties with which the new
  53. * instance is to be initialized.
  54. */
  55. constructor(props: Props) {
  56. super(props);
  57. this.fieldRef = React.createRef();
  58. this.state = {
  59. editing: false,
  60. expand: false,
  61. key: this.props._key
  62. };
  63. // Bind event handlers so they are only bound once for every instance.
  64. this._onExpand = this._onExpand.bind(this);
  65. this._onKeyChange = this._onKeyChange.bind(this);
  66. this._onSet = this._onSet.bind(this);
  67. this._onToggleSetKey = this._onToggleSetKey.bind(this);
  68. }
  69. /**
  70. * Implements React's {@link Component#render()}.
  71. *
  72. * @inheritdoc
  73. * @returns {ReactElement}
  74. */
  75. render() {
  76. const { _everyoneSupportsE2EE, t } = this.props;
  77. const { editing, expand } = this.state;
  78. const description = t('dialog.e2eeDescription');
  79. return (
  80. <div id = 'e2ee-section'>
  81. <p className = 'description'>
  82. { expand && description }
  83. { !expand && description.substring(0, 100) }
  84. { !expand && <span
  85. className = 'read-more'
  86. onClick = { this._onExpand }>
  87. ... { t('dialog.readMore') }
  88. </span> }
  89. </p>
  90. {
  91. !_everyoneSupportsE2EE
  92. && <span className = 'warning'>
  93. { t('dialog.e2eeWarning') }
  94. </span>
  95. }
  96. <div className = 'key-field'>
  97. <label>
  98. { t('dialog.e2eeLabel') }:
  99. </label>
  100. <input
  101. disabled = { !editing }
  102. name = 'e2eeKey'
  103. onChange = { this._onKeyChange }
  104. onKeyDown = { this._onKeyDown }
  105. placeholder = { t('dialog.e2eeNoKey') }
  106. ref = { this.fieldRef }
  107. type = 'password'
  108. value = { this.state.key } />
  109. { editing && <a onClick = { this._onSet }>
  110. { t('dialog.e2eeSet') }
  111. </a> }
  112. { !editing && <a onClick = { this._onToggleSetKey }>
  113. { t('dialog.e2eeToggleSet') }
  114. </a> }
  115. </div>
  116. </div>
  117. );
  118. }
  119. _onExpand: () => void;
  120. /**
  121. * Callback to be invoked when the description is expanded.
  122. *
  123. * @returns {void}
  124. */
  125. _onExpand() {
  126. this.setState({
  127. expand: true
  128. });
  129. }
  130. _onKeyChange: (Object) => void;
  131. /**
  132. * Updates the entered key.
  133. *
  134. * @param {Object} event - The DOM event triggered from the entered value having changed.
  135. * @private
  136. * @returns {void}
  137. */
  138. _onKeyChange(event) {
  139. this.setState({ key: event.target.value.trim() });
  140. }
  141. _onKeyDown: (Object) => void;
  142. /**
  143. * Handler for the keydown event on the form, preventing the closing of the dialog.
  144. *
  145. * @param {Object} event - The DOM event triggered by keydown events.
  146. * @returns {void}
  147. */
  148. _onKeyDown(event) {
  149. if (event.key === 'Enter') {
  150. event.preventDefault();
  151. }
  152. }
  153. _onSet: () => void;
  154. /**
  155. * Dispatches an action to set/unset the E2EE key.
  156. *
  157. * @private
  158. * @returns {void}
  159. */
  160. _onSet() {
  161. const { key } = this.state;
  162. sendAnalytics(createE2EEEvent(`key.${key ? 'set' : 'unset'}`));
  163. this.props.dispatch(setE2EEKey(key));
  164. this.setState({
  165. editing: false
  166. });
  167. }
  168. _onToggleSetKey: () => void;
  169. /**
  170. * Sets the section into edit mode so then the user can set the key.
  171. *
  172. * @returns {void}
  173. */
  174. _onToggleSetKey() {
  175. this.setState({
  176. editing: true
  177. }, () => {
  178. this.fieldRef.current.focus();
  179. });
  180. }
  181. }
  182. /**
  183. * Maps (parts of) the Redux state to the associated props for this component.
  184. *
  185. * @param {Object} state - The Redux state.
  186. * @private
  187. * @returns {Props}
  188. */
  189. function mapStateToProps(state) {
  190. const { e2eeKey } = state['features/e2ee'];
  191. const participants = getParticipants(state).filter(p => !p.local);
  192. return {
  193. _everyoneSupportsE2EE: participants.every(p => Boolean(p.e2eeSupported)),
  194. _key: e2eeKey || ''
  195. };
  196. }
  197. export default translate(connect(mapStateToProps)(E2EESection));