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 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 '../../../base/util';
  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. const { value } = formRef.current.querySelector('form > input');
  92. if (value) {
  93. onPasswordSubmit(value);
  94. }
  95. }
  96. }
  97. /**
  98. * Callback invoked to unlock the current JitsiConference.
  99. *
  100. * @returns {void}
  101. */
  102. function onPasswordRemove() {
  103. onPasswordSubmit('');
  104. }
  105. /**
  106. * Copies the password to the clipboard.
  107. *
  108. * @returns {void}
  109. */
  110. function onPasswordCopy() {
  111. copyText(password);
  112. }
  113. /**
  114. * Method that renders the password action(s) based on the current
  115. * locked-status of the conference.
  116. *
  117. * @returns {React$Element<any>}
  118. */
  119. function renderPasswordActions() {
  120. if (!canEditPassword) {
  121. return null;
  122. }
  123. if (passwordEditEnabled) {
  124. return (
  125. <>
  126. <a onClick = { onTogglePasswordEditState }>{ t('dialog.Cancel') }</a>
  127. <a onClick = { onPasswordSave }>{ t('dialog.add') }</a>
  128. </>
  129. );
  130. }
  131. if (locked) {
  132. return (
  133. <>
  134. <a
  135. className = 'remove-password'
  136. onClick = { onPasswordRemove }>{ t('dialog.Remove') }</a>
  137. {
  138. // There are cases like lobby and grant moderator when password is not available
  139. password ? <>
  140. <a
  141. className = 'copy-password'
  142. onClick = { onPasswordCopy }>{ t('dialog.copy') }</a>
  143. </> : null
  144. }
  145. </>
  146. );
  147. }
  148. return (
  149. <a
  150. className = 'add-password'
  151. onClick = { onTogglePasswordEditState }>{ t('info.addPassword') }</a>
  152. );
  153. }
  154. return (
  155. <div className = 'security-dialog password-section'>
  156. <p className = 'description'>
  157. { t(canEditPassword ? 'security.about' : 'security.aboutReadOnly') }
  158. </p>
  159. <div className = 'security-dialog password'>
  160. <div
  161. className = 'info-dialog info-dialog-column info-dialog-password'
  162. ref = { formRef }>
  163. <PasswordForm
  164. editEnabled = { passwordEditEnabled }
  165. locked = { locked }
  166. onSubmit = { onPasswordSubmit }
  167. password = { password }
  168. passwordNumberOfDigits = { passwordNumberOfDigits } />
  169. </div>
  170. <div className = 'security-dialog password-actions'>
  171. { renderPasswordActions() }
  172. </div>
  173. </div>
  174. </div>
  175. );
  176. }
  177. export default translate(PasswordSection);