"use client" import { useMemo } from "react" import { Pencil, Star, X, Mail, Phone, Building2, MapPin, Cake, FileText, MessageSquare, Video, } from "lucide-react" import { Button } from "@/components/ui/button" import { ScrollArea } from "@/components/ui/scroll-area" import { useContactsStore } from "@/lib/contacts/contacts-store" import { useContactsList } from "@/lib/contacts/use-contacts-list" import { fullContactDisplayName } from "@/lib/contacts/types" import { avatarColor, senderInitial } from "@/lib/sender-display" import { useMailSearch } from "@/lib/api/hooks/use-mail-queries" import { useComposeActions } from "@/lib/compose-context" import { useNavStore } from "@/lib/stores/nav-store" import { CONTACTS_HEADING_TEXT, CONTACTS_MUTED_TEXT, CONTACTS_PANEL_DIVIDER_CLASS, CONTACTS_PANEL_HEADER_CLASS, CONTACTS_PANEL_ICON_BTN_CLASS, CONTACTS_PANEL_MUTED_ICON_CLASS, CONTACTS_PANEL_PRIMARY_ACTION_CLASS, CONTACTS_PANEL_ROW_CLASS, CONTACTS_PANEL_SECONDARY_ICON_BTN_CLASS, CONTACTS_PANEL_SHELL_CLASS, CONTACTS_PANEL_TAG_CLASS, } from "@/lib/contacts-chrome-classes" import { cn } from "@/lib/utils" import { ContactsPanelLogo } from "./contacts-panel-logo" interface ContactDetailViewProps { contactId: string | null } const FRENCH_MONTHS = [ "janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre", ] function formatBirthday(b: { day?: number; month?: number; year?: number }): string { const parts: string[] = [] if (b.day) parts.push(String(b.day)) if (b.month) parts.push(FRENCH_MONTHS[b.month - 1] ?? "") if (b.year) parts.push(String(b.year)) return parts.join(" ") } function formatEmailDate(iso: string): string { const d = new Date(iso) const now = new Date() const diffDays = Math.floor((now.getTime() - d.getTime()) / 86_400_000) if (diffDays === 0) return "Aujourd'hui" if (diffDays === 1) return "Hier" if (diffDays < 7) return `Il y a ${diffDays} jours` return d.toLocaleDateString("fr-FR", { day: "numeric", month: "short", year: "numeric" }) } export function ContactDetailView({ contactId }: ContactDetailViewProps) { const { setView, showContactsList, closePanel } = useContactsStore() const { contacts } = useContactsList() const { openComposeWithInitial } = useComposeActions() const labelRows = useNavStore((s) => s.labelRows) const contact = contacts.find((c) => c.id === contactId) const primaryContactEmail = contact?.emails[0]?.value const { data: searchResult } = useMailSearch( primaryContactEmail ? { from: primaryContactEmail } : null ) const recentInteractions = useMemo(() => { if (!searchResult?.data) return [] return searchResult.data.slice(0, 10).map((msg) => ({ id: msg.id, subject: msg.subject, preview: msg.snippet, date: msg.date, })) }, [searchResult]) if (!contact) { return (
Contact introuvable
) } const displayName = fullContactDisplayName(contact) const name = displayName || contact.emails[0]?.value || contact.phones[0]?.value || "?" const color = avatarColor(name) const initial = senderInitial(name) const primaryEmail = contact.emails[0]?.value return (
{/* Header */}
{/* Avatar + Name */}
{contact.avatarUrl ? ( {name} ) : (
{initial}
)}

{name}

{contact.company && (

{contact.jobTitle ? `${contact.jobTitle} — ` : ""} {contact.company}

)} {contact.labels && contact.labels.length > 0 && (
{contact.labels.map((labelId) => { const row = labelRows.find((r) => r.id === labelId) return ( {row && ( )} {row?.label ?? labelId} ) })}
)}
{/* Quick actions */} {primaryEmail && (
)} {/* Contact details */}
{contact.emails.length > 0 && ( }> {contact.emails.map((e, i) => (

{e.value}

{e.label}

))}
)} {contact.phones.length > 0 && ( }> {contact.phones.map((p, i) => (

{p.value}

{p.label}

))}
)} {contact.company && ( }>

{contact.company}

{contact.department && (

{contact.department}

)} {contact.jobTitle && (

{contact.jobTitle}

)}
)} {contact.addresses && contact.addresses.length > 0 && ( }> {contact.addresses.map((addr, i) => (

{[addr.street, [addr.postalCode, addr.city].filter(Boolean).join(" "), addr.region, addr.country] .filter(Boolean) .join(", ")}

{addr.label}

))}
)} {contact.birthday && (contact.birthday.day || contact.birthday.month) && ( }>

{formatBirthday(contact.birthday)}

)} {contact.notes && ( }>

{contact.notes}

)}
{/* Recent interactions */} {recentInteractions.length > 0 && (

Interactions récentes

{recentInteractions.map((email) => (

{email.subject}

{email.preview}

{formatEmailDate(email.date)}

))}
)}
) } function DetailSection({ icon, children, }: { icon: React.ReactNode children: React.ReactNode }) { return (
{icon}
{children}
) }