您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

E2EESection.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 label.
  13. */
  14. _e2eeLabel: string,
  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 { _e2eeLabel, _everyoneSupportE2EE, t } = this.props;
  79. const { enabled } = this.state;
  80. let description;
  81. let label;
  82. let warning;
  83. if (_e2eeLabel) {
  84. description = t('dialog.e2eeDescriptionCustom', { label: _e2eeLabel });
  85. label = t('dialog.e2eeLabelCustom', { label: _e2eeLabel });
  86. warning = t('dialog.e2eeWarningCustom', { label: _e2eeLabel });
  87. } else {
  88. description = t('dialog.e2eeDescription');
  89. label = t('dialog.e2eeLabel');
  90. warning = t('dialog.e2eeWarning');
  91. }
  92. return (
  93. <div id = 'e2ee-section'>
  94. <p
  95. aria-live = 'polite'
  96. className = 'description'
  97. id = 'e2ee-section-description'>
  98. { description }
  99. { !_everyoneSupportE2EE && <br /> }
  100. { !_everyoneSupportE2EE && warning }
  101. </p>
  102. <div className = 'control-row'>
  103. <label htmlFor = 'e2ee-section-switch'>
  104. { label }
  105. </label>
  106. <Switch
  107. id = 'e2ee-section-switch'
  108. onValueChange = { this._onToggle }
  109. value = { enabled } />
  110. </div>
  111. </div>
  112. );
  113. }
  114. _onToggle: () => void;
  115. /**
  116. * Callback to be invoked when the user toggles E2EE on or off.
  117. *
  118. * @private
  119. * @returns {void}
  120. */
  121. _onToggle() {
  122. const newValue = !this.state.enabled;
  123. this.setState({
  124. enabled: newValue
  125. });
  126. sendAnalytics(createE2EEEvent(`enabled.${String(newValue)}`));
  127. this.props.dispatch(toggleE2EE(newValue));
  128. }
  129. }
  130. /**
  131. * Maps (parts of) the Redux state to the associated props for this component.
  132. *
  133. * @param {Object} state - The Redux state.
  134. * @private
  135. * @returns {Props}
  136. */
  137. function mapStateToProps(state) {
  138. const { enabled } = state['features/e2ee'];
  139. const { e2eeLabel } = state['features/base/config'];
  140. return {
  141. _e2eeLabel: e2eeLabel,
  142. _enabled: enabled,
  143. _everyoneSupportE2EE: doesEveryoneSupportE2EE(state)
  144. };
  145. }
  146. export default translate(connect(mapStateToProps)(E2EESection));