import {useEffect, useState} from 'react'; import { Alert, AlertDescription, Input, Label, Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@plunk/ui'; import {AlertCircle} from 'lucide-react'; import Link from 'next/link'; import {useDomains} from '../lib/hooks/useDomains'; import {useActiveProject} from '../lib/contexts/ActiveProjectProvider'; interface EmailDomainInputProps { value: string; onChange: (value: string) => void; id?: string; placeholder?: string; required?: boolean; label?: string; } export function EmailDomainInput({value, onChange, id, placeholder, required, label}: EmailDomainInputProps) { const {activeProject} = useActiveProject(); const {domains, isLoading} = useDomains(activeProject?.id); // Split the email into local part and domain const [localPart, setLocalPart] = useState(''); const [selectedDomain, setSelectedDomain] = useState(''); // Get verified domains only const verifiedDomains = domains?.filter(d => d.verified) || []; // Initialize from value useEffect(() => { if (value && value.includes('@')) { const [local, domain] = value.split('@'); setLocalPart(local ?? ''); // Check if the domain is in our verified list const domainPart = domain ?? ''; const matchingDomain = verifiedDomains.find(d => d.domain === domainPart); if (matchingDomain) { setSelectedDomain(domainPart); } else { // If domain not verified, keep it in the domain field setSelectedDomain(domainPart); } } else if (value) { // If no @ sign, treat entire value as local part setLocalPart(value); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [value]); // Initialize selected domain with first verified domain if none selected useEffect(() => { if (!selectedDomain && verifiedDomains.length > 0) { const firstDomain = verifiedDomains[0]; if (firstDomain) { setSelectedDomain(firstDomain.domain); } } // eslint-disable-next-line react-hooks/exhaustive-deps }, [selectedDomain]); // Update parent component when local part or domain changes const handleUpdate = (newLocal: string, newDomain: string) => { if (newLocal && newDomain) { onChange(`${newLocal}@${newDomain}`); } else if (newLocal) { onChange(newLocal); } else { onChange(''); } }; const handleLocalPartChange = (e: React.ChangeEvent) => { const newLocal = e.target.value; setLocalPart(newLocal); handleUpdate(newLocal, selectedDomain); }; const handleDomainChange = (newDomain: string) => { setSelectedDomain(newDomain); handleUpdate(localPart, newDomain); }; if (isLoading) { return (
{label && }
); } if (verifiedDomains.length === 0) { return (
{label && }

No verified domains

Please add and verify a domain in{' '} Settings → Domains {' '} before creating templates.

{/* Fallback to regular email input */} onChange(e.target.value)} placeholder={placeholder || 'email@example.com'} required={required} className="mt-2" />
); } return (
{label && }
@
); }