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.tsx 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /* eslint-disable react/jsx-no-bind */
  2. import React, { useRef, useState } from 'react';
  3. import { WithTranslation } from 'react-i18next';
  4. import { translate } from '../../../../base/i18n/functions';
  5. import { copyText } from '../../../../base/util/copyText.web';
  6. import { LOCKED_LOCALLY } from '../../../../room-lock/constants';
  7. import { NOTIFY_CLICK_MODE } from '../../../../toolbox/constants';
  8. import PasswordForm from './PasswordForm';
  9. import { INotifyClick } from './SecurityDialog';
  10. const DIGITS_ONLY = /^\d+$/;
  11. const KEY = 'add-passcode';
  12. interface IProps extends WithTranslation {
  13. /**
  14. * Toolbar buttons which have their click exposed through the API.
  15. */
  16. buttonsWithNotifyClick: Array<string | INotifyClick>;
  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. /**
  53. * Component that handles the password manipulation from the invite dialog.
  54. *
  55. * @returns {React$Element<any>}
  56. */
  57. function PasswordSection({
  58. buttonsWithNotifyClick,
  59. canEditPassword,
  60. conference,
  61. locked,
  62. password,
  63. passwordEditEnabled,
  64. passwordNumberOfDigits,
  65. setPassword,
  66. setPasswordEditEnabled,
  67. t }: IProps) {
  68. const formRef = useRef<HTMLDivElement>(null);
  69. const [ passwordVisible, setPasswordVisible ] = useState(false);
  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 | INotifyClick) =>
  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. * Callback invoked to show the current password.
  204. *
  205. * @returns {void}
  206. */
  207. function onPasswordShow() {
  208. setPasswordVisible(true);
  209. }
  210. /**
  211. * Callback invoked to show the current password.
  212. *
  213. * @param {Object} e - The key event to handle.
  214. *
  215. * @returns {void}
  216. */
  217. function onPasswordShowKeyPressHandler(e: React.KeyboardEvent) {
  218. if (e.key === ' ' || e.key === 'Enter') {
  219. e.preventDefault();
  220. setPasswordVisible(true);
  221. }
  222. }
  223. /**
  224. * Callback invoked to hide the current password.
  225. *
  226. * @returns {void}
  227. */
  228. function onPasswordHide() {
  229. setPasswordVisible(false);
  230. }
  231. /**
  232. * Callback invoked to hide the current password.
  233. *
  234. * @param {Object} e - The key event to handle.
  235. *
  236. * @returns {void}
  237. */
  238. function onPasswordHideKeyPressHandler(e: React.KeyboardEvent) {
  239. if (e.key === ' ' || e.key === 'Enter') {
  240. e.preventDefault();
  241. setPasswordVisible(false);
  242. }
  243. }
  244. /**
  245. * Method that renders the password action(s) based on the current
  246. * locked-status of the conference.
  247. *
  248. * @returns {React$Element<any>}
  249. */
  250. function renderPasswordActions() {
  251. if (!canEditPassword) {
  252. return null;
  253. }
  254. if (passwordEditEnabled) {
  255. return (
  256. <>
  257. <a
  258. aria-label = { t('dialog.Cancel') }
  259. onClick = { onTogglePasswordEditState }
  260. onKeyPress = { onTogglePasswordEditStateKeyPressHandler }
  261. role = 'button'
  262. tabIndex = { 0 }>{ t('dialog.Cancel') }</a>
  263. <a
  264. aria-label = { t('dialog.add') }
  265. onClick = { onPasswordSave }
  266. onKeyPress = { onPasswordSaveKeyPressHandler }
  267. role = 'button'
  268. tabIndex = { 0 }>{ t('dialog.add') }</a>
  269. </>
  270. );
  271. }
  272. if (locked) {
  273. return (
  274. <>
  275. <a
  276. aria-label = { t('dialog.Remove') }
  277. className = 'remove-password'
  278. onClick = { onPasswordRemove }
  279. onKeyPress = { onPasswordRemoveKeyPressHandler }
  280. role = 'button'
  281. tabIndex = { 0 }>{ t('dialog.Remove') }</a>
  282. {
  283. // There are cases like lobby and grant moderator when password is not available
  284. password ? <>
  285. <a
  286. aria-label = { t('dialog.copy') }
  287. className = 'copy-password'
  288. onClick = { onPasswordCopy }
  289. onKeyPress = { onPasswordCopyKeyPressHandler }
  290. role = 'button'
  291. tabIndex = { 0 }>{ t('dialog.copy') }</a>
  292. </> : null
  293. }
  294. {locked === LOCKED_LOCALLY && (
  295. <a
  296. aria-label = { t(passwordVisible ? 'dialog.hide' : 'dialog.show') }
  297. onClick = { passwordVisible ? onPasswordHide : onPasswordShow }
  298. onKeyPress = { passwordVisible
  299. ? onPasswordHideKeyPressHandler
  300. : onPasswordShowKeyPressHandler
  301. }
  302. role = 'button'
  303. tabIndex = { 0 }>{t(passwordVisible ? 'dialog.hide' : 'dialog.show')}</a>
  304. )}
  305. </>
  306. );
  307. }
  308. return (
  309. <a
  310. aria-label = { t('info.addPassword') }
  311. className = 'add-password'
  312. onClick = { onTogglePasswordEditState }
  313. onKeyPress = { onTogglePasswordEditStateKeyPressHandler }
  314. role = 'button'
  315. tabIndex = { 0 }>{ t('info.addPassword') }</a>
  316. );
  317. }
  318. return (
  319. <div className = 'security-dialog password-section'>
  320. <p className = 'description'>
  321. { t(canEditPassword ? 'security.about' : 'security.aboutReadOnly') }
  322. </p>
  323. <div className = 'security-dialog password'>
  324. <div
  325. className = 'info-dialog info-dialog-column info-dialog-password'
  326. ref = { formRef }>
  327. <PasswordForm
  328. editEnabled = { passwordEditEnabled }
  329. locked = { locked }
  330. onSubmit = { onPasswordSubmit }
  331. password = { password }
  332. passwordNumberOfDigits = { passwordNumberOfDigits }
  333. visible = { passwordVisible } />
  334. </div>
  335. <div className = 'security-dialog password-actions'>
  336. { renderPasswordActions() }
  337. </div>
  338. </div>
  339. </div>
  340. );
  341. }
  342. export default translate(PasswordSection);