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 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* @flow */
  2. import React, { Component } from 'react';
  3. import type { Dispatch } from 'redux';
  4. import { createE2EEEvent, sendAnalytics } from '../../analytics';
  5. import { translate, translateToHTML } 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. * The current E2EE key.
  34. */
  35. key: string
  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. fieldRef: Object;
  45. /**
  46. * Initializes a new {@code E2EEDialog } instance.
  47. *
  48. * @param {Object} props - The read-only properties with which the new
  49. * instance is to be initialized.
  50. */
  51. constructor(props: Props) {
  52. super(props);
  53. this.fieldRef = React.createRef();
  54. this.state = {
  55. editing: false,
  56. key: this.props._key
  57. };
  58. // Bind event handlers so they are only bound once for every instance.
  59. this._onKeyChange = this._onKeyChange.bind(this);
  60. this._onSet = this._onSet.bind(this);
  61. this._onToggleSetKey = this._onToggleSetKey.bind(this);
  62. }
  63. /**
  64. * Implements React's {@link Component#render()}.
  65. *
  66. * @inheritdoc
  67. * @returns {ReactElement}
  68. */
  69. render() {
  70. const { _everyoneSupportsE2EE, t } = this.props;
  71. const { editing } = this.state;
  72. return (
  73. <div id = 'e2ee-section'>
  74. <p className = 'title'>
  75. { t('dialog.e2eeTitle') }
  76. </p>
  77. <p className = 'description'>
  78. { translateToHTML(t, 'dialog.e2eeDescription') }
  79. </p>
  80. {
  81. !_everyoneSupportsE2EE
  82. && <span className = 'warning'>
  83. { t('dialog.e2eeWarning') }
  84. </span>
  85. }
  86. <div className = 'key-field'>
  87. <label>
  88. { t('dialog.e2eeLabel') }:
  89. </label>
  90. <input
  91. disabled = { !editing }
  92. name = 'e2eeKey'
  93. onChange = { this._onKeyChange }
  94. placeholder = { t('dialog.e2eeNoKey') }
  95. ref = { this.fieldRef }
  96. type = 'password'
  97. value = { this.state.key } />
  98. { editing && <a onClick = { this._onSet }>
  99. { t('dialog.e2eeSet') }
  100. </a> }
  101. { !editing && <a onClick = { this._onToggleSetKey }>
  102. { t('dialog.e2eeToggleSet') }
  103. </a> }
  104. </div>
  105. </div>
  106. );
  107. }
  108. _onKeyChange: (Object) => void;
  109. /**
  110. * Updates the entered key.
  111. *
  112. * @param {Object} event - The DOM event triggered from the entered value having changed.
  113. * @private
  114. * @returns {void}
  115. */
  116. _onKeyChange(event) {
  117. this.setState({ key: event.target.value.trim() });
  118. }
  119. _onSet: () => void;
  120. /**
  121. * Dispatches an action to set/unset the E2EE key.
  122. *
  123. * @private
  124. * @returns {void}
  125. */
  126. _onSet() {
  127. const { key } = this.state;
  128. sendAnalytics(createE2EEEvent(`key.${key ? 'set' : 'unset'}`));
  129. this.props.dispatch(setE2EEKey(key));
  130. this.setState({
  131. editing: false
  132. });
  133. }
  134. _onToggleSetKey: () => void;
  135. /**
  136. * Sets the section into edit mode so then the user can set the key.
  137. *
  138. * @returns {void}
  139. */
  140. _onToggleSetKey() {
  141. this.setState({
  142. editing: true
  143. }, () => {
  144. this.fieldRef.current.focus();
  145. });
  146. }
  147. }
  148. /**
  149. * Maps (parts of) the Redux state to the associated props for this component.
  150. *
  151. * @param {Object} state - The Redux state.
  152. * @private
  153. * @returns {Props}
  154. */
  155. function mapStateToProps(state) {
  156. const { e2eeKey } = state['features/e2ee'];
  157. const participants = getParticipants(state).filter(p => !p.local);
  158. return {
  159. _everyoneSupportsE2EE: participants.every(p => Boolean(p.e2eeSupported)),
  160. _key: e2eeKey || ''
  161. };
  162. }
  163. export default translate(connect(mapStateToProps)(E2EESection));