"use client"; import { useState, useTransition } from "react"; import dynamic from "next/dynamic"; import { toast } from "sonner"; import { Icons } from "../shared/icons"; import { Button } from "../ui/button"; import { Drawer, DrawerClose, DrawerContent, DrawerFooter, DrawerHeader, DrawerTitle, } from "../ui/drawer"; import { Input } from "../ui/input"; import "react-quill/dist/quill.snow.css"; import { useTranslations } from "next-intl"; const ReactQuill = dynamic(() => import("react-quill"), { ssr: false }); interface SendEmailModalProps { className?: string; emailAddress: string | null; triggerButton?: React.ReactNode; // 自定义触发按钮 onSuccess?: () => void; // 发送成功后的回调 } export function SendEmailModal({ className, emailAddress, triggerButton, onSuccess, }: SendEmailModalProps) { const [isOpen, setIsOpen] = useState(false); const [sendForm, setSendForm] = useState({ to: "", subject: "", html: "" }); const [isPending, startTransition] = useTransition(); const t = useTranslations("Email"); const handleSendEmail = async () => { if (!emailAddress) { toast.error("No email address selected"); return; } if (!sendForm.to || !sendForm.subject || !sendForm.html) { toast.error("Please fill in all fields"); return; } startTransition(async () => { try { const response = await fetch("/api/email/send", { method: "POST", body: JSON.stringify({ from: emailAddress, to: sendForm.to, subject: sendForm.subject, html: sendForm.html, }), }); if (response.ok) { toast.success("Email sent successfully"); setIsOpen(false); setSendForm({ to: "", subject: "", html: "" }); onSuccess?.(); } else { toast.error("Failed to send email", { description: await response.text(), }); } } catch (error) { toast.error(error.message || "Error sending email"); } }); }; return ( <> {triggerButton ? (