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

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