carnet panel contact

This commit is contained in:
alma 2025-04-20 19:38:17 +02:00
parent a654994b1f
commit ef842a15d6
2 changed files with 76 additions and 77 deletions

View File

@ -31,8 +31,8 @@ interface Note {
interface Contact {
id: string;
fullName: string;
email: string;
fullName?: string;
email?: string;
phone?: string;
organization?: string;
address?: string;
@ -132,51 +132,33 @@ export default function CarnetPage() {
}, [isSmallScreen, isMediumScreen]);
useEffect(() => {
const fetchNotes = async () => {
try {
setIsLoadingNotes(true);
const response = await fetch(`/api/nextcloud/files?folder=${selectedFolder}`);
if (!response.ok) {
throw new Error('Failed to fetch notes');
}
const data = await response.json();
if (selectedFolder === 'Contacts') {
// For contacts, parse the VCF files
const parsedContacts = await Promise.all(
data.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);
}
console.error('Failed to fetch VCF content:', contentResponse.status);
return null;
} catch (error) {
console.error('Error fetching VCF content:', error);
return null;
}
})
);
setContacts(parsedContacts.filter(Boolean));
} else {
if (selectedFolder === 'Contacts') {
// When "Contacts" is selected, show all contacts
fetchContacts('Contacts');
} else if (selectedFolder.endsWith('.vcf')) {
// When a specific VCF file is selected, show its contacts
fetchContacts(selectedFolder);
} else {
// For other folders (Notes, etc.), fetch notes
const fetchNotes = async () => {
try {
setIsLoadingNotes(true);
const response = await fetch(`/api/nextcloud/files?folder=${selectedFolder}`);
if (!response.ok) {
throw new Error('Failed to fetch notes');
}
const data = await response.json();
setNotes(data);
}
} catch (error) {
console.error('Error fetching data:', error);
if (selectedFolder === 'Contacts') {
setContacts([]);
} else {
} catch (error) {
console.error('Error fetching notes:', error);
setNotes([]);
} finally {
setIsLoadingNotes(false);
}
} finally {
setIsLoadingNotes(false);
}
};
fetchNotes();
}, [selectedFolder]);
};
fetchNotes();
}
}, [selectedFolder, session?.user?.id]);
const parseVCard = (content: string): Contact[] => {
try {
@ -277,39 +259,52 @@ export default function CarnetPage() {
const fetchContacts = async (folder: string) => {
try {
setIsLoadingContacts(true);
const response = await fetch(`/api/nextcloud/files?folder=${folder}`);
if (response.ok) {
const files = await response.json();
const vcfFiles = files.filter((file: any) => file.basename.endsWith('.vcf'));
// Parse VCF files and extract contact information
const parsedContacts = await Promise.all(
vcfFiles.map(async (file: any) => {
try {
// Construct the full WebDAV path
const webdavPath = `/files/${file.filename}`;
const contentResponse = await fetch(`/api/nextcloud/files/content?path=${encodeURIComponent(webdavPath)}`);
if (contentResponse.ok) {
const content = await contentResponse.text();
const contacts = parseVCard(content);
return contacts.map(contact => ({
...contact,
group: folder
}));
// First, check if we're looking at a specific VCF file
if (folder.endsWith('.vcf')) {
const response = await fetch(`/api/nextcloud/files/content?path=${encodeURIComponent(`/files/cube-${session?.user?.id}/Private/Contacts/${folder}`)}`);
if (response.ok) {
const { content } = await response.json();
const contacts = parseVCard(content);
setContacts(contacts.map(contact => ({
...contact,
group: folder.replace('.vcf', '')
})));
}
} else {
// If not a VCF file, list all VCF files in the folder
const response = await fetch(`/api/nextcloud/files?folder=${folder}`);
if (response.ok) {
const files = await response.json();
const vcfFiles = files.filter((file: any) => file.basename.endsWith('.vcf'));
// Parse VCF files and extract contact information
const parsedContacts = await Promise.all(
vcfFiles.map(async (file: any) => {
try {
const contentResponse = await fetch(`/api/nextcloud/files/content?path=${encodeURIComponent(file.filename)}`);
if (contentResponse.ok) {
const { content } = await contentResponse.json();
const contacts = parseVCard(content);
return contacts.map(contact => ({
...contact,
group: file.basename.replace('.vcf', '')
}));
}
return [];
} catch (error) {
console.error('Error fetching VCF content:', error);
return [];
}
return [];
} catch (error) {
console.error('Error fetching VCF content:', error);
return [];
}
})
);
// Flatten the array of contact arrays
setContacts(parsedContacts.flat().filter(Boolean));
})
);
// Flatten the array of contact arrays
setContacts(parsedContacts.flat().filter(Boolean));
}
}
} catch (error) {
console.error('Error fetching contacts:', error);
setContacts([]);
} finally {
setIsLoadingContacts(false);
}

View File

@ -5,8 +5,8 @@ import { Search, User, Mail, Phone, Building, MapPin, ChevronRight } from 'lucid
interface Contact {
id: string;
fullName: string;
email: string;
fullName?: string;
email?: string;
phone?: string;
organization?: string;
address?: string;
@ -76,8 +76,12 @@ export const ContactsView: React.FC<ContactsViewProps> = ({
<User className="h-5 w-5 text-primary" />
</div>
<div>
<div className="font-medium text-carnet-text-primary">{contact.fullName}</div>
<div className="text-sm text-carnet-text-muted">{contact.email}</div>
<div className="font-medium text-carnet-text-primary">
{contact.fullName || contact.email || 'Sans nom'}
</div>
{contact.email && (
<div className="text-sm text-carnet-text-muted">{contact.email}</div>
)}
</div>
</div>
<ChevronRight className="h-4 w-4 text-carnet-text-muted" />