Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

PasswordSection.js 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // @flow
  2. /* eslint-disable react/no-multi-comp, react/jsx-no-bind */
  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. * Toggles whether or not the password should currently be shown as being
  115. * edited locally.
  116. *
  117. * @param {Object} e - The key event to handle.
  118. *
  119. * @private
  120. * @returns {void}
  121. */
  122. function onTogglePasswordEditStateKeyPressHandler(e) {
  123. if (e.key === ' ' || e.key === 'Enter') {
  124. e.preventDefault();
  125. onTogglePasswordEditState();
  126. }
  127. }
  128. /**
  129. * Method to remotely submit the password from outside of the password form.
  130. *
  131. * @param {Object} e - The key event to handle.
  132. *
  133. * @private
  134. * @returns {void}
  135. */
  136. function onPasswordSaveKeyPressHandler(e) {
  137. if (e.key === ' ' || e.key === 'Enter') {
  138. e.preventDefault();
  139. onPasswordSave();
  140. }
  141. }
  142. /**
  143. * Callback invoked to unlock the current JitsiConference.
  144. *
  145. * @param {Object} e - The key event to handle.
  146. *
  147. * @private
  148. * @returns {void}
  149. */
  150. function onPasswordRemoveKeyPressHandler(e) {
  151. if (e.key === ' ' || e.key === 'Enter') {
  152. e.preventDefault();
  153. onPasswordRemove();
  154. }
  155. }
  156. /**
  157. * Copies the password to the clipboard.
  158. *
  159. * @param {Object} e - The key event to handle.
  160. *
  161. * @private
  162. * @returns {void}
  163. */
  164. function onPasswordCopyKeyPressHandler(e) {
  165. if (e.key === ' ' || e.key === 'Enter') {
  166. e.preventDefault();
  167. onPasswordCopy();
  168. }
  169. }
  170. /**
  171. * Method that renders the password action(s) based on the current
  172. * locked-status of the conference.
  173. *
  174. * @returns {React$Element<any>}
  175. */
  176. function renderPasswordActions() {
  177. if (!canEditPassword) {
  178. return null;
  179. }
  180. if (passwordEditEnabled) {
  181. return (
  182. <>
  183. <a
  184. aria-label = { t('dialog.Cancel') }
  185. onClick = { onTogglePasswordEditState }
  186. onKeyPress = { onTogglePasswordEditStateKeyPressHandler }
  187. role = 'button'
  188. tabIndex = { 0 }>{ t('dialog.Cancel') }</a>
  189. <a
  190. aria-label = { t('dialog.add') }
  191. onClick = { onPasswordSave }
  192. onKeyPress = { onPasswordSaveKeyPressHandler }
  193. role = 'button'
  194. tabIndex = { 0 }>{ t('dialog.add') }</a>
  195. </>
  196. );
  197. }
  198. if (locked) {
  199. return (
  200. <>
  201. <a
  202. aria-label = { t('dialog.Remove') }
  203. className = 'remove-password'
  204. onClick = { onPasswordRemove }
  205. onKeyPress = { onPasswordRemoveKeyPressHandler }
  206. role = 'button'
  207. tabIndex = { 0 }>{ t('dialog.Remove') }</a>
  208. {
  209. // There are cases like lobby and grant moderator when password is not available
  210. password ? <>
  211. <a
  212. aria-label = { t('dialog.copy') }
  213. className = 'copy-password'
  214. onClick = { onPasswordCopy }
  215. onKeyPress = { onPasswordCopyKeyPressHandler }
  216. role = 'button'
  217. tabIndex = { 0 }>{ t('dialog.copy') }</a>
  218. </> : null
  219. }
  220. </>
  221. );
  222. }
  223. return (
  224. <a
  225. aria-label = { t('info.addPassword') }
  226. className = 'add-password'
  227. onClick = { onTogglePasswordEditState }
  228. onKeyPress = { onTogglePasswordEditStateKeyPressHandler }
  229. role = 'button'
  230. tabIndex = { 0 }>{ t('info.addPassword') }</a>
  231. );
  232. }
  233. return (
  234. <div className = 'security-dialog password-section'>
  235. <p className = 'description'>
  236. { t(canEditPassword ? 'security.about' : 'security.aboutReadOnly') }
  237. </p>
  238. <div className = 'security-dialog password'>
  239. <div
  240. className = 'info-dialog info-dialog-column info-dialog-password'
  241. ref = { formRef }>
  242. <PasswordForm
  243. editEnabled = { passwordEditEnabled }
  244. locked = { locked }
  245. onSubmit = { onPasswordSubmit }
  246. password = { password }
  247. passwordNumberOfDigits = { passwordNumberOfDigits } />
  248. </div>
  249. <div className = 'security-dialog password-actions'>
  250. { renderPasswordActions() }
  251. </div>
  252. </div>
  253. </div>
  254. );
  255. }
  256. export default translate(PasswordSection);