import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; import { apiClient } from "../lib/apiClient"; import { Mail, Plus, RefreshCw, Trash2, CheckCircle2, AlertCircle, Server, Flame, Sliders, ShieldCheck } from "lucide-react"; import { useState } from "react"; import StatusBadge from "../components/StatusBadge"; import FormDrawer from "../components/FormDrawer"; import ConfirmModal from "../components/ConfirmModal"; import toast from "react-hot-toast"; export default function EmailAccounts() { const queryClient = useQueryClient(); const [isAddOpen, setIsAddOpen] = useState(false); const [activeSetupTab, setActiveSetupTab] = useState<"smtp" | "google" | "outlook">("smtp"); const [selectedForDelete, setSelectedForDelete] = useState(null); const { data: accounts = [], isLoading, refetch } = useQuery({ queryKey: ["email-accounts"], queryFn: async () => { const res = await apiClient.get("/email-accounts"); return res.data; }, }); const reconnectMutation = useMutation({ mutationFn: async (id: string) => { const res = await apiClient.post("/email-accounts/reconnect", { account_id: id }); return res.data; }, onSuccess: () => { toast.success("Mailbox connection verified and active"); refetch(); }, onError: () => { toast.error("Failed to verify mailbox connection"); } }); const deleteMutation = useMutation({ mutationFn: async (id: string) => { const res = await apiClient.delete(`/email-accounts/${id}`); return res.data; }, onSuccess: () => { toast.success("Email account disconnected"); setSelectedForDelete(null); refetch(); }, }); const getProviderIcon = (provider: string) => { switch (provider?.toLowerCase()) { case "google": case "gmail": return G; case "microsoft": case "outlook": return M; default: return ; } }; return (
{/* Header */}

Sending Inboxes & Mailboxes

Connect and manage sending infrastructure across Google Workspace, Microsoft 365, and custom SMTP/IMAP.

{/* Overview Cards */}
Total Sending Capacity
{accounts.reduce((sum: number, a: any) => sum + (a.daily_limit || 0), 0)} / day
{accounts.length} connected mailboxes
Active in Warmup
{accounts.filter((a: any) => a.warmup_enabled).length} of {accounts.length}
Autonomous ramp-up active
Network Deliverability
{accounts.length ? `${(accounts.reduce((sum: number, account: any) => sum + (account.warmup_score || 0), 0) / accounts.length).toFixed(1)}%` : "—"}
All DNS / SPF / DKIM valid
{/* Accounts Table */}
{accounts.map((acc: any) => { const usage = acc.sent_today || 0; const limit = acc.daily_limit || 0; const percentage = limit ? Math.min(100, Math.round((usage / limit) * 100)) : 0; return ( ); })}
Account / Sender Provider Status Daily Limit & Usage Warmup Actions
{acc.from_name}
{acc.from_email}
{getProviderIcon(acc.provider)} {acc.provider}
{usage} sent {limit} limit
90 ? "bg-amber-500" : "bg-indigo-500" }`} style={{ width: `${percentage}%` }} />
{acc.warmup_enabled ? "Ramping Up" : "Disabled"}
{/* Delete Confirmation */} setSelectedForDelete(null)} onConfirm={() => selectedForDelete && deleteMutation.mutate(selectedForDelete)} title="Disconnect Mailbox" description="Are you sure you want to disconnect this email account? Active campaigns using this mailbox will cease dispatching from it." confirmText="Disconnect" /> {/* Add Mailbox Drawer */} setIsAddOpen(false)} title="Connect Sending Mailbox" subtitle="Add Google Workspace, Microsoft 365, or Custom SMTP/IMAP credentials." >
{/* Provider Selector Tabs */}
{activeSetupTab === "smtp" && (
{ e.preventDefault(); const form = e.target as HTMLFormElement; const data = { from_name: (form.elements.namedItem("from_name") as HTMLInputElement).value, from_email: (form.elements.namedItem("from_email") as HTMLInputElement).value, smtp_host: (form.elements.namedItem("smtp_host") as HTMLInputElement).value, smtp_port: Number((form.elements.namedItem("smtp_port") as HTMLInputElement).value), smtp_username: (form.elements.namedItem("smtp_username") as HTMLInputElement).value, smtp_password: (form.elements.namedItem("smtp_password") as HTMLInputElement).value, imap_host: (form.elements.namedItem("imap_host") as HTMLInputElement).value, imap_port: Number((form.elements.namedItem("imap_port") as HTMLInputElement).value), imap_username: (form.elements.namedItem("imap_username") as HTMLInputElement).value, imap_password: (form.elements.namedItem("imap_password") as HTMLInputElement).value, daily_limit: Number((form.elements.namedItem("daily_limit") as HTMLInputElement).value), provider: "smtp", }; try { await apiClient.post("/email-accounts/smtp", data); toast.success("SMTP Mailbox connected successfully"); setIsAddOpen(false); refetch(); } catch (err: any) { toast.error(err.response?.data?.error || "Failed to connect SMTP account"); } }} className="space-y-4" >

SMTP Configuration (Outbound)

IMAP Configuration (Inbox & Replies)

)} {(activeSetupTab === "google" || activeSetupTab === "outlook") && (
{activeSetupTab === "google" ? "Google Workspace" : "Microsoft 365"}

Authorize direct OAuth access to dispatch outbound sequences with verified sender tokens.

)}
); }