From e4a93b8443fa71bab3a647fd0bfcda46586588d6 Mon Sep 17 00:00:00 2001 From: alma Date: Sun, 20 Apr 2025 19:28:35 +0200 Subject: [PATCH] carnet panel contact --- app/carnet/page.tsx | 172 +++++++++++++++++++++++--------------------- 1 file changed, 89 insertions(+), 83 deletions(-) diff --git a/app/carnet/page.tsx b/app/carnet/page.tsx index c3828baa..0c38dda5 100644 --- a/app/carnet/page.tsx +++ b/app/carnet/page.tsx @@ -178,92 +178,99 @@ export default function CarnetPage() { fetchNotes(); }, [selectedFolder]); - const parseVCard = (content: string): Contact | null => { + const parseVCard = (content: string): Contact[] => { try { console.log('Raw VCF content:', content); - const lines = content.split('\n').filter(line => line.trim()); - console.log('Parsed lines:', lines); - const contact: Partial = { - id: Math.random().toString(36).substr(2, 9), - fullName: '', - email: '', - phone: '', - organization: '', - address: '', - notes: '' - }; + // Split the content into individual vCards + const vcardSections = content.split('BEGIN:VCARD').filter(section => section.trim()); + console.log('Found vCard sections:', vcardSections.length); + + return vcardSections.map(section => { + const lines = section.split('\n').filter(line => line.trim()); + console.log('Processing vCard section with lines:', lines.length); + + const contact: Partial = { + id: Math.random().toString(36).substr(2, 9), + fullName: '', + email: '', + phone: '', + organization: '', + address: '', + notes: '' + }; - lines.forEach(line => { - console.log('Processing line:', line); - // Handle FN (Formatted Name) - if (line.startsWith('FN:')) { - contact.fullName = line.substring(3).trim(); - console.log('Found full name:', contact.fullName); - } - // Handle N (Name components) - else if (line.startsWith('N:')) { - const nameParts = line.substring(2).split(';'); - if (nameParts.length >= 2) { - const lastName = nameParts[0]?.trim(); - const firstName = nameParts[1]?.trim(); - if (!contact.fullName) { - contact.fullName = `${firstName} ${lastName}`.trim(); - console.log('Constructed full name from N field:', contact.fullName); + lines.forEach(line => { + console.log('Processing line:', line); + // Handle FN (Formatted Name) + if (line.startsWith('FN:')) { + contact.fullName = line.substring(3).trim(); + console.log('Found full name:', contact.fullName); + } + // Handle N (Name components) + else if (line.startsWith('N:')) { + const nameParts = line.substring(2).split(';'); + if (nameParts.length >= 2) { + const lastName = nameParts[0]?.trim(); + const firstName = nameParts[1]?.trim(); + if (!contact.fullName) { + contact.fullName = `${firstName} ${lastName}`.trim(); + console.log('Constructed full name from N field:', contact.fullName); + } } } - } - // Handle EMAIL - else if (line.startsWith('EMAIL;')) { - const email = line.split(':')[1]; - if (email) { - contact.email = email.trim(); - console.log('Found email:', contact.email); + // Handle EMAIL + else if (line.startsWith('EMAIL;')) { + const email = line.split(':')[1]; + if (email) { + contact.email = email.trim(); + console.log('Found email:', contact.email); + } } - } - // Handle TEL - else if (line.startsWith('TEL;')) { - const phone = line.split(':')[1]; - if (phone) { - contact.phone = phone.trim(); - console.log('Found phone:', contact.phone); + // Handle TEL + else if (line.startsWith('TEL;')) { + const phone = line.split(':')[1]; + if (phone) { + contact.phone = phone.trim(); + console.log('Found phone:', contact.phone); + } } - } - // Handle ORG - else if (line.startsWith('ORG:')) { - contact.organization = line.substring(4).trim(); - console.log('Found organization:', contact.organization); - } - // Handle ADR - 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(', '); - console.log('Found address:', contact.address); + // Handle ORG + else if (line.startsWith('ORG:')) { + contact.organization = line.substring(4).trim(); + console.log('Found organization:', contact.organization); } + // Handle ADR + 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(', '); + console.log('Found address:', contact.address); + } + } + // Handle NOTE + else if (line.startsWith('NOTE:')) { + contact.notes = line.substring(5).trim(); + console.log('Found notes:', contact.notes); + } + }); + + if (!contact.fullName) { + contact.fullName = 'Unknown Contact'; + console.log('No name found, using default'); } - // Handle NOTE - else if (line.startsWith('NOTE:')) { - contact.notes = line.substring(5).trim(); - console.log('Found notes:', contact.notes); - } + + console.log('Final contact object:', contact); + return contact as Contact; }); - - if (!contact.fullName) { - contact.fullName = 'Unknown Contact'; - console.log('No name found, using default'); - } - - console.log('Final contact object:', contact); - return contact as Contact; } catch (error) { console.error('Error parsing VCF content:', error); - return null; + return []; } }; @@ -284,23 +291,22 @@ export default function CarnetPage() { const contentResponse = await fetch(`/api/nextcloud/files/content?path=${encodeURIComponent(webdavPath)}`); if (contentResponse.ok) { const content = await contentResponse.text(); - const contact = parseVCard(content); - if (contact) { - return { - ...contact, - group: folder - }; - } + const contacts = parseVCard(content); + return contacts.map(contact => ({ + ...contact, + group: folder + })); } - return null; + return []; } catch (error) { console.error('Error fetching VCF content:', error); - return null; + return []; } }) ); - setContacts(parsedContacts.filter(Boolean)); + // Flatten the array of contact arrays + setContacts(parsedContacts.flat().filter(Boolean)); } } catch (error) { console.error('Error fetching contacts:', error);