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.4KB

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