|
| 1 | +import React, { useCallback, useState } from 'react'; |
| 2 | +import { useTranslation } from 'react-i18next'; |
| 3 | + |
| 4 | +import { Button } from '@/components/ui/button'; |
| 5 | +import { |
| 6 | + Dialog, |
| 7 | + DialogContent, |
| 8 | + DialogDescription, |
| 9 | + DialogFooter, |
| 10 | + DialogHeader, |
| 11 | + DialogTitle, |
| 12 | +} from '@/components/ui/dialog'; |
| 13 | +import { Input } from '@/components/ui/input'; |
| 14 | + |
| 15 | +// Email validation regex - checks for valid email format |
| 16 | +const EMAIL_REGEX = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; |
| 17 | + |
| 18 | +interface SamlLoginDialogProps { |
| 19 | + open: boolean; |
| 20 | + onOpenChange: (open: boolean) => void; |
| 21 | + onSubmit: (domain: string) => Promise<void>; |
| 22 | +} |
| 23 | + |
| 24 | +function SamlLoginDialog({ open, onOpenChange, onSubmit }: SamlLoginDialogProps) { |
| 25 | + const { t } = useTranslation(); |
| 26 | + const [email, setEmail] = useState(''); |
| 27 | + const [loading, setLoading] = useState(false); |
| 28 | + const [error, setError] = useState<string | null>(null); |
| 29 | + |
| 30 | + const resetState = useCallback(() => { |
| 31 | + setEmail(''); |
| 32 | + setError(null); |
| 33 | + setLoading(false); |
| 34 | + }, []); |
| 35 | + |
| 36 | + const handleOpenChange = useCallback( |
| 37 | + (newOpen: boolean) => { |
| 38 | + onOpenChange(newOpen); |
| 39 | + |
| 40 | + if (!newOpen) { |
| 41 | + resetState(); |
| 42 | + } |
| 43 | + }, |
| 44 | + [onOpenChange, resetState] |
| 45 | + ); |
| 46 | + |
| 47 | + const validateEmail = useCallback( |
| 48 | + (emailValue: string): string | null => { |
| 49 | + if (!emailValue.trim()) { |
| 50 | + return t('web.emailRequired'); |
| 51 | + } |
| 52 | + |
| 53 | + if (!EMAIL_REGEX.test(emailValue)) { |
| 54 | + return t('web.invalidEmail'); |
| 55 | + } |
| 56 | + |
| 57 | + return null; |
| 58 | + }, |
| 59 | + [t] |
| 60 | + ); |
| 61 | + |
| 62 | + const handleSubmit = useCallback(async () => { |
| 63 | + const validationError = validateEmail(email); |
| 64 | + |
| 65 | + if (validationError) { |
| 66 | + setError(validationError); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + // Extract domain from email |
| 71 | + const domain = email.split('@')[1]; |
| 72 | + |
| 73 | + setLoading(true); |
| 74 | + setError(null); |
| 75 | + |
| 76 | + try { |
| 77 | + await onSubmit(domain); |
| 78 | + } catch (e: unknown) { |
| 79 | + const err = e as { message?: string }; |
| 80 | + |
| 81 | + setError(err?.message || t('web.signInError')); |
| 82 | + } finally { |
| 83 | + setLoading(false); |
| 84 | + } |
| 85 | + }, [email, validateEmail, onSubmit, t]); |
| 86 | + |
| 87 | + const handleKeyDown = useCallback( |
| 88 | + (e: React.KeyboardEvent) => { |
| 89 | + if (e.key === 'Enter' && !loading) { |
| 90 | + void handleSubmit(); |
| 91 | + } |
| 92 | + }, |
| 93 | + [handleSubmit, loading] |
| 94 | + ); |
| 95 | + |
| 96 | + const handleEmailChange = useCallback((e: React.ChangeEvent<HTMLInputElement>) => { |
| 97 | + setEmail(e.target.value); |
| 98 | + setError(null); |
| 99 | + }, []); |
| 100 | + |
| 101 | + return ( |
| 102 | + <Dialog open={open} onOpenChange={handleOpenChange}> |
| 103 | + <DialogContent className="sm:max-w-md"> |
| 104 | + <DialogHeader> |
| 105 | + <DialogTitle>{t('web.ssoLogin')}</DialogTitle> |
| 106 | + <DialogDescription id="saml-dialog-description"> |
| 107 | + {t('web.ssoLoginDescription')} |
| 108 | + </DialogDescription> |
| 109 | + </DialogHeader> |
| 110 | + <div className="flex flex-col gap-2 py-4"> |
| 111 | + <Input |
| 112 | + type="email" |
| 113 | + placeholder={t('web.emailPlaceholder')} |
| 114 | + value={email} |
| 115 | + onChange={handleEmailChange} |
| 116 | + onKeyDown={handleKeyDown} |
| 117 | + disabled={loading} |
| 118 | + aria-label={t('web.emailPlaceholder')} |
| 119 | + aria-describedby="saml-dialog-description" |
| 120 | + aria-invalid={!!error} |
| 121 | + autoFocus |
| 122 | + /> |
| 123 | + {error && ( |
| 124 | + <p className="text-sm text-destructive" role="alert"> |
| 125 | + {error} |
| 126 | + </p> |
| 127 | + )} |
| 128 | + </div> |
| 129 | + <DialogFooter> |
| 130 | + <Button |
| 131 | + variant="outline" |
| 132 | + onClick={() => handleOpenChange(false)} |
| 133 | + disabled={loading} |
| 134 | + > |
| 135 | + {t('button.cancel')} |
| 136 | + </Button> |
| 137 | + <Button |
| 138 | + onClick={handleSubmit} |
| 139 | + disabled={loading || !email.trim()} |
| 140 | + > |
| 141 | + {loading ? t('web.signingIn') : t('web.continueWithSso')} |
| 142 | + </Button> |
| 143 | + </DialogFooter> |
| 144 | + </DialogContent> |
| 145 | + </Dialog> |
| 146 | + ); |
| 147 | +} |
| 148 | + |
| 149 | +export default SamlLoginDialog; |
0 commit comments