refactor: reviewer's comments
- use state variables to decide whether email or passcode fields should be focused
This commit is contained in:
@@ -31,7 +31,6 @@ import TargetedEvent = JSXInternal.TargetedEvent;
|
|||||||
import TargetedKeyboardEvent = JSXInternal.TargetedKeyboardEvent;
|
import TargetedKeyboardEvent = JSXInternal.TargetedKeyboardEvent;
|
||||||
import TargetedMouseEvent = JSXInternal.TargetedMouseEvent;
|
import TargetedMouseEvent = JSXInternal.TargetedMouseEvent;
|
||||||
import { alertDialog, confirmDialog } from '@Services/alertService';
|
import { alertDialog, confirmDialog } from '@Services/alertService';
|
||||||
import { RefObject } from 'react';
|
|
||||||
import { ConfirmSignoutContainer } from '@/components/ConfirmSignoutModal';
|
import { ConfirmSignoutContainer } from '@/components/ConfirmSignoutModal';
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
@@ -105,6 +104,9 @@ const AccountMenu = observer(({ application, appState }: Props) => {
|
|||||||
const [hasProtections] = useState(application.hasProtectionSources());
|
const [hasProtections] = useState(application.hasProtectionSources());
|
||||||
const [notesAndTagsCount] = useState(appState.accountMenu.notesAndTagsCount);
|
const [notesAndTagsCount] = useState(appState.accountMenu.notesAndTagsCount);
|
||||||
|
|
||||||
|
const [isEmailFocused, setIsEmailFocused] = useState(false);
|
||||||
|
const [isPasscodeFocused, setIsPasscodeFocused] = useState(false);
|
||||||
|
|
||||||
const refreshedCredentialState = () => {
|
const refreshedCredentialState = () => {
|
||||||
setUser(application.getUser());
|
setUser(application.getUser());
|
||||||
setCanAddPasscode(!application.isEphemeralSession());
|
setCanAddPasscode(!application.isEphemeralSession());
|
||||||
@@ -147,26 +149,18 @@ const AccountMenu = observer(({ application, appState }: Props) => {
|
|||||||
const passcodeAutoLockOptions = application.getAutolockService().getAutoLockIntervalOptions();
|
const passcodeAutoLockOptions = application.getAutolockService().getAutoLockIntervalOptions();
|
||||||
const showBetaWarning = appState.showBetaWarning;
|
const showBetaWarning = appState.showBetaWarning;
|
||||||
|
|
||||||
const focusWithTimeout = (inputElementRef: RefObject<HTMLInputElement>) => {
|
|
||||||
// In case the ref element is not yet available at this moment,
|
|
||||||
// we call `focus()` after timeout.
|
|
||||||
setTimeout(() => {
|
|
||||||
inputElementRef.current && inputElementRef.current.focus();
|
|
||||||
}, 0);
|
|
||||||
};
|
|
||||||
|
|
||||||
const closeAccountMenu = () => {
|
const closeAccountMenu = () => {
|
||||||
appState.accountMenu.closeAccountMenu();
|
appState.accountMenu.closeAccountMenu();
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSignInClick = () => {
|
const handleSignInClick = () => {
|
||||||
setShowLogin(true);
|
setShowLogin(true);
|
||||||
focusWithTimeout(emailInputRef);
|
setIsEmailFocused(true);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleRegisterClick = () => {
|
const handleRegisterClick = () => {
|
||||||
setShowRegister(true);
|
setShowRegister(true);
|
||||||
focusWithTimeout(emailInputRef);
|
setIsEmailFocused(true);
|
||||||
};
|
};
|
||||||
|
|
||||||
const blurAuthFields = () => {
|
const blurAuthFields = () => {
|
||||||
@@ -315,10 +309,7 @@ const AccountMenu = observer(({ application, appState }: Props) => {
|
|||||||
|
|
||||||
const handleAddPassCode = () => {
|
const handleAddPassCode = () => {
|
||||||
setShowPasscodeForm(true);
|
setShowPasscodeForm(true);
|
||||||
|
setIsPasscodeFocused(true);
|
||||||
// At this moment the passcode input is not available, therefore the ref
|
|
||||||
// is null. Therefore we call `focus()` after timeout.
|
|
||||||
focusWithTimeout(passcodeInputRef);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const submitPasscodeForm = async (event: TargetedEvent<HTMLFormElement> | TargetedMouseEvent<HTMLButtonElement>) => {
|
const submitPasscodeForm = async (event: TargetedEvent<HTMLFormElement> | TargetedMouseEvent<HTMLButtonElement>) => {
|
||||||
@@ -328,7 +319,7 @@ const AccountMenu = observer(({ application, appState }: Props) => {
|
|||||||
await alertDialog({
|
await alertDialog({
|
||||||
text: STRING_NON_MATCHING_PASSCODES
|
text: STRING_NON_MATCHING_PASSCODES
|
||||||
});
|
});
|
||||||
passcodeInputRef.current.focus();
|
setIsPasscodeFocused(true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -340,7 +331,7 @@ const AccountMenu = observer(({ application, appState }: Props) => {
|
|||||||
: await application.addPasscode(passcode as string);
|
: await application.addPasscode(passcode as string);
|
||||||
|
|
||||||
if (!successful) {
|
if (!successful) {
|
||||||
passcodeInputRef.current.focus();
|
setIsPasscodeFocused(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
@@ -541,6 +532,17 @@ const AccountMenu = observer(({ application, appState }: Props) => {
|
|||||||
refreshEncryptionStatus();
|
refreshEncryptionStatus();
|
||||||
}, [refreshEncryptionStatus]);
|
}, [refreshEncryptionStatus]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (isEmailFocused) {
|
||||||
|
emailInputRef.current.focus();
|
||||||
|
setIsEmailFocused(false);
|
||||||
|
}
|
||||||
|
if (isPasscodeFocused) {
|
||||||
|
passcodeInputRef.current.focus();
|
||||||
|
setIsPasscodeFocused(false);
|
||||||
|
}
|
||||||
|
}, [isEmailFocused, isPasscodeFocused]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className='sn-component'>
|
<div className='sn-component'>
|
||||||
<div id='account-panel' className='sk-panel'>
|
<div id='account-panel' className='sk-panel'>
|
||||||
|
|||||||
Reference in New Issue
Block a user