'use client' import { useState } from 'react' import { useTranslations } from 'next-intl' import { cn } from '@/lib/utils' import { Mail, Loader2, Send } from 'lucide-react' import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from '@/components/ui/dialog' import { Button } from '@/components/ui/button' import { Textarea } from '@/components/ui/textarea' import { useToast } from '@/components/ui/use-toast' import { submitFeedback } from '@/lib/support/submit-feedback' import { useCompanyOptional } from '@/contexts/CompanyContext' interface SupportLinkProps { variant?: 'inline' | 'muted' subject?: string children?: React.ReactNode className?: string } export function SupportLink({ variant = 'inline', subject, children, className, }: SupportLinkProps) { const t = useTranslations('support_link') const [open, setOpen] = useState(false) const [message, setMessage] = useState('') const [isSending, setIsSending] = useState(false) const [sent, setSent] = useState(false) const { toast } = useToast() const companyCtx = useCompanyOptional() if (companyCtx?.isSandbox) return null async function handleSubmit(e: React.FormEvent) { e.preventDefault() if (message.trim().length < 5) return setIsSending(true) const result = await submitFeedback({ subject, message: message.trim() }) setIsSending(false) if (result.ok) { setSent(true) setTimeout(() => { setOpen(false) setSent(false) setMessage('') }, 2000) } else { toast({ title: t('send_failed_title'), description: result.error || t('send_failed_fallback'), variant: 'destructive', }) } } function handleOpenChange(next: boolean) { setOpen(next) if (!next) { setSent(false) setMessage('') } } const trigger = variant === 'muted' ? ( {children ?? t('default_label')} ) : ( {children ?? t('default_label')} ) return ( {trigger} {t('dialog_title')} {t('dialog_description')} {sent ? ( {t('thanks')} ) : ( setMessage(e.target.value)} placeholder={t('placeholder')} className="min-h-[120px] resize-none" maxLength={5000} disabled={isSending} autoFocus /> {t('char_count', { count: message.length })} setOpen(false)} disabled={isSending} > {t('cancel')} {isSending ? ( <> {t('sending')} > ) : ( <> {t('send')} > )} )} ) }
{t('thanks')}
{t('char_count', { count: message.length })}