diff --git a/app/carnet/page.tsx b/app/carnet/page.tsx index 04a14a5d..8ca14e5e 100644 --- a/app/carnet/page.tsx +++ b/app/carnet/page.tsx @@ -178,6 +178,68 @@ export default function CarnetPage() { fetchNotes(); }, [selectedFolder]); + const parseVCard = (content: string): Contact | null => { + try { + const lines = content.split('\n').filter(line => line.trim()); + const contact: Partial = { + id: Math.random().toString(36).substr(2, 9), + fullName: '', + email: '', + phone: '', + organization: '', + address: '', + notes: '' + }; + + lines.forEach(line => { + if (line.startsWith('FN:')) { + contact.fullName = line.substring(3).trim(); + } else if (line.startsWith('EMAIL;')) { + const email = line.split(':')[1]; + if (email) contact.email = email.trim(); + } else if (line.startsWith('TEL;')) { + const phone = line.split(':')[1]; + if (phone) contact.phone = phone.trim(); + } else if (line.startsWith('ORG:')) { + contact.organization = line.substring(4).trim(); + } else if (line.startsWith('ADR;')) { + const addressParts = line.split(':')[1].split(';'); + if (addressParts.length >= 7) { + const street = addressParts[2]?.trim(); + const city = addressParts[3]?.trim(); + const state = addressParts[4]?.trim(); + const zip = addressParts[5]?.trim(); + const country = addressParts[6]?.trim(); + contact.address = [street, city, state, zip, country].filter(Boolean).join(', '); + } + } else if (line.startsWith('NOTE:')) { + contact.notes = line.substring(5).trim(); + } + }); + + if (!contact.fullName) { + const nLine = lines.find(line => line.startsWith('N:')); + if (nLine) { + const nameParts = nLine.substring(2).split(';'); + if (nameParts.length >= 2) { + const lastName = nameParts[0]?.trim(); + const firstName = nameParts[1]?.trim(); + contact.fullName = `${firstName} ${lastName}`.trim(); + } + } + } + + if (!contact.fullName) { + contact.fullName = 'Unknown Contact'; + } + + return contact as Contact; + } catch (error) { + console.error('Error parsing VCF content:', error); + return null; + } + }; + const fetchContacts = async (folder: string) => { try { setIsLoadingContacts(true); @@ -190,13 +252,17 @@ export default function CarnetPage() { const parsedContacts = await Promise.all( vcfFiles.map(async (file: any) => { try { - // Use the full filename for fetching content const contentResponse = await fetch(`/api/nextcloud/files/content?path=${encodeURIComponent(file.filename)}`); if (contentResponse.ok) { const content = await contentResponse.text(); - return parseVCard(content); + const contact = parseVCard(content); + if (contact) { + return { + ...contact, + group: folder + }; + } } - console.error('Failed to fetch VCF content:', contentResponse.status); return null; } catch (error) { console.error('Error fetching VCF content:', error); @@ -214,43 +280,6 @@ export default function CarnetPage() { } }; - const parseVCard = (content: string): Contact => { - const lines = content.split('\n'); - const contact: Partial = {}; - - lines.forEach(line => { - if (line.startsWith('FN:')) { - contact.fullName = line.substring(3).trim(); - } else if (line.startsWith('EMAIL;')) { - contact.email = line.split(':')[1].trim(); - } else if (line.startsWith('TEL;')) { - contact.phone = line.split(':')[1].trim(); - } else if (line.startsWith('ORG:')) { - contact.organization = line.substring(4).trim(); - } else if (line.startsWith('ADR;')) { - contact.address = line.split(':')[1].trim(); - } else if (line.startsWith('NOTE:')) { - contact.notes = line.substring(5).trim(); - } - }); - - return { - id: Math.random().toString(36).substr(2, 9), - fullName: contact.fullName || 'Unknown', - email: contact.email || '', - phone: contact.phone, - organization: contact.organization, - address: contact.address, - notes: contact.notes - } as Contact; - }; - - useEffect(() => { - if (selectedFolder === 'Contacts') { - fetchContacts(selectedFolder); - } - }, [selectedFolder]); - // Handle panel resizing const handleNavResize = (e: MouseEvent) => { if (!isDraggingNav) return;