@@ -4,45 +4,67 @@ import { Input } from '@/components/ui/input';
44import { createHotkey , HOT_KEY_NAME } from '@/utils/hotkeys' ;
55import React , { useContext } from 'react' ;
66import { useTranslation } from 'react-i18next' ;
7+ import { useSearchParams } from 'react-router-dom' ;
78import { toast } from 'sonner' ;
89import isEmail from 'validator/lib/isEmail' ;
910
1011function MagicLink ( { redirectTo } : { redirectTo : string } ) {
1112 const { t } = useTranslation ( ) ;
1213 const [ email , setEmail ] = React . useState < string > ( '' ) ;
13- const [ , setLoading ] = React . useState < boolean > ( false ) ;
14+ const [ loading , setLoading ] = React . useState < boolean > ( false ) ;
15+ const [ error , setError ] = React . useState < string > ( '' ) ;
16+ const [ , setSearch ] = useSearchParams ( ) ;
1417 const service = useContext ( AFConfigContext ) ?. service ;
1518 const handleSubmit = async ( ) => {
19+ if ( loading ) return ;
1620 const isValidEmail = isEmail ( email ) ;
1721
1822 if ( ! isValidEmail ) {
1923 toast . error ( t ( 'signIn.invalidEmail' ) ) ;
2024 return ;
2125 }
2226
27+ setError ( '' ) ;
2328 setLoading ( true ) ;
2429
25- try {
26- void service ?. signInMagicLink ( {
27- email,
28- redirectTo,
29- } ) ;
30+ void ( async ( ) => {
31+ try {
32+ await service ?. signInMagicLink ( {
33+ email,
34+ redirectTo,
35+ } ) ;
36+ // eslint-disable-next-line
37+ } catch ( e : any ) {
38+ if ( e . code === 429 || e . response ?. status === 429 ) {
39+ toast . error ( t ( 'tooManyRequests' ) ) ;
40+ } else {
41+ toast . error ( e . message ) ;
42+ }
43+ } finally {
44+ setLoading ( false ) ;
45+ }
46+ } ) ( ) ;
47+
48+ setSearch ( prev => {
49+ prev . set ( 'email' , email ) ;
50+ prev . set ( 'action' , 'checkEmail' ) ;
51+ return prev ;
52+ } ) ;
3053
31- window . location . href = `/login?action=checkEmail&email=${ email } &redirectTo=${ redirectTo } ` ;
32- } catch ( e ) {
33- toast . error ( t ( 'web.signInError' ) ) ;
34- } finally {
35- setLoading ( false ) ;
36- }
3754 } ;
3855
3956 return (
4057 < div className = { 'flex w-full flex-col items-center justify-center gap-3' } >
4158 < Input
4259 size = { 'md' }
60+ variant = { error ? 'destructive' : 'default' }
61+ helpText = { error }
4362 type = { 'email' }
4463 className = { 'w-[320px]' }
45- onChange = { ( e ) => setEmail ( e . target . value ) }
64+ onChange = { ( e ) => {
65+ setError ( '' ) ;
66+ setEmail ( e . target . value ) ;
67+ } }
4668 value = { email }
4769 placeholder = { t ( 'signIn.pleaseInputYourEmail' ) }
4870 onKeyDown = { e => {
@@ -56,8 +78,9 @@ function MagicLink ({ redirectTo }: { redirectTo: string }) {
5678 onClick = { handleSubmit }
5779 size = { 'lg' }
5880 className = { 'w-[320px]' }
81+ loading = { loading }
5982 >
60- { t ( 'signIn.signInWithEmail' ) }
83+ { loading ? t ( 'loading' ) : t ( 'signIn.signInWithEmail' ) }
6184 </ Button >
6285 </ div >
6386 ) ;
0 commit comments