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.

E2EEDialog.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* @flow */
  2. import { FieldTextStateless as TextField } from '@atlaskit/field-text';
  3. import React, { Component } from 'react';
  4. import type { Dispatch } from 'redux';
  5. import { createE2EEEvent, sendAnalytics } from '../../analytics';
  6. import { Dialog } from '../../base/dialog';
  7. import { translate, translateToHTML } from '../../base/i18n';
  8. import { getParticipants } from '../../base/participants';
  9. import { connect } from '../../base/redux';
  10. import { setE2EEKey } from '../actions';
  11. type Props = {
  12. /**
  13. * Indicates whether all participants in the conference currently support E2EE.
  14. */
  15. _everyoneSupportsE2EE: boolean,
  16. /**
  17. * The current E2EE key.
  18. */
  19. _key: string,
  20. /**
  21. * The redux {@code dispatch} function.
  22. */
  23. dispatch: Dispatch<any>,
  24. /**
  25. * Invoked to obtain translated strings.
  26. */
  27. t: Function
  28. };
  29. type State = {
  30. /**
  31. * The current E2EE key.
  32. */
  33. key: string
  34. };
  35. /**
  36. * Implements a React {@code Component} for displaying a dialog with a field
  37. * for setting the E2EE key.
  38. *
  39. * @extends Component
  40. */
  41. class E2EEDialog extends Component<Props, State> {
  42. /**
  43. * Initializes a new {@code E2EEDialog } instance.
  44. *
  45. * @param {Object} props - The read-only properties with which the new
  46. * instance is to be initialized.
  47. */
  48. constructor(props: Props) {
  49. super(props);
  50. this.state = {
  51. key: this.props._key
  52. };
  53. // Bind event handlers so they are only bound once for every instance.
  54. this._onKeyChange = this._onKeyChange.bind(this);
  55. this._onSubmit = this._onSubmit.bind(this);
  56. }
  57. /**
  58. * Implements React's {@link Component#render()}.
  59. *
  60. * @inheritdoc
  61. * @returns {ReactElement}
  62. */
  63. render() {
  64. const { _everyoneSupportsE2EE, t } = this.props;
  65. return (
  66. <Dialog
  67. isModal = { false }
  68. onSubmit = { this._onSubmit }
  69. titleKey = 'dialog.e2eeTitle'
  70. width = 'small'>
  71. <div className = 'e2ee-destription'>
  72. { translateToHTML(t, 'dialog.e2eeDescription') }
  73. </div>
  74. {
  75. !_everyoneSupportsE2EE
  76. && <div className = 'e2ee-warn'>
  77. { translateToHTML(t, 'dialog.e2eeWarning') }
  78. </div>
  79. }
  80. <TextField
  81. autoFocus = { true }
  82. compact = { true }
  83. label = { t('dialog.e2eeLabel') }
  84. name = 'e2eeKey'
  85. onChange = { this._onKeyChange }
  86. shouldFitContainer = { true }
  87. type = 'password'
  88. value = { this.state.key } />
  89. </Dialog>);
  90. }
  91. _onKeyChange: (Object) => void;
  92. /**
  93. * Updates the entered key.
  94. *
  95. * @param {Object} event - The DOM event triggered from the entered value having changed.
  96. * @private
  97. * @returns {void}
  98. */
  99. _onKeyChange(event) {
  100. this.setState({ key: event.target.value.trim() });
  101. }
  102. _onSubmit: () => boolean;
  103. /**
  104. * Dispatches an action to update the E2EE key.
  105. *
  106. * @private
  107. * @returns {boolean}
  108. */
  109. _onSubmit() {
  110. const { key } = this.state;
  111. sendAnalytics(createE2EEEvent(`key.${key ? 'set' : 'unset'}`));
  112. this.props.dispatch(setE2EEKey(key));
  113. return true;
  114. }
  115. }
  116. /**
  117. * Maps (parts of) the Redux state to the associated props for this component.
  118. *
  119. * @param {Object} state - The Redux state.
  120. * @private
  121. * @returns {Props}
  122. */
  123. function mapStateToProps(state) {
  124. const { e2eeKey } = state['features/e2ee'];
  125. const participants = getParticipants(state).filter(p => !p.local);
  126. return {
  127. _everyoneSupportsE2EE: participants.every(p => Boolean(p.e2eeSupported)),
  128. _key: e2eeKey || ''
  129. };
  130. }
  131. export default translate(connect(mapStateToProps)(E2EEDialog));