"use client" import { useMemo, useState } from "react" import toast from "react-hot-toast" import { Button } from "../../ui/button" import { Input } from "../../ui/input" import { Textarea } from "../../ui/textarea" type SubjectOption = 'Question - General' | 'Question - Billing' | 'Feature Recommendation' | 'Other' export function SupportForm({ session, productLabel = 'KYD Self-check', }: { session?: { user?: { fullName?: string | null; name?: string | null; email?: string | null } | null } | null productLabel?: string }) { const [subject, setSubject] = useState('Question - General') const [description, setDescription] = useState('') const [file, setFile] = useState(null) const [isSubmitting, setIsSubmitting] = useState(false) const [isSent, setIsSent] = useState(false) const nameVal = useMemo(() => (session?.user?.fullName || session?.user?.name || ''), [session]) const emailVal = useMemo(() => (session?.user?.email || ''), [session]) const productVal = productLabel const onFileChange: React.ChangeEventHandler = (e) => { const f = e.target.files?.[0] || null if (!f) { setFile(null); return } const allowed = new Set(['image/png', 'image/jpeg', 'image/webp']) if (!allowed.has(f.type)) { toast.error('Only PNG, JPG, or WEBP allowed'); return } if (f.size > 5 * 1024 * 1024) { toast.error('Max file size is 5MB'); return } setFile(f) } async function handleSubmit(e: React.FormEvent) { e.preventDefault() if (!session) { toast.error('Please sign in to submit.'); return } if (!description.trim()) { toast.error('Please add a description.'); return } setIsSubmitting(true) try { let screenshotKey: string | undefined if (file) { const mime = file.type const body = JSON.stringify({ filename: file.name, mime }) const res = await fetch('/api/support/presign', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body }) if (!res.ok) throw new Error('Failed to prepare upload') const { uploadUrl, key } = await res.json() const up = await fetch(uploadUrl, { method: 'PUT', headers: { 'Content-Type': mime }, body: file }) if (!up.ok) throw new Error('Upload failed') screenshotKey = key } const resp = await fetch('/api/support', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ subject, description, screenshotKey }) }) if (!resp.ok) { const t = await resp.json().catch(() => ({})) throw new Error(t?.error || 'Failed to submit support request') } toast.success('Sent to support. Thank you!') setDescription('') setFile(null) setSubject('Question - General') setIsSent(true) } catch (err) { const e2 = err as Error toast.error(e2.message || 'Something went wrong') } finally { setIsSubmitting(false) } } if (isSent) { return (

Thanks! Your message was sent.

Our team will review and get back to you at your account email.

) } return (

Contact Support

Description