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.

PasswordSection.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* eslint-disable react/no-multi-comp */
  2. // @flow
  3. import React, { useRef } from 'react';
  4. import { translate } from '../../../base/i18n';
  5. import { copyText } from '../../../invite';
  6. import PasswordForm from './PasswordForm';
  7. type Props = {
  8. /**
  9. * Whether or not the current user can modify the current password.
  10. */
  11. canEditPassword: boolean,
  12. /**
  13. * The JitsiConference for which to display a lock state and change the
  14. * password.
  15. */
  16. conference: Object,
  17. /**
  18. * The value for how the conference is locked (or undefined if not locked)
  19. * as defined by room-lock constants.
  20. */
  21. locked: string,
  22. /**
  23. * The current known password for the JitsiConference.
  24. */
  25. password: string,
  26. /**
  27. * Whether or not to show the password in editing mode.
  28. */
  29. passwordEditEnabled: boolean,
  30. /**
  31. * The number of digits to be used in the password.
  32. */
  33. passwordNumberOfDigits: ?number,
  34. /**
  35. * Action that sets the conference password.
  36. */
  37. setPassword: Function,
  38. /**
  39. * Method that sets whether the password editing is enabled or not.
  40. */
  41. setPasswordEditEnabled: Function,
  42. /**
  43. * Invoked to obtain translated strings.
  44. */
  45. t: Function
  46. };
  47. /**
  48. * Component that handles the password manipulation from the invite dialog.
  49. *
  50. * @returns {React$Element<any>}
  51. */
  52. function PasswordSection({
  53. canEditPassword,
  54. conference,
  55. locked,
  56. password,
  57. passwordEditEnabled,
  58. passwordNumberOfDigits,
  59. setPassword,
  60. setPasswordEditEnabled,
  61. t }: Props) {
  62. const formRef: Object = useRef(null);
  63. /**
  64. * Callback invoked to set a password on the current JitsiConference.
  65. *
  66. * @param {string} enteredPassword - The new password to be used to lock the
  67. * current JitsiConference.
  68. * @private
  69. * @returns {void}
  70. */
  71. function onPasswordSubmit(enteredPassword) {
  72. setPassword(conference, conference.lock, enteredPassword);
  73. }
  74. /**
  75. * Toggles whether or not the password should currently be shown as being
  76. * edited locally.
  77. *
  78. * @private
  79. * @returns {void}
  80. */
  81. function onTogglePasswordEditState() {
  82. setPasswordEditEnabled(!passwordEditEnabled);
  83. }
  84. /**
  85. * Method to remotely submit the password from outside of the password form.
  86. *
  87. * @returns {void}
  88. */
  89. function onPasswordSave() {
  90. if (formRef.current) {
  91. formRef.current.querySelector('form').requestSubmit();
  92. }
  93. }
  94. /**
  95. * Callback invoked to unlock the current JitsiConference.
  96. *
  97. * @returns {void}
  98. */
  99. function onPasswordRemove() {
  100. onPasswordSubmit('');
  101. }
  102. /**
  103. * Copies the password to the clipboard.
  104. *
  105. * @returns {void}
  106. */
  107. function onPasswordCopy() {
  108. copyText(password);
  109. }
  110. /**
  111. * Method that renders the password action(s) based on the current
  112. * locked-status of the conference.
  113. *
  114. * @returns {React$Element<any>}
  115. */
  116. function renderPasswordActions() {
  117. if (!canEditPassword) {
  118. return null;
  119. }
  120. if (passwordEditEnabled) {
  121. return (
  122. <>
  123. <a onClick = { onTogglePasswordEditState }>{ t('dialog.Cancel') }</a>
  124. <a onClick = { onPasswordSave }>{ t('dialog.add') }</a>
  125. </>
  126. );
  127. }
  128. if (locked) {
  129. return (
  130. <>
  131. <a
  132. className = 'remove-password'
  133. onClick = { onPasswordRemove }>{ t('dialog.Remove') }</a>
  134. <a
  135. className = 'copy-password'
  136. onClick = { onPasswordCopy }>{ t('dialog.copy') }</a>
  137. </>
  138. );
  139. }
  140. return (
  141. <a
  142. className = 'add-password'
  143. onClick = { onTogglePasswordEditState }>{ t('info.addPassword') }</a>
  144. );
  145. }
  146. return (
  147. <div className = 'security-dialog password-section'>
  148. <p className = 'description'>
  149. { t('security.about') }
  150. </p>
  151. <div className = 'security-dialog password'>
  152. <div
  153. className = 'info-dialog info-dialog-column info-dialog-password'
  154. ref = { formRef }>
  155. <PasswordForm
  156. editEnabled = { passwordEditEnabled }
  157. locked = { locked }
  158. onSubmit = { onPasswordSubmit }
  159. password = { password }
  160. passwordNumberOfDigits = { passwordNumberOfDigits } />
  161. </div>
  162. <div className = 'security-dialog password-actions'>
  163. { renderPasswordActions() }
  164. </div>
  165. </div>
  166. </div>
  167. );
  168. }
  169. export default translate(PasswordSection);