import { WebApplication } from '@/Application/Application' import { ViewControllerManager } from '@/Services/ViewControllerManager' import { isDev } from '@/Utils' import { observer } from 'mobx-react-lite' import React, { FunctionComponent, KeyboardEventHandler, useCallback, useEffect, useRef, useState } from 'react' import { AccountMenuPane } from './AccountMenuPane' import Button from '@/Components/Button/Button' import Checkbox from '@/Components/Checkbox/Checkbox' import DecoratedInput from '@/Components/Input/DecoratedInput' import DecoratedPasswordInput from '@/Components/Input/DecoratedPasswordInput' import Icon from '@/Components/Icon/Icon' import IconButton from '@/Components/Button/IconButton' import AdvancedOptions from './AdvancedOptions' type Props = { viewControllerManager: ViewControllerManager application: WebApplication setMenuPane: (pane: AccountMenuPane) => void } const SignInPane: FunctionComponent = ({ application, viewControllerManager, setMenuPane }) => { const { notesAndTagsCount } = viewControllerManager.accountMenuController const [email, setEmail] = useState('') const [password, setPassword] = useState('') const [error, setError] = useState('') const [isEphemeral, setIsEphemeral] = useState(false) const [isStrictSignin, setIsStrictSignin] = useState(false) const [isSigningIn, setIsSigningIn] = useState(false) const [shouldMergeLocal, setShouldMergeLocal] = useState(true) const [isPrivateWorkspace, setIsPrivateWorkspace] = useState(false) const emailInputRef = useRef(null) const passwordInputRef = useRef(null) useEffect(() => { if (emailInputRef?.current) { emailInputRef.current?.focus() } if (isDev && window.devAccountEmail) { setEmail(window.devAccountEmail) setPassword(window.devAccountPassword as string) } }, []) const resetInvalid = useCallback(() => { if (error.length) { setError('') } }, [setError, error]) const handleEmailChange = useCallback((text: string) => { setEmail(text) }, []) const handlePasswordChange = useCallback( (text: string) => { if (error.length) { setError('') } setPassword(text) }, [setPassword, error], ) const handleEphemeralChange = useCallback(() => { setIsEphemeral(!isEphemeral) }, [isEphemeral]) const handleStrictSigninChange = useCallback(() => { setIsStrictSignin(!isStrictSignin) }, [isStrictSignin]) const handleShouldMergeChange = useCallback(() => { setShouldMergeLocal(!shouldMergeLocal) }, [shouldMergeLocal]) const signIn = useCallback(() => { 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) } viewControllerManager.accountMenuController.closeAccountMenu() }) .catch((err) => { console.error(err) setError(err.message ?? err.toString()) setPassword('') passwordInputRef?.current?.blur() }) .finally(() => { setIsSigningIn(false) }) }, [viewControllerManager, application, email, isEphemeral, isStrictSignin, password, shouldMergeLocal]) const onPrivateWorkspaceChange = useCallback( (newIsPrivateWorkspace: boolean, privateWorkspaceIdentifier?: string) => { setIsPrivateWorkspace(newIsPrivateWorkspace) if (newIsPrivateWorkspace && privateWorkspaceIdentifier) { setEmail(privateWorkspaceIdentifier) } }, [setEmail], ) const handleSignInFormSubmit = useCallback( (e: React.SyntheticEvent) => { e.preventDefault() if (!email || email.length === 0) { emailInputRef?.current?.focus() return } if (!password || password.length === 0) { passwordInputRef?.current?.focus() return } signIn() }, [email, password, signIn], ) const handleKeyDown: KeyboardEventHandler = useCallback( (e) => { if (e.key === 'Enter') { handleSignInFormSubmit(e) } }, [handleSignInFormSubmit], ) return ( <>
setMenuPane(AccountMenuPane.GeneralMenu)} focusable={true} disabled={isSigningIn} />
Sign in
]} type="email" placeholder="Email" value={email} onChange={handleEmailChange} onFocus={resetInvalid} onKeyDown={handleKeyDown} disabled={isSigningIn || isPrivateWorkspace} ref={emailInputRef} /> ]} onChange={handlePasswordChange} onFocus={resetInvalid} onKeyDown={handleKeyDown} placeholder="Password" ref={passwordInputRef} value={password} /> {error ?
{error}
: null}
) } export default observer(SignInPane)