carnet panel contact

This commit is contained in:
alma 2025-04-20 22:32:38 +02:00
parent 643afb5567
commit 23d9e0aed5

View File

@ -394,30 +394,56 @@ export default function CarnetPage() {
const { content } = await response.json();
// Split the content into individual vCards and clean up empty lines
// Split the content into individual vCards
const vcards = content.split('BEGIN:VCARD')
.filter(section => section.trim())
.map(section => section.trim());
.map(section => 'BEGIN:VCARD' + section.trim());
// Update or add the contact
let updatedVcards: string[] = [];
let contactUpdated = false;
for (const section of vcards) {
const vcard = parseVCard('BEGIN:VCARD' + section);
if (vcard.uid?.[0]?.value === contact.id) {
for (const vcard of vcards) {
const parsed = parseVCard(vcard);
if (parsed.uid?.[0]?.value === contact.id) {
// Replace the existing contact
updatedVcards.push(generateVCardContent(contact));
const newVcard = [
'BEGIN:VCARD',
'VERSION:3.0',
`UID:${contact.id}`,
`FN:${contact.fullName || ''}`,
...(contact.email ? [`EMAIL;TYPE=INTERNET:${contact.email}`] : []),
...(contact.phone ? [`TEL;TYPE=CELL:${contact.phone}`] : []),
...(contact.organization ? [`ORG:${contact.organization}`] : []),
...(contact.address ? [`ADR:${contact.address}`] : []),
...(contact.notes ? [`NOTE:${contact.notes}`] : []),
...(contact.group ? [`CATEGORIES:${contact.group}`] : []),
'END:VCARD'
].join('\n');
updatedVcards.push(newVcard);
contactUpdated = true;
} else {
// Keep the existing contact
updatedVcards.push('BEGIN:VCARD' + section);
updatedVcards.push(vcard);
}
}
// If contact wasn't found, add it as new
if (!contactUpdated) {
updatedVcards.push(generateVCardContent(contact));
const newVcard = [
'BEGIN:VCARD',
'VERSION:3.0',
`UID:${contact.id}`,
`FN:${contact.fullName || ''}`,
...(contact.email ? [`EMAIL;TYPE=INTERNET:${contact.email}`] : []),
...(contact.phone ? [`TEL;TYPE=CELL:${contact.phone}`] : []),
...(contact.organization ? [`ORG:${contact.organization}`] : []),
...(contact.address ? [`ADR:${contact.address}`] : []),
...(contact.notes ? [`NOTE:${contact.notes}`] : []),
...(contact.group ? [`CATEGORIES:${contact.group}`] : []),
'END:VCARD'
].join('\n');
updatedVcards.push(newVcard);
}
// Join all vCards back together with proper spacing