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

PasswordSection.tsx 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /* eslint-disable react/jsx-no-bind */
  2. import React, { useRef } from 'react';
  3. import { WithTranslation } from 'react-i18next';
  4. import { translate } from '../../../../base/i18n/functions';
  5. import { copyText } from '../../../../base/util/helpers';
  6. import { NOTIFY_CLICK_MODE } from '../../../../toolbox/constants';
  7. // @ts-ignore
  8. import PasswordForm from './PasswordForm';
  9. import { NotifyClick } from './SecurityDialog';
  10. const DIGITS_ONLY = /^\d+$/;
  11. const KEY = 'add-passcode';
  12. interface Props extends WithTranslation {
  13. /**
  14. * Toolbar buttons which have their click exposed through the API.
  15. */
  16. buttonsWithNotifyClick: Array<string | NotifyClick>,
  17. /**
  18. * Whether or not the current user can modify the current password.
  19. */
  20. canEditPassword: boolean,
  21. /**
  22. * The JitsiConference for which to display a lock state and change the
  23. * password.
  24. */
  25. conference: any,
  26. /**
  27. * The value for how the conference is locked (or undefined if not locked)
  28. * as defined by room-lock constants.
  29. */
  30. locked: string,
  31. /**
  32. * The current known password for the JitsiConference.
  33. */
  34. password: string,
  35. /**
  36. * Whether or not to show the password in editing mode.
  37. */
  38. passwordEditEnabled: boolean,
  39. /**
  40. * The number of digits to be used in the password.
  41. */
  42. passwordNumberOfDigits?: number,
  43. /**
  44. * Action that sets the conference password.
  45. */
  46. setPassword: Function,
  47. /**
  48. * Method that sets whether the password editing is enabled or not.
  49. */
  50. setPasswordEditEnabled: Function
  51. }
  52. declare let APP: any;
  53. /**
  54. * Component that handles the password manipulation from the invite dialog.
  55. *
  56. * @returns {React$Element<any>}
  57. */
  58. function PasswordSection({
  59. buttonsWithNotifyClick,
  60. canEditPassword,
  61. conference,
  62. locked,
  63. password,
  64. passwordEditEnabled,
  65. passwordNumberOfDigits,
  66. setPassword,
  67. setPasswordEditEnabled,
  68. t }: Props) {
  69. const formRef = useRef<HTMLDivElement>(null);
  70. /**
  71. * Callback invoked to set a password on the current JitsiConference.
  72. *
  73. * @param {string} enteredPassword - The new password to be used to lock the
  74. * current JitsiConference.
  75. * @private
  76. * @returns {void}
  77. */
  78. function onPasswordSubmit(enteredPassword: string) {
  79. if (enteredPassword && passwordNumberOfDigits && !DIGITS_ONLY.test(enteredPassword)) {
  80. // Don't set the password.
  81. return;
  82. }
  83. setPassword(conference, conference.lock, enteredPassword);
  84. }
  85. /**
  86. * Toggles whether or not the password should currently be shown as being
  87. * edited locally.
  88. *
  89. * @private
  90. * @returns {void}
  91. */
  92. function onTogglePasswordEditState() {
  93. if (typeof APP === 'undefined' || !buttonsWithNotifyClick?.length) {
  94. setPasswordEditEnabled(!passwordEditEnabled);
  95. return;
  96. }
  97. let notifyMode;
  98. const notify = buttonsWithNotifyClick.find(
  99. (btn: string | NotifyClick) =>
  100. (typeof btn === 'string' && btn === KEY)
  101. || (typeof btn === 'object' && btn.key === KEY)
  102. );
  103. if (notify) {
  104. notifyMode = typeof notify === 'string' || notify.preventExecution
  105. ? NOTIFY_CLICK_MODE.PREVENT_AND_NOTIFY
  106. : NOTIFY_CLICK_MODE.ONLY_NOTIFY;
  107. APP.API.notifyToolbarButtonClicked(
  108. KEY, notifyMode === NOTIFY_CLICK_MODE.PREVENT_AND_NOTIFY
  109. );
  110. }
  111. if (notifyMode === NOTIFY_CLICK_MODE.ONLY_NOTIFY) {
  112. setPasswordEditEnabled(!passwordEditEnabled);
  113. }
  114. }
  115. /**
  116. * Method to remotely submit the password from outside of the password form.
  117. *
  118. * @returns {void}
  119. */
  120. function onPasswordSave() {
  121. if (formRef.current) {
  122. // @ts-ignore
  123. const { value } = formRef.current.querySelector('div > input');
  124. if (value) {
  125. onPasswordSubmit(value);
  126. }
  127. }
  128. }
  129. /**
  130. * Callback invoked to unlock the current JitsiConference.
  131. *
  132. * @returns {void}
  133. */
  134. function onPasswordRemove() {
  135. onPasswordSubmit('');
  136. }
  137. /**
  138. * Copies the password to the clipboard.
  139. *
  140. * @returns {void}
  141. */
  142. function onPasswordCopy() {
  143. copyText(password);
  144. }
  145. /**
  146. * Toggles whether or not the password should currently be shown as being
  147. * edited locally.
  148. *
  149. * @param {Object} e - The key event to handle.
  150. *
  151. * @private
  152. * @returns {void}
  153. */
  154. function onTogglePasswordEditStateKeyPressHandler(e: React.KeyboardEvent) {
  155. if (e.key === ' ' || e.key === 'Enter') {
  156. e.preventDefault();
  157. onTogglePasswordEditState();
  158. }
  159. }
  160. /**
  161. * Method to remotely submit the password from outside of the password form.
  162. *
  163. * @param {Object} e - The key event to handle.
  164. *
  165. * @private
  166. * @returns {void}
  167. */
  168. function onPasswordSaveKeyPressHandler(e: React.KeyboardEvent) {
  169. if (e.key === ' ' || e.key === 'Enter') {
  170. e.preventDefault();
  171. onPasswordSave();
  172. }
  173. }
  174. /**
  175. * Callback invoked to unlock the current JitsiConference.
  176. *
  177. * @param {Object} e - The key event to handle.
  178. *
  179. * @private
  180. * @returns {void}
  181. */
  182. function onPasswordRemoveKeyPressHandler(e: React.KeyboardEvent) {
  183. if (e.key === ' ' || e.key === 'Enter') {
  184. e.preventDefault();
  185. onPasswordRemove();
  186. }
  187. }
  188. /**
  189. * Copies the password to the clipboard.
  190. *
  191. * @param {Object} e - The key event to handle.
  192. *
  193. * @private
  194. * @returns {void}
  195. */
  196. function onPasswordCopyKeyPressHandler(e: React.KeyboardEvent) {
  197. if (e.key === ' ' || e.key === 'Enter') {
  198. e.preventDefault();
  199. onPasswordCopy();
  200. }
  201. }
  202. /**
  203. * Method that renders the password action(s) based on the current
  204. * locked-status of the conference.
  205. *
  206. * @returns {React$Element<any>}
  207. */
  208. function renderPasswordActions() {
  209. if (!canEditPassword) {
  210. return null;
  211. }
  212. if (passwordEditEnabled) {
  213. return (
  214. <>
  215. <a
  216. aria-label = { t('dialog.Cancel') }
  217. onClick = { onTogglePasswordEditState }
  218. onKeyPress = { onTogglePasswordEditStateKeyPressHandler }
  219. role = 'button'
  220. tabIndex = { 0 }>{ t('dialog.Cancel') }</a>
  221. <a
  222. aria-label = { t('dialog.add') }
  223. onClick = { onPasswordSave }
  224. onKeyPress = { onPasswordSaveKeyPressHandler }
  225. role = 'button'
  226. tabIndex = { 0 }>{ t('dialog.add') }</a>
  227. </>
  228. );
  229. }
  230. if (locked) {
  231. return (
  232. <>
  233. <a
  234. aria-label = { t('dialog.Remove') }
  235. className = 'remove-password'
  236. onClick = { onPasswordRemove }
  237. onKeyPress = { onPasswordRemoveKeyPressHandler }
  238. role = 'button'
  239. tabIndex = { 0 }>{ t('dialog.Remove') }</a>
  240. {
  241. // There are cases like lobby and grant moderator when password is not available
  242. password ? <>
  243. <a
  244. aria-label = { t('dialog.copy') }
  245. className = 'copy-password'
  246. onClick = { onPasswordCopy }
  247. onKeyPress = { onPasswordCopyKeyPressHandler }
  248. role = 'button'
  249. tabIndex = { 0 }>{ t('dialog.copy') }</a>
  250. </> : null
  251. }
  252. </>
  253. );
  254. }
  255. return (
  256. <a
  257. aria-label = { t('info.addPassword') }
  258. className = 'add-password'
  259. onClick = { onTogglePasswordEditState }
  260. onKeyPress = { onTogglePasswordEditStateKeyPressHandler }
  261. role = 'button'
  262. tabIndex = { 0 }>{ t('info.addPassword') }</a>
  263. );
  264. }
  265. return (
  266. <div className = 'security-dialog password-section'>
  267. <p className = 'description'>
  268. { t(canEditPassword ? 'security.about' : 'security.aboutReadOnly') }
  269. </p>
  270. <div className = 'security-dialog password'>
  271. <div
  272. className = 'info-dialog info-dialog-column info-dialog-password'
  273. ref = { formRef }>
  274. <PasswordForm
  275. editEnabled = { passwordEditEnabled }
  276. locked = { locked }
  277. onSubmit = { onPasswordSubmit }
  278. password = { password }
  279. passwordNumberOfDigits = { passwordNumberOfDigits } />
  280. </div>
  281. <div className = 'security-dialog password-actions'>
  282. { renderPasswordActions() }
  283. </div>
  284. </div>
  285. </div>
  286. );
  287. }
  288. export default translate(PasswordSection);