import { WebApplication } from '@/ui_models/application'; import { AppState } from '@/ui_models/app_state'; import { observer } from 'mobx-react-lite'; import { FunctionComponent } from 'preact'; import { useEffect, useRef, useState } from 'preact/hooks'; import { AccountMenuPane } from '.'; import { Button } from '../Button'; import { Checkbox } from '../Checkbox'; import { Icon } from '../Icon'; import { IconButton } from '../IconButton'; import { InputWithIcon } from '../InputWithIcon'; import { AdvancedOptions } from './AdvancedOptions'; type Props = { appState: AppState; application: WebApplication; setMenuPane: (pane: AccountMenuPane) => void; }; export const SignInPane: FunctionComponent = observer( ({ application, appState, setMenuPane }) => { const { notesAndTagsCount } = appState.accountMenu; const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [isInvalid, setIsInvalid] = useState(false); const [isEphemeral, setIsEphemeral] = useState(false); const [isStrictSignin, setIsStrictSignin] = useState(false); const [isSigningIn, setIsSigningIn] = useState(false); const [showPassword, setShowPassword] = useState(false); const [shouldMergeLocal, setShouldMergeLocal] = useState(true); const emailInputRef = useRef(null); const passwordInputRef = useRef(null); useEffect(() => { if (emailInputRef?.current) { emailInputRef.current?.focus(); } }, []); const resetInvalid = () => { if (isInvalid) { setIsInvalid(false); } }; const handleEmailChange = (e: Event) => { if (e.target instanceof HTMLInputElement) { setEmail(e.target.value); } }; const handlePasswordChange = (e: Event) => { if (isInvalid) { setIsInvalid(false); } if (e.target instanceof HTMLInputElement) { setPassword(e.target.value); } }; const handleEphemeralChange = () => { setIsEphemeral(!isEphemeral); }; const handleStrictSigninChange = () => { setIsStrictSignin(!isStrictSignin); }; const handleShouldMergeChange = () => { setShouldMergeLocal(!shouldMergeLocal); }; const signIn = () => { setIsSigningIn(true); emailInputRef?.current?.blur(); passwordInputRef?.current?.blur(); application .signIn(email, password, isStrictSignin, isEphemeral, shouldMergeLocal) .then((res) => { if (res.error) { throw new Error(res.error.message); } appState.accountMenu.closeAccountMenu(); }) .catch((err) => { console.error(err); if (err.toString().includes('Invalid email or password')) { setIsInvalid(true); } else { application.alertService.alert(err); } setPassword(''); passwordInputRef?.current?.blur(); }) .finally(() => { setIsSigningIn(false); }); }; const handleKeyDown = (e: KeyboardEvent) => { if (e.key === 'Enter') { handleSignInFormSubmit(e); } }; const handleSignInFormSubmit = (e: Event) => { e.preventDefault(); if (!email || email.length === 0) { emailInputRef?.current?.focus(); return; } if (!password || password.length === 0) { passwordInputRef?.current?.focus(); return; } signIn(); }; return ( <>
setMenuPane(AccountMenuPane.GeneralMenu)} focusable={true} disabled={isSigningIn} />
Sign in
{isInvalid ? (
Invalid email or password.
) : null}
); } );