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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. };
  67. // Bind event handlers so they are only bound once for every instance.
  68. this._onToggle = this._onToggle.bind(this);
  69. }
  70. /**
  71. * Implements React's {@link Component#render()}.
  72. *
  73. * @inheritdoc
  74. * @returns {ReactElement}
  75. */
  76. render() {
  77. const { _everyoneSupportE2EE, t } = this.props;
  78. const { enabled } = this.state;
  79. const description = t('dialog.e2eeDescription');
  80. return (
  81. <div id = 'e2ee-section'>
  82. <p
  83. aria-live = 'polite'
  84. className = 'description'
  85. id = 'e2ee-section-description'>
  86. { description }
  87. { !_everyoneSupportE2EE && <br /> }
  88. { !_everyoneSupportE2EE && t('dialog.e2eeWarning') }
  89. </p>
  90. <div className = 'control-row'>
  91. <label htmlFor = 'e2ee-section-switch'>
  92. { t('dialog.e2eeLabel') }
  93. </label>
  94. <Switch
  95. id = 'e2ee-section-switch'
  96. onValueChange = { this._onToggle }
  97. value = { enabled } />
  98. </div>
  99. </div>
  100. );
  101. }
  102. _onToggle: () => void;
  103. /**
  104. * Callback to be invoked when the user toggles E2EE on or off.
  105. *
  106. * @private
  107. * @returns {void}
  108. */
  109. _onToggle() {
  110. const newValue = !this.state.enabled;
  111. this.setState({
  112. enabled: newValue
  113. });
  114. sendAnalytics(createE2EEEvent(`enabled.${String(newValue)}`));
  115. this.props.dispatch(toggleE2EE(newValue));
  116. }
  117. }
  118. /**
  119. * Maps (parts of) the Redux state to the associated props for this component.
  120. *
  121. * @param {Object} state - The Redux state.
  122. * @private
  123. * @returns {Props}
  124. */
  125. function mapStateToProps(state) {
  126. const { enabled, everyoneSupportE2EE } = state['features/e2ee'];
  127. return {
  128. _enabled: enabled,
  129. _everyoneSupportE2EE: everyoneSupportE2EE
  130. };
  131. }
  132. export default translate(connect(mapStateToProps)(E2EESection));