import { createContext, useContext, type ReactNode } from "react"; import { useQuery } from "@tanstack/react-query"; import { Mail, Paperclip } from "lucide-react"; import type { EmailMessage, EmailPublicationSummary, EmailThreadSummary, } from "@paperclipai/shared"; import { useEmailThread } from "@/hooks/useEmailThread"; import { issuesApi } from "@/api/issues"; const EmailContext = createContext(null); export function EmailThreadProvider({ companyId, issueId, children, }: { companyId: string; issueId: string; children: ReactNode; }) { const thread = useEmailThread(companyId, issueId); return ( {children} ); } export function useEmailComment(commentId: string) { const thread = useContext(EmailContext); const message = thread?.messages.find((m) => m.commentId === commentId); return message && thread ? ( p.providerMessageId === message.providerMessageId, )} issueId={thread.issueId} /> ) : null; } export function EmailMessageCard({ message, publication, issueId, contextNotice = false, }: { contextNotice?: boolean; message: EmailMessage; publication?: EmailPublicationSummary; issueId: string; }) { const attachments = useQuery({ queryKey: ["email-attachments", issueId], queryFn: () => issuesApi.listAttachments(issueId), enabled: message.attachmentIds.length > 0, }); return (
{message.direction === "inbound" ? "Email received" : "Email sent"} {new Date(message.timestamp).toLocaleString()}

{message.from} {message.direction === "inbound" && ( External )}

To: {message.to.join(", ")}

{!!message.cc?.length && (

Cc: {message.cc.join(", ")}

)}

{message.subject}

{message.text || "(No text body)"}
{!!message.attachmentIds.length && (
{message.attachmentIds.map((id) => { const attachment = attachments.data?.find((a) => a.id === id); return ( {attachment?.originalFilename ?? "Open attachment"} ); })}
)}
Email details
{!!message.bcc?.length &&

Bcc: {message.bcc.join(", ")}

}

Message ID: {message.providerMessageId}

{message.fullText !== message.text && (
{message.fullText}
)}
{publication && (

{publication.outcome === "delivered" ? "Delivered" : publication.outcome === "failed" ? "Delivery failed" : publication.outcome === "uncertain" ? "Delivery uncertain" : publication.outcome === "queued" ? "Queued" : "Sent"} {publication.error ? ` ยท ${publication.error}` : ""}

)} {attachments.error && (

Attachments could not be loaded.

)}
); }