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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* eslint-disable react/jsx-no-bind */
  2. import React, { useCallback, useRef, useState } from 'react';
  3. import { useTranslation } from 'react-i18next';
  4. import { useDispatch, useSelector } from 'react-redux';
  5. import { IReduxState } from '../../../../app/types';
  6. import { setPassword } from '../../../../base/conference/actions';
  7. import { isLocalParticipantModerator } from '../../../../base/participants/functions';
  8. import { copyText } from '../../../../base/util/copyText.web';
  9. import { LOCKED_LOCALLY } from '../../../../room-lock/constants';
  10. import { NOTIFY_CLICK_MODE } from '../../../../toolbox/types';
  11. import PasswordForm from './PasswordForm';
  12. const DIGITS_ONLY = /^\d+$/;
  13. const KEY = 'add-passcode';
  14. /**
  15. * Component that handles the password manipulation from the invite dialog.
  16. *
  17. * @returns {React$Element<any>}
  18. */
  19. function PasswordSection() {
  20. const { t } = useTranslation();
  21. const dispatch = useDispatch();
  22. const canEditPassword = useSelector(isLocalParticipantModerator);
  23. const passwordNumberOfDigits = useSelector(
  24. (state: IReduxState) => state['features/base/config'].roomPasswordNumberOfDigits);
  25. const conference = useSelector((state: IReduxState) => state['features/base/conference'].conference);
  26. const locked = useSelector((state: IReduxState) => state['features/base/conference'].locked);
  27. const password = useSelector((state: IReduxState) => state['features/base/conference'].password);
  28. const formRef = useRef<HTMLDivElement>(null);
  29. const [ passwordVisible, setPasswordVisible ] = useState(false);
  30. const buttonsWithNotifyClick = useSelector(
  31. (state: IReduxState) => state['features/toolbox'].buttonsWithNotifyClick);
  32. const [ passwordEditEnabled, setPasswordEditEnabled ] = useState(false);
  33. if (passwordEditEnabled && (password || locked)) {
  34. setPasswordEditEnabled(false);
  35. }
  36. const onPasswordSubmit = useCallback((enteredPassword: string) => {
  37. if (enteredPassword && passwordNumberOfDigits && !DIGITS_ONLY.test(enteredPassword)) {
  38. // Don't set the password.
  39. return;
  40. }
  41. dispatch(setPassword(conference, conference?.lock, enteredPassword));
  42. }, [ dispatch, passwordNumberOfDigits, conference?.lock ]);
  43. const onTogglePasswordEditState = useCallback(() => {
  44. if (typeof APP === 'undefined' || !buttonsWithNotifyClick?.size) {
  45. setPasswordEditEnabled(!passwordEditEnabled);
  46. return;
  47. }
  48. const notifyMode = buttonsWithNotifyClick?.get(KEY);
  49. if (notifyMode) {
  50. APP.API.notifyToolbarButtonClicked(
  51. KEY, notifyMode === NOTIFY_CLICK_MODE.PREVENT_AND_NOTIFY
  52. );
  53. }
  54. if (!notifyMode || notifyMode === NOTIFY_CLICK_MODE.ONLY_NOTIFY) {
  55. setPasswordEditEnabled(!passwordEditEnabled);
  56. }
  57. }, [ buttonsWithNotifyClick, setPasswordEditEnabled, passwordEditEnabled ]);
  58. const onPasswordSave = useCallback(() => {
  59. if (formRef.current) {
  60. // @ts-ignore
  61. const { value } = formRef.current.querySelector('div > input');
  62. if (value) {
  63. onPasswordSubmit(value);
  64. }
  65. }
  66. }, [ formRef.current, onPasswordSubmit ]);
  67. const onPasswordRemove = useCallback(() => {
  68. onPasswordSubmit('');
  69. }, [ onPasswordSubmit ]);
  70. const onPasswordCopy = useCallback(() => {
  71. copyText(password ?? '');
  72. }, [ password ]);
  73. const onPasswordShow = useCallback(() => {
  74. setPasswordVisible(true);
  75. }, [ setPasswordVisible ]);
  76. const onPasswordHide = useCallback(() => {
  77. setPasswordVisible(false);
  78. }, [ setPasswordVisible ]);
  79. let actions = null;
  80. if (canEditPassword) {
  81. if (passwordEditEnabled) {
  82. actions = (
  83. <>
  84. <button
  85. className = 'as-link'
  86. onClick = { onTogglePasswordEditState }
  87. type = 'button'>
  88. { t('dialog.Cancel') }
  89. <span className = 'sr-only'>({ t('dialog.password') })</span>
  90. </button>
  91. <button
  92. className = 'as-link'
  93. onClick = { onPasswordSave }
  94. type = 'button'>
  95. { t('dialog.add') }
  96. <span className = 'sr-only'>({ t('dialog.password') })</span>
  97. </button>
  98. </>
  99. );
  100. } else if (locked) {
  101. actions = (
  102. <>
  103. <button
  104. className = 'remove-password as-link'
  105. onClick = { onPasswordRemove }
  106. type = 'button'>
  107. { t('dialog.Remove') }
  108. <span className = 'sr-only'>({ t('dialog.password') })</span>
  109. </button>
  110. {
  111. // There are cases like lobby and grant moderator when password is not available
  112. password ? <>
  113. <button
  114. className = 'copy-password as-link'
  115. onClick = { onPasswordCopy }
  116. type = 'button'>
  117. { t('dialog.copy') }
  118. <span className = 'sr-only'>({ t('dialog.password') })</span>
  119. </button>
  120. </> : null
  121. }
  122. {locked === LOCKED_LOCALLY && (
  123. <button
  124. className = 'as-link'
  125. onClick = { passwordVisible ? onPasswordHide : onPasswordShow }
  126. type = 'button'>
  127. {t(passwordVisible ? 'dialog.hide' : 'dialog.show')}
  128. <span className = 'sr-only'>({ t('dialog.password') })</span>
  129. </button>
  130. )}
  131. </>
  132. );
  133. } else {
  134. actions = (
  135. <button
  136. className = 'add-password as-link'
  137. onClick = { onTogglePasswordEditState }
  138. type = 'button'>{ t('info.addPassword') }</button>
  139. );
  140. }
  141. }
  142. return (
  143. <div className = 'security-dialog password-section'>
  144. <p className = 'description'>
  145. { t(canEditPassword ? 'security.about' : 'security.aboutReadOnly') }
  146. </p>
  147. <div className = 'security-dialog password'>
  148. <div
  149. className = 'info-dialog info-dialog-column info-dialog-password'
  150. ref = { formRef }>
  151. <PasswordForm
  152. editEnabled = { passwordEditEnabled }
  153. locked = { locked }
  154. onSubmit = { onPasswordSubmit }
  155. password = { password }
  156. passwordNumberOfDigits = { passwordNumberOfDigits }
  157. visible = { passwordVisible } />
  158. </div>
  159. <div className = 'security-dialog password-actions'>
  160. { actions }
  161. </div>
  162. </div>
  163. </div>
  164. );
  165. }
  166. export default PasswordSection;