"use client"; import React, { useState, useEffect } from 'react'; import { Search, BookOpen, Tag, Trash2, Star, Archive, X, Folder, FileText, Calendar, Heart, Users, LucideIcon, ChevronRight } from 'lucide-react'; import { Card } from '@/components/ui/card'; interface NavigationProps { nextcloudFolders: string[]; onFolderSelect: (folder: string) => void; } type FolderType = 'Notes' | 'Diary' | 'Health' | 'Contacts'; interface FolderConfig { icon: LucideIcon; order: number; displayName: string; } // Define folder order, icons and display names const FOLDER_CONFIG: Record = { 'Notes': { icon: FileText, order: 1, displayName: 'Bloc-notes' }, 'Diary': { icon: Calendar, order: 2, displayName: 'Journal' }, 'Health': { icon: Heart, order: 3, displayName: 'Carnet de santé' }, 'Contacts': { icon: Users, order: 4, displayName: 'Carnet d\'adresses' } }; interface ContactFile { id: string; filename: string; basename: string; lastmod: string; } interface Contact { id: string; name: string; email: string; phone: string; address: string; notes: string; tags: string[]; created_at: string; updated_at: string; user_id: string; vcard: string; } export default function Navigation({ nextcloudFolders, onFolderSelect }: NavigationProps) { const [searchQuery, setSearchQuery] = useState(''); const [expandedContacts, setExpandedContacts] = useState(false); const [contactFiles, setContactFiles] = useState([]); const [isLoadingContacts, setIsLoadingContacts] = useState(false); const getFolderIcon = (folder: string) => { switch (folder) { case 'Notes': return FileText; case 'Diary': return Calendar; case 'Health': return Heart; case 'Contacts': return Users; default: return FileText; } }; // Sort folders according to the specified order const sortedFolders = [...nextcloudFolders].sort((a, b) => { const orderA = (FOLDER_CONFIG[a as FolderType]?.order) || 999; const orderB = (FOLDER_CONFIG[b as FolderType]?.order) || 999; return orderA - orderB; }); const fetchContactFiles = async () => { try { setIsLoadingContacts(true); const response = await fetch('/api/nextcloud/files?folder=Contacts'); if (response.ok) { const files = await response.json(); // Only log the number of files received, not their contents console.log(`Received ${files.length} files from Nextcloud`); // Filter for VCF files and map to ContactFile interface const vcfFiles = files .filter((file: any) => file.basename.endsWith('.vcf')) .map((file: any) => ({ id: file.etag, filename: file.filename, basename: file.basename, lastmod: file.lastmod })); // Only log the number of VCF files processed console.log(`Processed ${vcfFiles.length} VCF files`); setContactFiles(vcfFiles); } } catch (error) { console.error('Error fetching contact files:', error); setContactFiles([]); } finally { setIsLoadingContacts(false); } }; useEffect(() => { if (expandedContacts) { fetchContactFiles(); } }, [expandedContacts]); const handleVcfFile = async (file: File) => { try { const text = await file.text(); const vcards = text.split('END:VCARD').filter(vcard => vcard.trim()); const processedContacts = vcards.map(vcard => { const lines = vcard.split('\n'); const contact: Contact = { id: crypto.randomUUID(), name: '', email: '', phone: '', address: '', notes: '', tags: [], created_at: new Date().toISOString(), updated_at: new Date().toISOString(), user_id: '', vcard: vcard + 'END:VCARD' }; lines.forEach(line => { if (line.startsWith('FN:')) { contact.name = line.substring(3); } else if (line.startsWith('EMAIL;')) { contact.email = line.split(':')[1]; } else if (line.startsWith('TEL;')) { contact.phone = line.split(':')[1]; } else if (line.startsWith('ADR;')) { contact.address = line.split(':')[1]; } }); return contact; }); // Only log the number of contacts processed, not their details console.log(`Processed ${processedContacts.length} contacts from VCF file`); // Convert Contact objects to ContactFile objects const contactFiles: ContactFile[] = processedContacts.map(contact => ({ id: contact.id, filename: `${contact.name}.vcf`, basename: contact.name, lastmod: new Date().toISOString() })); setContactFiles(prev => [...prev, ...contactFiles]); } catch (error) { console.error('Error processing VCF file:', error); } }; return (
{/* Search */}
PAGES
setSearchQuery(e.target.value)} placeholder="Recherche..." className="w-full pl-8 pr-4 py-1.5 bg-white border border-carnet-border rounded-md text-sm text-carnet-text-primary placeholder-carnet-text-muted focus:outline-none focus:ring-1 focus:ring-primary" /> {searchQuery && ( )}
{/* Folders */}

VUES

{sortedFolders.map((folder) => { const config = FOLDER_CONFIG[folder as FolderType]; const Icon = config?.icon || FileText; const displayName = config?.displayName || folder; return (
{folder === 'Contacts' && expandedContacts && (
{isLoadingContacts ? (
Chargement...
) : contactFiles.length === 0 ? (
Aucun contact
) : ( contactFiles.map((file) => { return ( ); }) )}
)}
); })}
); }