"use client" import { useRef, useState } from "react" import { Dialog, DialogContent, DialogHeader, DialogTitle, } from "@/components/ui/dialog" import { Button } from "@/components/ui/button" import { Info } from "lucide-react" import { parseContactFile } from "@/lib/contacts/import-parsers" import { useCreateContact } from "@/lib/api/hooks/use-contact-mutations" import { fullContactToApiContact } from "@/lib/api/adapters" import type { FullContact } from "@/lib/contacts/types" import { CONTACTS_HEADING_TEXT, CONTACTS_MUTED_TEXT, CONTACTS_PAGE_LINK_BTN_CLASS, CONTACTS_PANEL_MUTED_ICON_CLASS, CONTACTS_PRIMARY_BTN_CLASS, } from "@/lib/contacts-chrome-classes" import { cn } from "@/lib/utils" interface ImportDialogProps { open: boolean onOpenChange: (open: boolean) => void } export function ImportDialog({ open, onOpenChange }: ImportDialogProps) { const fileRef = useRef(null) const createContactMutation = useCreateContact() const [pendingFile, setPendingFile] = useState(null) const [previewCount, setPreviewCount] = useState(0) const [error, setError] = useState(null) const [importing, setImporting] = useState(false) function resetState() { setPendingFile(null) setPreviewCount(0) setError(null) setImporting(false) if (fileRef.current) fileRef.current.value = "" } function handleOpenChange(next: boolean) { if (!next) resetState() onOpenChange(next) } function handleFileSelect() { fileRef.current?.click() } async function handleFileChange(e: React.ChangeEvent) { const file = e.target.files?.[0] if (!file) return setError(null) setPendingFile(file) try { const parsed = await parseContactFile(file) setPreviewCount(parsed.length) if (parsed.length === 0) { setError("Aucun contact trouvé dans ce fichier.") } } catch { setError("Impossible de lire ce fichier.") setPreviewCount(0) } } async function handleImport() { if (!pendingFile || previewCount === 0) return setImporting(true) setError(null) try { const parsed = await parseContactFile(pendingFile) if (parsed.length === 0) { setError("Aucun contact importé.") return } for (const partial of parsed) { const fullContact: FullContact = { id: crypto.randomUUID(), createdAt: Date.now(), updatedAt: Date.now(), ...partial, firstName: partial.firstName ?? "", lastName: partial.lastName ?? "", emails: partial.emails ?? [], phones: partial.phones ?? [], } createContactMutation.mutate({ bookId: "default", contact: fullContactToApiContact(fullContact), }) } handleOpenChange(false) } catch { setError("L'import a échoué. Vérifiez le format du fichier.") } finally { setImporting(false) } } return (
Importer des contacts

Pour commencer, sélectionnez un fichier.
Utilisez le format CSV ou vCard (.vcf).

{pendingFile && previewCount > 0 && (

{previewCount} contact{previewCount > 1 ? "s" : ""} prêt {previewCount > 1 ? "s" : ""} à importer depuis{" "} {pendingFile.name}

)} {error &&

{error}

}

Vous essayez de sauvegarder les contacts de votre mobile ?
Voici comment les synchroniser.

) }