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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. * Callback invoked to show the current password.
  147. *
  148. * @returns {void}
  149. */
  150. function onPasswordShow() {
  151. setPasswordVisible(true);
  152. }
  153. /**
  154. * Callback invoked to hide the current password.
  155. *
  156. * @returns {void}
  157. */
  158. function onPasswordHide() {
  159. setPasswordVisible(false);
  160. }
  161. /**
  162. * Method that renders the password action(s) based on the current
  163. * locked-status of the conference.
  164. *
  165. * @returns {React$Element<any>}
  166. */
  167. function renderPasswordActions() {
  168. if (!canEditPassword) {
  169. return null;
  170. }
  171. if (passwordEditEnabled) {
  172. return (
  173. <>
  174. <button
  175. className = 'as-link'
  176. onClick = { onTogglePasswordEditState }
  177. type = 'button'>
  178. { t('dialog.Cancel') }
  179. <span className = 'sr-only'>({ t('dialog.password') })</span>
  180. </button>
  181. <button
  182. className = 'as-link'
  183. onClick = { onPasswordSave }
  184. type = 'button'>
  185. { t('dialog.add') }
  186. <span className = 'sr-only'>({ t('dialog.password') })</span>
  187. </button>
  188. </>
  189. );
  190. }
  191. if (locked) {
  192. return (
  193. <>
  194. <button
  195. className = 'remove-password as-link'
  196. onClick = { onPasswordRemove }
  197. type = 'button'>
  198. { t('dialog.Remove') }
  199. <span className = 'sr-only'>({ t('dialog.password') })</span>
  200. </button>
  201. {
  202. // There are cases like lobby and grant moderator when password is not available
  203. password ? <>
  204. <button
  205. className = 'copy-password as-link'
  206. onClick = { onPasswordCopy }
  207. type = 'button'>
  208. { t('dialog.copy') }
  209. <span className = 'sr-only'>({ t('dialog.password') })</span>
  210. </button>
  211. </> : null
  212. }
  213. {locked === LOCKED_LOCALLY && (
  214. <button
  215. className = 'as-link'
  216. onClick = { passwordVisible ? onPasswordHide : onPasswordShow }
  217. type = 'button'>
  218. {t(passwordVisible ? 'dialog.hide' : 'dialog.show')}
  219. <span className = 'sr-only'>({ t('dialog.password') })</span>
  220. </button>
  221. )}
  222. </>
  223. );
  224. }
  225. return (
  226. <button
  227. className = 'add-password as-link'
  228. onClick = { onTogglePasswordEditState }
  229. type = 'button'>{ t('info.addPassword') }</button>
  230. );
  231. }
  232. return (
  233. <div className = 'security-dialog password-section'>
  234. <p className = 'description'>
  235. { t(canEditPassword ? 'security.about' : 'security.aboutReadOnly') }
  236. </p>
  237. <div className = 'security-dialog password'>
  238. <div
  239. className = 'info-dialog info-dialog-column info-dialog-password'
  240. ref = { formRef }>
  241. <PasswordForm
  242. editEnabled = { passwordEditEnabled }
  243. locked = { locked }
  244. onSubmit = { onPasswordSubmit }
  245. password = { password }
  246. passwordNumberOfDigits = { passwordNumberOfDigits }
  247. visible = { passwordVisible } />
  248. </div>
  249. <div className = 'security-dialog password-actions'>
  250. { renderPasswordActions() }
  251. </div>
  252. </div>
  253. </div>
  254. );
  255. }
  256. export default translate(PasswordSection);