"use client"; import { useState, useCallback, useEffect } from "react"; import { getWelcomeSequence, resendWelcomeEmail, type WelcomeSequenceEntry } from "@/actions/email-automation/welcome-sequence"; type Props = { brandId: string }; const STATUS_LABELS: Record = { active: { en: "Active", color: "bg-blue-900/40 text-blue-400 border border-blue-800" }, completed: { en: "Completed", color: "bg-emerald-900/40 text-emerald-400 border border-emerald-800" }, unsubscribed: { en: "Unsubscribed", color: "bg-red-900/40 text-red-400 border border-red-800" }, bounced: { en: "Bounced", color: "bg-amber-900/40 text-amber-400 border border-amber-800" }, }; const STEP_LABELS: Record = { 0: "Enrolled", 1: "Welcome (1h)", 2: "How it works (24h)", 3: "First order (48h)", 4: "Final (72h)", }; export default function WelcomeSequenceDashboard({ brandId }: Props) { const [entries, setEntries] = useState([]); const [stats, setStats] = useState({ total: 0, completed: 0, active: 0, unsubscribed: 0 }); const [loading, setLoading] = useState(false); const [filter, setFilter] = useState<"all" | "active" | "completed">("all"); const [page, setPage] = useState(0); const PAGE_SIZE = 25; const [resending, setResending] = useState(null); const handleResend = async (entryId: string) => { setResending(entryId); await resendWelcomeEmail(entryId, brandId); await load(); setResending(null); }; const load = useCallback(async () => { setLoading(true); const result = await getWelcomeSequence(brandId); if (result.success) { setEntries(result.entries); setStats(result.stats); } setLoading(false); }, [brandId]); useEffect(() => { setPage(0); }, [filter]); const filtered = entries.filter(e => { if (filter === "active") return e.status === "active"; if (filter === "completed") return e.status === "completed"; return true; }); const paginatedEntries = filtered.slice(page * PAGE_SIZE, (page + 1) * PAGE_SIZE); const totalPages = Math.ceil(filtered.length / PAGE_SIZE); return (
{/* Stats */}
{[ { label: "Total", value: stats.total, color: "text-zinc-100" }, { label: "Active", value: stats.active, color: "text-blue-400" }, { label: "Completed", value: stats.completed, color: "text-emerald-400" }, { label: "Unsubscribed", value: stats.unsubscribed, color: "text-red-400" }, ].map(s => (

{s.value}

{s.label}

))}
{/* Filter + refresh + pagination */}
{(["all", "active", "completed"] as const).map(f => ( ))}
{totalPages > 1 && (
{page + 1}/{totalPages}
)}
{/* Table */} {filtered.length === 0 ? (

No entries{filter !== "all" ? ` (${filter})` : ""}

) : (
{paginatedEntries.map(e => { const meta = STATUS_LABELS[e.status] ?? { en: e.status, color: "bg-zinc-800 text-zinc-400" }; return ( ); })}
Contact Language Step Status Enrolled Last Email Actions

{e.contact_name ?? "—"}

{e.contact_email}

{e.locale} {STEP_LABELS[e.sequence_step] ?? e.sequence_step} {e.next_email_at && e.status === "active" && ( Next: {new Date(e.next_email_at).toLocaleString()} )} {meta.en} {new Date(e.created_at).toLocaleDateString()} {e.last_email_sent_at ? new Date(e.last_email_sent_at).toLocaleString() : "—"} {e.status !== "unsubscribed" && ( )}
)}
); }