"use client" import { useState } from "react" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog" import { Plus, RefreshCw } from "lucide-react" import { useToast } from "@/components/ui/use-toast" import { nanoid } from "nanoid" import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group" import { Label } from "@/components/ui/label" import { EXPIRY_OPTIONS } from "@/types/email" import { EMAIL_CONFIG } from "@/config" interface CreateDialogProps { onEmailCreated: () => void } export function CreateDialog({ onEmailCreated }: CreateDialogProps) { const [open, setOpen] = useState(false) const [loading, setLoading] = useState(false) const [emailName, setEmailName] = useState("") const [expiryTime, setExpiryTime] = useState(EXPIRY_OPTIONS[1].value.toString()) // Default to 24 hours const { toast } = useToast() const generateRandomName = () => setEmailName(nanoid(8)) const createEmail = async () => { if (!emailName.trim()) { toast({ title: "错误", description: "请输入邮箱名", variant: "destructive" }) return } setLoading(true) try { const response = await fetch("/api/emails/generate", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name: emailName, expiryTime: parseInt(expiryTime) // 确保转换为数字 }) }) if (response.status === 409) { toast({ title: "错误", description: "该邮箱名已被使用", variant: "destructive" }) return } if (response.status === 403) { toast({ title: "错误", description: "已达到最大邮箱数量限制", variant: "destructive" }) return } if (!response.ok) throw new Error("Failed to create email") toast({ title: "成功", description: "已创建新的临时邮箱" }) onEmailCreated() setOpen(false) setEmailName("") } catch { toast({ title: "错误", description: "创建邮箱失败", variant: "destructive" }) } finally { setLoading(false) } } return ( 创建新的临时邮箱
setEmailName(e.target.value)} placeholder="输入邮箱名" className="flex-1" />
{EXPIRY_OPTIONS.map((option) => (
))}
完整邮箱地址将为: {emailName ? `${emailName}@${EMAIL_CONFIG.DOMAIN}` : "..."}
) }