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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. import { doesEveryoneSupportE2EE } from '../functions';
  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. _everyoneSupportE2EE: 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. /**
  35. * Implements a React {@code Component} for displaying a security dialog section with a field
  36. * for setting the E2EE key.
  37. *
  38. * @extends Component
  39. */
  40. class E2EESection extends Component<Props, State> {
  41. /**
  42. * Implements React's {@link Component#getDerivedStateFromProps()}.
  43. *
  44. * @inheritdoc
  45. */
  46. static getDerivedStateFromProps(props: Props, state: Object) {
  47. if (props._enabled !== state.enabled) {
  48. return {
  49. enabled: props._enabled
  50. };
  51. }
  52. return null;
  53. }
  54. /**
  55. * Instantiates a new component.
  56. *
  57. * @inheritdoc
  58. */
  59. constructor(props: Props) {
  60. super(props);
  61. this.state = {
  62. enabled: false
  63. };
  64. // Bind event handlers so they are only bound once for every instance.
  65. this._onToggle = this._onToggle.bind(this);
  66. }
  67. /**
  68. * Implements React's {@link Component#render()}.
  69. *
  70. * @inheritdoc
  71. * @returns {ReactElement}
  72. */
  73. render() {
  74. const { _everyoneSupportE2EE, t } = this.props;
  75. const { enabled } = this.state;
  76. const description = t('dialog.e2eeDescription');
  77. return (
  78. <div id = 'e2ee-section'>
  79. <p
  80. aria-live = 'polite'
  81. className = 'description'
  82. id = 'e2ee-section-description'>
  83. { description }
  84. { !_everyoneSupportE2EE && <br /> }
  85. { !_everyoneSupportE2EE && t('dialog.e2eeWarning') }
  86. </p>
  87. <div className = 'control-row'>
  88. <label htmlFor = 'e2ee-section-switch'>
  89. { t('dialog.e2eeLabel') }
  90. </label>
  91. <Switch
  92. id = 'e2ee-section-switch'
  93. onValueChange = { this._onToggle }
  94. value = { enabled } />
  95. </div>
  96. </div>
  97. );
  98. }
  99. _onToggle: () => void;
  100. /**
  101. * Callback to be invoked when the user toggles E2EE on or off.
  102. *
  103. * @private
  104. * @returns {void}
  105. */
  106. _onToggle() {
  107. const newValue = !this.state.enabled;
  108. this.setState({
  109. enabled: newValue
  110. });
  111. sendAnalytics(createE2EEEvent(`enabled.${String(newValue)}`));
  112. this.props.dispatch(toggleE2EE(newValue));
  113. }
  114. }
  115. /**
  116. * Maps (parts of) the Redux state to the associated props for this component.
  117. *
  118. * @param {Object} state - The Redux state.
  119. * @private
  120. * @returns {Props}
  121. */
  122. function mapStateToProps(state) {
  123. const { enabled } = state['features/e2ee'];
  124. return {
  125. _enabled: enabled,
  126. _everyoneSupportE2EE: doesEveryoneSupportE2EE(state)
  127. };
  128. }
  129. export default translate(connect(mapStateToProps)(E2EESection));