import { useState, useEffect } from 'react'; import { useForm } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; import { Send } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Textarea } from '@/components/ui/textarea'; import { Label } from '@/components/ui/label'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import { TurnstileCaptcha } from '@/components/auth/TurnstileCaptcha'; import { supabase } from '@/lib/supabaseClient'; import { handleError, handleSuccess } from '@/lib/errorHandler'; import { logger } from '@/lib/logger'; import { contactFormSchema, contactCategories, type ContactFormData } from '@/lib/contactValidation'; import { useAuth } from '@/hooks/useAuth'; export function ContactForm() { const { user } = useAuth(); const [isSubmitting, setIsSubmitting] = useState(false); const [captchaToken, setCaptchaToken] = useState(''); const [captchaKey, setCaptchaKey] = useState(0); const [userName, setUserName] = useState(''); const [userEmail, setUserEmail] = useState(''); // Fetch user profile if logged in useEffect(() => { const fetchUserProfile = async () => { if (user) { setUserEmail(user.email || ''); const { data: profile } = await supabase .from('profiles') .select('display_name, username') .eq('user_id', user.id) .single(); if (profile) { setUserName(profile.display_name || profile.username || ''); } } }; fetchUserProfile(); }, [user]); const { register, handleSubmit, setValue, watch, reset, formState: { errors }, } = useForm({ resolver: zodResolver(contactFormSchema), defaultValues: { name: userName, email: userEmail, subject: '', category: 'general', message: '', captchaToken: '', }, }); // Update form when user data loads useEffect(() => { if (userName) { setValue('name', userName); } if (userEmail) { setValue('email', userEmail); } }, [userName, userEmail, setValue]); const onCaptchaSuccess = (token: string) => { setCaptchaToken(token); setValue('captchaToken', token); }; const onCaptchaError = () => { setCaptchaToken(''); setValue('captchaToken', ''); handleError( new Error('CAPTCHA verification failed'), { action: 'verify_captcha' } ); }; const onCaptchaExpire = () => { setCaptchaToken(''); setValue('captchaToken', ''); }; const onSubmit = async (data: ContactFormData) => { if (!captchaToken) { handleError( new Error('Please complete the CAPTCHA'), { action: 'submit_contact_form' } ); return; } setIsSubmitting(true); try { logger.info('Submitting contact form', { category: data.category }); const { data: result, error } = await supabase.functions.invoke( 'send-contact-message', { body: { name: data.name, email: data.email, subject: data.subject, message: data.message, category: data.category, captchaToken: data.captchaToken, }, } ); if (error) { throw error; } handleSuccess( 'Message Sent!', "Thank you for contacting us. We'll respond within 24-48 hours." ); // Reset form reset({ name: userName, email: userEmail, subject: '', category: 'general', message: '', captchaToken: '', }); // Reset CAPTCHA setCaptchaToken(''); setCaptchaKey((prev) => prev + 1); } catch (error) { handleError(error, { action: 'submit_contact_form', metadata: { category: data.category }, }); // Reset CAPTCHA on error setCaptchaToken(''); setCaptchaKey((prev) => prev + 1); } finally { setIsSubmitting(false); } }; return (
{/* Name */}
{errors.name && (

{errors.name.message}

)}
{/* Email */}
{errors.email && (

{errors.email.message}

)}
{/* Category */}
{errors.category && (

{errors.category.message}

)}
{/* Subject */}
{errors.subject && (

{errors.subject.message}

)}
{/* Message */}