'use client'; import { useTranslations } from 'next-intl'; import { Mail, Tag } from 'lucide-react'; import { cn } from '@/lib/utils'; import type { Email, Identity } from '@/lib/jmap/types'; import { parseSubAddress } from '@/lib/sub-addressing'; interface EmailIdentityBadgeProps { email: Email; identities: Identity[]; compact?: boolean; className?: string; } export function EmailIdentityBadge({ email, identities, compact = false, className, }: EmailIdentityBadgeProps) { const t = useTranslations('identities.badge'); const fromAddress = email.from?.[0]?.email; if (!fromAddress) return null; // Parse the from address to check for sub-addressing const parsedFrom = parseSubAddress(fromAddress); // Find matching identity (email sent BY the user) const matchingIdentity = identities.find( (identity) => identity.email === fromAddress || identity.email === `${parsedFrom.baseUser}@${parsedFrom.domain}` ); // Check if email was sent TO a sub-address (received email) let receivedToTag: string | null = null; if (!matchingIdentity) { // Check all TO addresses for sub-address tags matching user's identities for (const recipient of email.to || []) { const parsedTo = parseSubAddress(recipient.email); if (parsedTo.tag) { // Check if this base email matches any of the user's identities const matchingToIdentity = identities.find( (identity) => identity.email === `${parsedTo.baseUser}@${parsedTo.domain}` ); if (matchingToIdentity) { receivedToTag = parsedTo.tag; break; } } } } // Determine which tag to display (sent or received) const displayTag = matchingIdentity ? parsedFrom.tag : receivedToTag; // Don't show badge if not from user's identity and not to user's sub-address if (!matchingIdentity && !receivedToTag) return null; if (compact) { // Compact view for email list if (displayTag) { return (
+{displayTag}
); } if ( matchingIdentity && matchingIdentity.name && matchingIdentity.name !== matchingIdentity.email && matchingIdentity.name !== fromAddress ) { return (
{matchingIdentity.name}
); } return null; } // Full view for email viewer - now shows compact badges only return (
{/* Sub-address tag badge */} {displayTag && (
{t('subaddress_tag', { tag: displayTag })}
)} {/* Identity badge (only if identity has a name and no sub-address tag) */} {!displayTag && matchingIdentity && matchingIdentity.name && matchingIdentity.name !== matchingIdentity.email && matchingIdentity.name !== fromAddress && (
{t('identity_short', { name: matchingIdentity.name })}
)}
); }