import { useState } from 'react'; import { EmailApi } from '../api/client.js'; const fmtDate = (iso) => { const d = new Date(iso); const now = new Date(); const diffDays = (now - d) / 86400000; if (diffDays < 1) return d.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }); if (diffDays < 7) return d.toLocaleDateString([], { weekday: 'short' }); return d.toLocaleDateString([], { month: 'short', day: 'numeric' }); }; const fmtSize = (b) => { if (b < 1024) return `${b} B`; if (b < 1048576) return `${(b / 1024).toFixed(0)} KB`; return `${(b / 1048576).toFixed(1)} MB`; }; export default function EmailRow({ email: initial, onRemove, selected, onToggleSelect, focused }) { const [email, setEmail] = useState(initial); const [acting, setActing] = useState(false); const act = (fn, patch) => async (e) => { e.stopPropagation(); if (acting) return; setActing(true); try { await fn(email.id); setEmail((prev) => ({ ...prev, ...patch })); } finally { setActing(false); } }; const handleUnsub = async (e) => { e.stopPropagation(); if (acting) return; setActing(true); try { const res = await EmailApi.unsubscribe(email.id); if (res.method === 'mailto') { window.location.href = res.target; } else { setEmail((prev) => ({ ...prev, _unsubDone: true })); } } finally { setActing(false); } }; const handleTrash = async (e) => { e.stopPropagation(); if (acting) return; setActing(true); try { await EmailApi.trash(email.id); onRemove?.(email.id); } finally { setActing(false); } }; const openInGmail = () => window.open( `https://mail.google.com/mail/u/0/#all/${email.gmailMessageId}`, '_blank', 'noopener,noreferrer' ); return ( {onToggleSelect && ( e.stopPropagation()}> onToggleSelect(email.id)} /> )} {email.isUnread && } {email.senderDisplayName || email.senderAddress} {email.subject || '(no subject)'} {email.snippet && — {email.snippet}} {email.hasAttachments && 📎} {email.sizeEstimateBytes > 1048576 && {fmtSize(email.sizeEstimateBytes)}} {fmtDate(email.sentAtUtc)} e.stopPropagation()}> {email.hasListUnsubscribe && ( )} ); }