import React, { useState } from 'react'; import { supabase } from '../services/supabaseClient'; import { LoadingSpinner } from './LoadingSpinner'; import { XMarkIcon } from './icons/XMarkIcon'; import { GoogleIcon } from './icons/GoogleIcon'; import { GithubIcon } from './icons/GithubIcon'; import { PasswordStrength } from './PasswordStrength'; interface SignUpModalProps { isOpen: boolean; onClose: () => void; onSwitchToSignIn: () => void; } export const SignUpModal: React.FC = ({ isOpen, onClose, onSwitchToSignIn }) => { const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [confirmPassword, setConfirmPassword] = useState(''); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [message, setMessage] = useState(null); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); // Add password confirmation check if (password !== confirmPassword) { setError("Passwords do not match."); setLoading(false); return; } setError(null); setMessage(null); // Check if there is a current anonymous session const { data: { session } } = await supabase.auth.getSession(); if (session && session.user.is_anonymous) { // If the user is anonymous, upgrade their account instead of creating a new one. try { const { error } = await supabase.auth.updateUser({ email, password }); if (error) throw error; setMessage('Your account has been created! Check your email for a confirmation link.'); } catch (err: any) { setError(err.message || 'An unexpected error occurred during account upgrade.'); } finally { setLoading(false); } } else { // Standard sign-up flow for new users. try { const { error } = await supabase.auth.signUp({ email, password, options: { emailRedirectTo: window.location.href } }); if (error) throw error; setMessage('Check your email for the confirmation link!'); } catch (err: any) { setError(err.message || 'An unexpected error occurred.'); } finally { setLoading(false); } } }; const handleOAuthSignIn = async (provider: 'google' | 'github') => { setLoading(true); setError(null); const { error } = await supabase.auth.signInWithOAuth({ provider, options: { redirectTo: window.location.href, } }); if (error) { setError(error.message); setLoading(false); } }; if (!isOpen) return null; return (
e.stopPropagation()} >

Create an Account

to start personalizing your experience.

OR
setEmail(e.target.value)} required className="mt-1 block w-full px-3 py-2 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-brand-primary focus:border-brand-primary" placeholder="you@example.com" />
setPassword(e.target.value)} required className="mt-1 block w-full px-3 py-2 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-brand-primary focus:border-brand-primary" placeholder="••••••••" /> {password.length > 0 && }
setConfirmPassword(e.target.value)} required className="mt-1 block w-full px-3 py-2 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-brand-primary focus:border-brand-primary" placeholder="••••••••" />
{error &&

{error}

} {message &&

{message}

}

Already have an account?

); };