"use client"; import { useState, useTransition } from "react"; import { useTranslations } from "next-intl"; import { toast } from "sonner"; import { Icons } from "../shared/icons"; import { Button } from "../ui/button"; import { Drawer, DrawerClose, DrawerContent, DrawerHeader, DrawerTitle, } from "../ui/drawer"; import { Input } from "../ui/input"; import { EmailEditor } from "./EmailEditor"; 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: "", text: "", }); const [isPending, startTransition] = useTransition(); const t = useTranslations("Email"); const handleSendEmail = async () => { if (!emailAddress) { toast.error("No email address selected"); return; } console.log("sendForm", sendForm); // return; if (!sendForm.to || !sendForm.subject || !sendForm.html) { toast.error("Please fill in all required 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, text: sendForm.text, }), }); if (response.ok) { toast.success("Email sent successfully"); setIsOpen(false); setSendForm({ to: "", subject: "", html: "", text: "" }); onSuccess?.(); } else { toast.error("Failed to send email", { description: await response.text(), }); } } catch (error) { toast.error(error.message || "Error sending email"); } }); }; return ( <> {triggerButton ? (
setIsOpen(true)}>{triggerButton}
) : ( )} {t("Send Email")}{" "} {/* */}
setSendForm({ ...sendForm, subject: e.target.value }) } placeholder="Your subject" className="border-none" />
setSendForm({ ...sendForm, to: e.target.value }) } placeholder="recipient@example.com" className="border-none" />
setSendForm({ ...sendForm, html: e, text: t }) } />
); }