Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. * Custom e2ee labels.
  13. */
  14. _e2eeLabels: Object,
  15. /**
  16. * Whether E2EE is currently enabled or not.
  17. */
  18. _enabled: boolean,
  19. /**
  20. * Indicates whether all participants in the conference currently support E2EE.
  21. */
  22. _everyoneSupportE2EE: boolean,
  23. /**
  24. * The redux {@code dispatch} function.
  25. */
  26. dispatch: Dispatch<any>,
  27. /**
  28. * Invoked to obtain translated strings.
  29. */
  30. t: Function
  31. };
  32. type State = {
  33. /**
  34. * True if the switch is toggled on.
  35. */
  36. enabled: 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. };
  68. // Bind event handlers so they are only bound once for every instance.
  69. this._onToggle = this._onToggle.bind(this);
  70. }
  71. /**
  72. * Implements React's {@link Component#render()}.
  73. *
  74. * @inheritdoc
  75. * @returns {ReactElement}
  76. */
  77. render() {
  78. const { _e2eeLabels, _everyoneSupportE2EE, t } = this.props;
  79. const { enabled } = this.state;
  80. const description = _e2eeLabels?.description || t('dialog.e2eeDescription');
  81. const label = _e2eeLabels?.label || t('dialog.e2eeLabel');
  82. const warning = _e2eeLabels?.warning || t('dialog.e2eeWarning');
  83. return (
  84. <div id = 'e2ee-section'>
  85. <p
  86. aria-live = 'polite'
  87. className = 'description'
  88. id = 'e2ee-section-description'>
  89. { description }
  90. { !_everyoneSupportE2EE && <br /> }
  91. { !_everyoneSupportE2EE && warning }
  92. </p>
  93. <div className = 'control-row'>
  94. <label htmlFor = 'e2ee-section-switch'>
  95. { label }
  96. </label>
  97. <Switch
  98. id = 'e2ee-section-switch'
  99. onValueChange = { this._onToggle }
  100. value = { enabled } />
  101. </div>
  102. </div>
  103. );
  104. }
  105. _onToggle: () => void;
  106. /**
  107. * Callback to be invoked when the user toggles E2EE on or off.
  108. *
  109. * @private
  110. * @returns {void}
  111. */
  112. _onToggle() {
  113. const newValue = !this.state.enabled;
  114. this.setState({
  115. enabled: newValue
  116. });
  117. sendAnalytics(createE2EEEvent(`enabled.${String(newValue)}`));
  118. this.props.dispatch(toggleE2EE(newValue));
  119. }
  120. }
  121. /**
  122. * Maps (parts of) the Redux state to the associated props for this component.
  123. *
  124. * @param {Object} state - The Redux state.
  125. * @private
  126. * @returns {Props}
  127. */
  128. function mapStateToProps(state) {
  129. const { enabled } = state['features/e2ee'];
  130. const { e2eeLabels } = state['features/base/config'];
  131. return {
  132. _e2eeLabels: e2eeLabels,
  133. _enabled: enabled,
  134. _everyoneSupportE2EE: doesEveryoneSupportE2EE(state)
  135. };
  136. }
  137. export default translate(connect(mapStateToProps)(E2EESection));