import { EmailMessageCard } from "./EmailMessageCard"; import { useState } from "react"; import { useMutation, useQueryClient } from "@tanstack/react-query"; import { emailApi } from "@/api/email"; import { useEmailThread } from "@/hooks/useEmailThread"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import type { EmailPublicationSummary } from "@paperclipai/shared"; // Email actions belong to the agent's task conversation. Only surface mail // without a task comment yet and delivery outcomes that need attention here. export function EmailTaskActivity({ companyId, issueId, }: { companyId: string; issueId: string; }) { const cache = useQueryClient(); const threadKey = ["email-thread", companyId, issueId]; const queryEnabled = Boolean(companyId && issueId) && !issueId.startsWith("chat:"); const thread = useEmailThread(companyId, issueId); if (!queryEnabled) return null; const data = thread.data; const messages = data?.messages.filter((m) => !m.commentId) ?? []; const publications = data?.publications.filter( (p) => !p.providerMessageId || p.outcome === "uncertain", ) ?? []; if (!thread.error && !messages.length && !publications.length) return null; return (
{thread.error && (

{thread.error.message}

)} {messages.map((m) => ( p.providerMessageId === m.providerMessageId, )} /> ))} {publications.map((p) => ( { void cache.invalidateQueries({ queryKey: threadKey }); }} /> ))}
); } function EmailDelivery({ companyId, publication: p, onResolved, }: { companyId: string; publication: EmailPublicationSummary; onResolved: () => void; }) { const [messageId, setMessageId] = useState(""); const resolve = useMutation({ mutationFn: (outcome: "sent" | "failed") => emailApi.resolve(companyId, p.id, outcome, messageId || undefined), onSuccess: onResolved, }); return (
{p.request && !p.providerMessageId && (

{p.request.subject ?? "Email reply"}

{p.request.to &&

To: {p.request.to.join(", ")}

}
{p.request.text}
)}

Email {p.outcome} {p.error ? ` — ${p.error}` : ""}

{p.outcome === "uncertain" && (
Resolve delivery after checking AgentMail

Confirm the outcome in AgentMail before resolving. This action does not resend.

setMessageId(e.target.value)} placeholder="Provider message ID" />
{resolve.error && (

{resolve.error.message}

)}
)}
); }