import { useState, useCallback, useEffect } from 'react'; import { useTranslation } from 'react-i18next'; import { CloseIcon } from './icons/CloseIcon'; import { GoogleIcon } from './icons/GoogleIcon'; import { trackEvent } from '../../lib/analytics'; type View = 'login' | 'register' | 'forgot'; export default function AuthModal({ onClose, onLogin, onRegister, onOAuthLogin, onForgotPassword, loading, error, onClearError, initialTab = 'login', }: { onClose: () => void; onLogin: (email: string, password: string) => Promise; onRegister: (email: string, password: string) => Promise; onOAuthLogin: (provider: string) => Promise; onForgotPassword: (email: string) => Promise; loading: boolean; error: string | null; onClearError: () => void; initialTab?: 'login' | 'register'; }) { const { t } = useTranslation(); const [view, setView] = useState(initialTab); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [resetSent, setResetSent] = useState(false); useEffect(() => { trackEvent('Auth Modal Open', { tab: initialTab }); }, []); // eslint-disable-line react-hooks/exhaustive-deps const switchView = useCallback( (newView: View) => { setView(newView); setResetSent(false); onClearError(); }, [onClearError] ); const handleSubmit = useCallback( async (e: React.FormEvent) => { e.preventDefault(); try { if (view === 'login') { await onLogin(email, password); onClose(); } else if (view === 'register') { await onRegister(email, password); onClose(); } else { await onForgotPassword(email); setResetSent(true); } } catch { // Error is handled by the hook } }, [view, email, password, onLogin, onRegister, onForgotPassword, onClose] ); const handleOAuth = useCallback( async (provider: string) => { try { await onOAuthLogin(provider); onClose(); } catch { // Error is handled by the hook } }, [onOAuthLogin, onClose] ); const title = view === 'login' ? t('auth.logIn') : view === 'register' ? t('auth.createAccount') : t('auth.resetPassword'); return (
{ if (e.target === e.currentTarget) onClose(); }} >
{/* Header */}

{title}

{/* Tabs (hidden in forgot view) */} {view !== 'forgot' && (
)}
{/* Value prop */} {view !== 'forgot' && (

{t('auth.valueProp')}

)} {/* OAuth buttons (hidden in forgot view) */} {view !== 'forgot' && ( <>
{/* Divider */}
{t('common.or')}
)} {/* Email form */}
setEmail(e.target.value)} required className="w-full px-3 py-2 text-sm rounded border border-warm-200 dark:border-warm-700 bg-white dark:bg-warm-800 text-navy-950 dark:text-white placeholder-warm-400 dark:placeholder-warm-500 outline-none focus:ring-2 ring-teal-400 dark:ring-teal-500" placeholder={t('auth.emailPlaceholder')} />
{view !== 'forgot' && (
setPassword(e.target.value)} required minLength={8} className="w-full px-3 py-2 text-sm rounded border border-warm-200 dark:border-warm-700 bg-white dark:bg-warm-800 text-navy-950 dark:text-white placeholder-warm-400 dark:placeholder-warm-500 outline-none focus:ring-2 ring-teal-400 dark:ring-teal-500" placeholder={view === 'register' ? t('auth.passwordPlaceholderRegister') : t('auth.passwordPlaceholderLogin')} /> {view === 'login' && ( )}
)} {view === 'forgot' && resetSent && (

{t('auth.resetSent')}

)} {error &&

{error}

} {!(view === 'forgot' && resetSent) && ( )} {view === 'forgot' && ( )}
); }