"use client"; import { useTranslations } from "next-intl"; import { formatDate } from "@/lib/utils"; import { Email } from "@/lib/jmap/types"; import { cn } from "@/lib/utils"; import { Avatar } from "@/components/ui/avatar"; import { Paperclip, Star, Circle, CheckSquare, Square, Tag } from "lucide-react"; import { useEmailStore } from "@/stores/email-store"; import { useSettingsStore, KEYWORD_PALETTE } from "@/stores/settings-store"; import { useAuthStore } from "@/stores/auth-store"; import { useEmailDrag } from "@/hooks/use-email-drag"; import { EmailIdentityBadge } from "./email-identity-badge"; import { getEmailColorTag } from "@/lib/thread-utils"; interface EmailListItemProps { email: Email; selected?: boolean; onClick?: () => void; onContextMenu?: (e: React.MouseEvent, email: Email) => void; } export function EmailListItem({ email, selected, onClick, onContextMenu }: EmailListItemProps) { const t = useTranslations('email_viewer'); const { selectedEmailIds, toggleEmailSelection, selectRangeEmails, selectedMailbox, clearSelection } = useEmailStore(); const showPreview = useSettingsStore((state) => state.showPreview); const emailKeywords = useSettingsStore((state) => state.emailKeywords); const { identities } = useAuthStore(); const isChecked = selectedEmailIds.has(email.id); const isUnread = !email.keywords?.$seen; const isStarred = email.keywords?.$flagged; const isImportant = email.keywords?.["$important"]; const sender = email.from?.[0]; // Resolve color tag using keyword definitions from settings const colorTagId = getEmailColorTag(email.keywords); const keywordDef = colorTagId ? emailKeywords.find(k => k.id === colorTagId) : null; const colorTag = keywordDef ? KEYWORD_PALETTE[keywordDef.color]?.bg ?? null : null; // Drag and drop functionality const { dragHandlers, isDragging } = useEmailDrag({ email, sourceMailboxId: selectedMailbox, }); const handleCheckboxClick = (e: React.MouseEvent) => { e.stopPropagation(); toggleEmailSelection(email.id); }; const handleContextMenu = (e: React.MouseEvent) => { onContextMenu?.(e, email); }; return (
{ if (e.ctrlKey || e.metaKey) { e.preventDefault(); toggleEmailSelection(email.id); } else if (e.shiftKey) { e.preventDefault(); selectRangeEmails(email.id); } else { if (selectedEmailIds.size > 0) clearSelection(); onClick?.(); } }} onContextMenu={handleContextMenu} style={{ minHeight: 'var(--list-item-height)' }} >
{/* Checkbox - only visible when in selection mode */} {selectedEmailIds.size > 0 && ( )} {/* Unread indicator */} {isUnread && (
)} {/* Avatar */} {/* Content */}
{/* First Line: Sender and Date */}
{sender?.name || sender?.email || "Unknown"}
{isStarred && ( )} {isImportant && ( Important )} {email.hasAttachment && ( )}
{keywordDef && ( {keywordDef.label} )} {formatDate(email.receivedAt)}
{/* Second Line: Subject */}
{email.subject || t('no_subject')}
{/* Third Line: Preview (controlled by showPreview setting) */} {showPreview && (

{email.preview || "No preview available"}

)}
); }