carnet panel contact

This commit is contained in:
alma 2025-04-20 19:28:35 +02:00
parent b7f889e011
commit e4a93b8443

View File

@ -178,92 +178,99 @@ export default function CarnetPage() {
fetchNotes(); fetchNotes();
}, [selectedFolder]); }, [selectedFolder]);
const parseVCard = (content: string): Contact | null => { const parseVCard = (content: string): Contact[] => {
try { try {
console.log('Raw VCF content:', content); console.log('Raw VCF content:', content);
const lines = content.split('\n').filter(line => line.trim());
console.log('Parsed lines:', lines);
const contact: Partial<Contact> = { // Split the content into individual vCards
id: Math.random().toString(36).substr(2, 9), const vcardSections = content.split('BEGIN:VCARD').filter(section => section.trim());
fullName: '', console.log('Found vCard sections:', vcardSections.length);
email: '',
phone: '',
organization: '',
address: '',
notes: ''
};
lines.forEach(line => { return vcardSections.map(section => {
console.log('Processing line:', line); const lines = section.split('\n').filter(line => line.trim());
// Handle FN (Formatted Name) console.log('Processing vCard section with lines:', lines.length);
if (line.startsWith('FN:')) {
contact.fullName = line.substring(3).trim(); const contact: Partial<Contact> = {
console.log('Found full name:', contact.fullName); id: Math.random().toString(36).substr(2, 9),
} fullName: '',
// Handle N (Name components) email: '',
else if (line.startsWith('N:')) { phone: '',
const nameParts = line.substring(2).split(';'); organization: '',
if (nameParts.length >= 2) { address: '',
const lastName = nameParts[0]?.trim(); notes: ''
const firstName = nameParts[1]?.trim(); };
if (!contact.fullName) {
contact.fullName = `${firstName} ${lastName}`.trim(); lines.forEach(line => {
console.log('Constructed full name from N field:', contact.fullName); 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
// Handle EMAIL else if (line.startsWith('EMAIL;')) {
else if (line.startsWith('EMAIL;')) { const email = line.split(':')[1];
const email = line.split(':')[1]; if (email) {
if (email) { contact.email = email.trim();
contact.email = email.trim(); console.log('Found email:', contact.email);
console.log('Found email:', contact.email); }
} }
} // Handle TEL
// Handle TEL else if (line.startsWith('TEL;')) {
else if (line.startsWith('TEL;')) { const phone = line.split(':')[1];
const phone = line.split(':')[1]; if (phone) {
if (phone) { contact.phone = phone.trim();
contact.phone = phone.trim(); console.log('Found phone:', contact.phone);
console.log('Found phone:', contact.phone); }
} }
} // Handle ORG
// Handle ORG else if (line.startsWith('ORG:')) {
else if (line.startsWith('ORG:')) { contact.organization = line.substring(4).trim();
contact.organization = line.substring(4).trim(); console.log('Found organization:', contact.organization);
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 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:')) { console.log('Final contact object:', contact);
contact.notes = line.substring(5).trim(); return contact as Contact;
console.log('Found notes:', contact.notes);
}
}); });
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) { } catch (error) {
console.error('Error parsing VCF content:', 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)}`); const contentResponse = await fetch(`/api/nextcloud/files/content?path=${encodeURIComponent(webdavPath)}`);
if (contentResponse.ok) { if (contentResponse.ok) {
const content = await contentResponse.text(); const content = await contentResponse.text();
const contact = parseVCard(content); const contacts = parseVCard(content);
if (contact) { return contacts.map(contact => ({
return { ...contact,
...contact, group: folder
group: folder }));
};
}
} }
return null; return [];
} catch (error) { } catch (error) {
console.error('Error fetching VCF content:', 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) { } catch (error) {
console.error('Error fetching contacts:', error); console.error('Error fetching contacts:', error);