"use client" import { useMemo, useState } from "react" import { Button } from "@/components/ui/button" import { useContactsStore, type MergeSuggestion } from "@/lib/contacts/contacts-store" import { findDuplicatePairs, type DuplicateMatchReason } from "@/lib/contacts/duplicate-detection" import { fullContactDisplayName } from "@/lib/contacts/types" import { avatarColor, senderInitial } from "@/lib/sender-display" import { AddCoordinatesView } from "./add-coordinates-view" type SubView = "merge" | "coordinates" const REASON_LABELS: Record = { email: "Même adresse e-mail", phone: "Même numéro de téléphone", name: "Nom similaire", } export function MergeDuplicatesView() { const [subView, setSubView] = useState("merge") const contacts = useContactsStore((s) => s.contacts) const ignoredMergePairs = useContactsStore((s) => s.ignoredMergePairs) const mergeContacts = useContactsStore((s) => s.mergeContacts) const ignoreMergePair = useContactsStore((s) => s.ignoreMergePair) const getCoordinateSuggestions = useContactsStore((s) => s.getCoordinateSuggestions) const mergeSuggestions = useMemo( () => findDuplicatePairs(contacts, new Set(ignoredMergePairs)), [contacts, ignoredMergePairs] ) const coordSuggestions = useMemo( () => getCoordinateSuggestions(), [getCoordinateSuggestions, contacts] ) const [mergingAll, setMergingAll] = useState(false) function handleMerge(suggestion: MergeSuggestion) { mergeContacts(suggestion.contactA.id, suggestion.contactB.id) } function handleIgnore(suggestion: MergeSuggestion) { ignoreMergePair(suggestion.contactA.id, suggestion.contactB.id) } function handleMergeAll() { setMergingAll(true) try { let pairs = findDuplicatePairs( useContactsStore.getState().contacts, new Set(useContactsStore.getState().ignoredMergePairs) ) while (pairs.length > 0) { const { contactA, contactB } = pairs[0] mergeContacts(contactA.id, contactB.id) const state = useContactsStore.getState() pairs = findDuplicatePairs(state.contacts, new Set(state.ignoredMergePairs)) } } finally { setMergingAll(false) } } return (
🧹

Des méthodes simples pour nettoyer vos contacts

Obtenez de l'aide pour fusionner les contacts en double, ajouter des informations utiles, et bien encore

{subView === "merge" && (

Fusionner les doublons ({mergeSuggestions.length})

{mergeSuggestions.length > 0 && ( )}
{mergeSuggestions.length === 0 && (

Aucun doublon détecté

)}
{mergeSuggestions.map((suggestion) => ( handleMerge(suggestion)} onIgnore={() => handleIgnore(suggestion)} /> ))}
)} {subView === "coordinates" && }
) } function MergeSuggestionCard({ suggestion, onMerge, onIgnore, }: { suggestion: MergeSuggestion onMerge: () => void onIgnore: () => void }) { const { contactA, contactB, reason } = suggestion return (

{REASON_LABELS[reason]}

) } function ContactMiniCard({ contact }: { contact: import("@/lib/contacts/types").FullContact }) { const displayName = fullContactDisplayName(contact) const name = displayName || contact.emails[0]?.value || "?" const color = avatarColor(name) const initial = senderInitial(name) return (
{contact.avatarUrl ? ( {name} ) : (
{initial}
)}

{name}

{contact.emails[0] && (

{contact.emails[0].value}

)} {contact.phones[0] && (

{contact.phones[0].value} ({contact.phones[0].label})

)}
) }