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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 { 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. const [ passwordVisible, setPasswordVisible ] = useState(false);
  71. /**
  72. * Callback invoked to set a password on the current JitsiConference.
  73. *
  74. * @param {string} enteredPassword - The new password to be used to lock the
  75. * current JitsiConference.
  76. * @private
  77. * @returns {void}
  78. */
  79. function onPasswordSubmit(enteredPassword: string) {
  80. if (enteredPassword && passwordNumberOfDigits && !DIGITS_ONLY.test(enteredPassword)) {
  81. // Don't set the password.
  82. return;
  83. }
  84. setPassword(conference, conference.lock, enteredPassword);
  85. }
  86. /**
  87. * Toggles whether or not the password should currently be shown as being
  88. * edited locally.
  89. *
  90. * @private
  91. * @returns {void}
  92. */
  93. function onTogglePasswordEditState() {
  94. if (typeof APP === 'undefined' || !buttonsWithNotifyClick?.length) {
  95. setPasswordEditEnabled(!passwordEditEnabled);
  96. return;
  97. }
  98. let notifyMode;
  99. const notify = buttonsWithNotifyClick.find(
  100. (btn: string | NotifyClick) =>
  101. (typeof btn === 'string' && btn === KEY)
  102. || (typeof btn === 'object' && btn.key === KEY)
  103. );
  104. if (notify) {
  105. notifyMode = typeof notify === 'string' || notify.preventExecution
  106. ? NOTIFY_CLICK_MODE.PREVENT_AND_NOTIFY
  107. : NOTIFY_CLICK_MODE.ONLY_NOTIFY;
  108. APP.API.notifyToolbarButtonClicked(
  109. KEY, notifyMode === NOTIFY_CLICK_MODE.PREVENT_AND_NOTIFY
  110. );
  111. }
  112. if (notifyMode === NOTIFY_CLICK_MODE.ONLY_NOTIFY) {
  113. setPasswordEditEnabled(!passwordEditEnabled);
  114. }
  115. }
  116. /**
  117. * Method to remotely submit the password from outside of the password form.
  118. *
  119. * @returns {void}
  120. */
  121. function onPasswordSave() {
  122. if (formRef.current) {
  123. // @ts-ignore
  124. const { value } = formRef.current.querySelector('div > input');
  125. if (value) {
  126. onPasswordSubmit(value);
  127. }
  128. }
  129. }
  130. /**
  131. * Callback invoked to unlock the current JitsiConference.
  132. *
  133. * @returns {void}
  134. */
  135. function onPasswordRemove() {
  136. onPasswordSubmit('');
  137. }
  138. /**
  139. * Copies the password to the clipboard.
  140. *
  141. * @returns {void}
  142. */
  143. function onPasswordCopy() {
  144. copyText(password);
  145. }
  146. /**
  147. * Toggles whether or not the password should currently be shown as being
  148. * edited locally.
  149. *
  150. * @param {Object} e - The key event to handle.
  151. *
  152. * @private
  153. * @returns {void}
  154. */
  155. function onTogglePasswordEditStateKeyPressHandler(e: React.KeyboardEvent) {
  156. if (e.key === ' ' || e.key === 'Enter') {
  157. e.preventDefault();
  158. onTogglePasswordEditState();
  159. }
  160. }
  161. /**
  162. * Method to remotely submit the password from outside of the password form.
  163. *
  164. * @param {Object} e - The key event to handle.
  165. *
  166. * @private
  167. * @returns {void}
  168. */
  169. function onPasswordSaveKeyPressHandler(e: React.KeyboardEvent) {
  170. if (e.key === ' ' || e.key === 'Enter') {
  171. e.preventDefault();
  172. onPasswordSave();
  173. }
  174. }
  175. /**
  176. * Callback invoked to unlock the current JitsiConference.
  177. *
  178. * @param {Object} e - The key event to handle.
  179. *
  180. * @private
  181. * @returns {void}
  182. */
  183. function onPasswordRemoveKeyPressHandler(e: React.KeyboardEvent) {
  184. if (e.key === ' ' || e.key === 'Enter') {
  185. e.preventDefault();
  186. onPasswordRemove();
  187. }
  188. }
  189. /**
  190. * Copies the password to the clipboard.
  191. *
  192. * @param {Object} e - The key event to handle.
  193. *
  194. * @private
  195. * @returns {void}
  196. */
  197. function onPasswordCopyKeyPressHandler(e: React.KeyboardEvent) {
  198. if (e.key === ' ' || e.key === 'Enter') {
  199. e.preventDefault();
  200. onPasswordCopy();
  201. }
  202. }
  203. /**
  204. * Callback invoked to show the current password.
  205. *
  206. * @returns {void}
  207. */
  208. function onPasswordShow() {
  209. setPasswordVisible(true);
  210. }
  211. /**
  212. * Callback invoked to show the current password.
  213. *
  214. * @param {Object} e - The key event to handle.
  215. *
  216. * @returns {void}
  217. */
  218. function onPasswordShowKeyPressHandler(e: React.KeyboardEvent) {
  219. if (e.key === ' ' || e.key === 'Enter') {
  220. e.preventDefault();
  221. setPasswordVisible(true);
  222. }
  223. }
  224. /**
  225. * Callback invoked to hide the current password.
  226. *
  227. * @returns {void}
  228. */
  229. function onPasswordHide() {
  230. setPasswordVisible(false);
  231. }
  232. /**
  233. * Callback invoked to hide the current password.
  234. *
  235. * @param {Object} e - The key event to handle.
  236. *
  237. * @returns {void}
  238. */
  239. function onPasswordHideKeyPressHandler(e: React.KeyboardEvent) {
  240. if (e.key === ' ' || e.key === 'Enter') {
  241. e.preventDefault();
  242. setPasswordVisible(false);
  243. }
  244. }
  245. /**
  246. * Method that renders the password action(s) based on the current
  247. * locked-status of the conference.
  248. *
  249. * @returns {React$Element<any>}
  250. */
  251. function renderPasswordActions() {
  252. if (!canEditPassword) {
  253. return null;
  254. }
  255. if (passwordEditEnabled) {
  256. return (
  257. <>
  258. <a
  259. aria-label = { t('dialog.Cancel') }
  260. onClick = { onTogglePasswordEditState }
  261. onKeyPress = { onTogglePasswordEditStateKeyPressHandler }
  262. role = 'button'
  263. tabIndex = { 0 }>{ t('dialog.Cancel') }</a>
  264. <a
  265. aria-label = { t('dialog.add') }
  266. onClick = { onPasswordSave }
  267. onKeyPress = { onPasswordSaveKeyPressHandler }
  268. role = 'button'
  269. tabIndex = { 0 }>{ t('dialog.add') }</a>
  270. </>
  271. );
  272. }
  273. if (locked) {
  274. return (
  275. <>
  276. <a
  277. aria-label = { t('dialog.Remove') }
  278. className = 'remove-password'
  279. onClick = { onPasswordRemove }
  280. onKeyPress = { onPasswordRemoveKeyPressHandler }
  281. role = 'button'
  282. tabIndex = { 0 }>{ t('dialog.Remove') }</a>
  283. {
  284. // There are cases like lobby and grant moderator when password is not available
  285. password ? <>
  286. <a
  287. aria-label = { t('dialog.copy') }
  288. className = 'copy-password'
  289. onClick = { onPasswordCopy }
  290. onKeyPress = { onPasswordCopyKeyPressHandler }
  291. role = 'button'
  292. tabIndex = { 0 }>{ t('dialog.copy') }</a>
  293. </> : null
  294. }
  295. {locked === LOCKED_LOCALLY && (
  296. <a
  297. aria-label = { t(passwordVisible ? 'dialog.hide' : 'dialog.show') }
  298. onClick = { passwordVisible ? onPasswordHide : onPasswordShow }
  299. onKeyPress = { passwordVisible
  300. ? onPasswordHideKeyPressHandler
  301. : onPasswordShowKeyPressHandler
  302. }
  303. role = 'button'
  304. tabIndex = { 0 }>{t(passwordVisible ? 'dialog.hide' : 'dialog.show')}</a>
  305. )}
  306. </>
  307. );
  308. }
  309. return (
  310. <a
  311. aria-label = { t('info.addPassword') }
  312. className = 'add-password'
  313. onClick = { onTogglePasswordEditState }
  314. onKeyPress = { onTogglePasswordEditStateKeyPressHandler }
  315. role = 'button'
  316. tabIndex = { 0 }>{ t('info.addPassword') }</a>
  317. );
  318. }
  319. return (
  320. <div className = 'security-dialog password-section'>
  321. <p className = 'description'>
  322. { t(canEditPassword ? 'security.about' : 'security.aboutReadOnly') }
  323. </p>
  324. <div className = 'security-dialog password'>
  325. <div
  326. className = 'info-dialog info-dialog-column info-dialog-password'
  327. ref = { formRef }>
  328. <PasswordForm
  329. editEnabled = { passwordEditEnabled }
  330. locked = { locked }
  331. onSubmit = { onPasswordSubmit }
  332. password = { password }
  333. passwordNumberOfDigits = { passwordNumberOfDigits }
  334. visible = { passwordVisible } />
  335. </div>
  336. <div className = 'security-dialog password-actions'>
  337. { renderPasswordActions() }
  338. </div>
  339. </div>
  340. </div>
  341. );
  342. }
  343. export default translate(PasswordSection);