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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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._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 { _everyoneSupportsE2EE, t } = this.props;
  81. const { enabled, expand } = this.state;
  82. const description = t('dialog.e2eeDescription');
  83. return (
  84. <div id = 'e2ee-section'>
  85. <p className = 'description'>
  86. { expand && description }
  87. { !expand && description.substring(0, 100) }
  88. { !expand && <span
  89. className = 'read-more'
  90. onClick = { this._onExpand }>
  91. ... { t('dialog.readMore') }
  92. </span> }
  93. </p>
  94. {
  95. !_everyoneSupportsE2EE
  96. && <span className = 'warning'>
  97. { t('dialog.e2eeWarning') }
  98. </span>
  99. }
  100. <div className = 'control-row'>
  101. <label>
  102. { t('dialog.e2eeLabel') }
  103. </label>
  104. <Switch
  105. onValueChange = { this._onToggle }
  106. value = { enabled } />
  107. </div>
  108. </div>
  109. );
  110. }
  111. _onExpand: () => void;
  112. /**
  113. * Callback to be invoked when the description is expanded.
  114. *
  115. * @returns {void}
  116. */
  117. _onExpand() {
  118. this.setState({
  119. expand: true
  120. });
  121. }
  122. _onToggle: () => void;
  123. /**
  124. * Callback to be invoked when the user toggles E2EE on or off.
  125. *
  126. * @private
  127. * @returns {void}
  128. */
  129. _onToggle() {
  130. const newValue = !this.state.enabled;
  131. this.setState({
  132. enabled: newValue
  133. });
  134. sendAnalytics(createE2EEEvent(`enabled.${String(newValue)}`));
  135. this.props.dispatch(toggleE2EE(newValue));
  136. }
  137. }
  138. /**
  139. * Maps (parts of) the Redux state to the associated props for this component.
  140. *
  141. * @param {Object} state - The Redux state.
  142. * @private
  143. * @returns {Props}
  144. */
  145. function mapStateToProps(state) {
  146. const { enabled } = state['features/e2ee'];
  147. const participants = getParticipants(state).filter(p => !p.local);
  148. return {
  149. _enabled: enabled,
  150. _everyoneSupportsE2EE: participants.every(p => Boolean(p.e2eeSupported))
  151. };
  152. }
  153. export default translate(connect(mapStateToProps)(E2EESection));