857 lines
28 KiB
TypeScript
857 lines
28 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useState, useRef } from "react";
|
|
import { useSession } from "next-auth/react";
|
|
import { redirect } from "next/navigation";
|
|
import Navigation from "@/components/carnet/navigation";
|
|
import { NotesView } from "@/components/carnet/notes-view";
|
|
import { Editor } from "@/components/carnet/editor";
|
|
import { PanelResizer } from "@/components/carnet/panel-resizer";
|
|
import { useMediaQuery } from "@/hooks/use-media-query";
|
|
import { ContactsView } from '@/components/carnet/contacts-view';
|
|
import { X, Menu } from "lucide-react";
|
|
import { ContactDetails } from '@/components/carnet/contact-details';
|
|
import { parse as parseVCard, format as formatVCard } from 'vcard-parser';
|
|
import { PaneLayout } from './pane-layout';
|
|
|
|
interface Note {
|
|
id: string;
|
|
title: string;
|
|
content: string;
|
|
lastModified: string;
|
|
type: string;
|
|
mime: string;
|
|
etag: string;
|
|
}
|
|
|
|
interface Contact {
|
|
id: string;
|
|
fullName?: string;
|
|
email?: string;
|
|
phone?: string;
|
|
organization?: string;
|
|
address?: string;
|
|
notes?: string;
|
|
group?: string;
|
|
}
|
|
|
|
export default function CarnetPage() {
|
|
const { data: session, status } = useSession();
|
|
const [isLoading, setIsLoading] = useState(true);
|
|
const [isSaving, setIsSaving] = useState(false);
|
|
const [layoutMode, setLayoutMode] = useState<string>("item-selection");
|
|
const [selectedNote, setSelectedNote] = useState<Note | null>(null);
|
|
const [isMobile, setIsMobile] = useState(false);
|
|
const [showNav, setShowNav] = useState(true);
|
|
const [showNotes, setShowNotes] = useState(true);
|
|
const [nextcloudFolders, setNextcloudFolders] = useState<string[]>([]);
|
|
const [selectedFolder, setSelectedFolder] = useState<string>('Notes');
|
|
const [notes, setNotes] = useState<Note[]>([]);
|
|
const [isLoadingNotes, setIsLoadingNotes] = useState(true);
|
|
const [contacts, setContacts] = useState<Contact[]>([]);
|
|
const [selectedContact, setSelectedContact] = useState<Contact | null>(null);
|
|
const [isLoadingContacts, setIsLoadingContacts] = useState(true);
|
|
|
|
// Panel widths state
|
|
const [navWidth, setNavWidth] = useState(220);
|
|
const [notesWidth, setNotesWidth] = useState(400);
|
|
const [isDraggingNav, setIsDraggingNav] = useState(false);
|
|
const [isDraggingNotes, setIsDraggingNotes] = useState(false);
|
|
|
|
// Check screen size
|
|
const isSmallScreen = useMediaQuery("(max-width: 768px)");
|
|
const isMediumScreen = useMediaQuery("(max-width: 1024px)");
|
|
|
|
// Cache for Nextcloud folders
|
|
const foldersCache = useRef<{ folders: string[]; timestamp: number } | null>(null);
|
|
|
|
// Cache for notes list (Panel 2)
|
|
const notesCache = useRef<Record<string, { notes: Note[]; timestamp: number }>>({});
|
|
|
|
// Cache for note content (Panel 3)
|
|
const noteContentCache = useRef<Record<string, { content: string; timestamp: number }>>({});
|
|
|
|
// Clear folder cache on component mount to ensure fresh data
|
|
useEffect(() => {
|
|
try {
|
|
localStorage.removeItem('nextcloud_folders');
|
|
console.log('Cleared folder cache');
|
|
} catch (error) {
|
|
console.error('Error clearing folder cache:', error);
|
|
}
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
const fetchNextcloudFolders = async () => {
|
|
// First check localStorage cache
|
|
const cachedData = localStorage.getItem('nextcloud_folders');
|
|
if (cachedData) {
|
|
const { folders, timestamp } = JSON.parse(cachedData);
|
|
const cacheAge = Date.now() - timestamp;
|
|
if (cacheAge < 5 * 60 * 1000) { // 5 minutes cache
|
|
setNextcloudFolders(folders);
|
|
return;
|
|
}
|
|
}
|
|
|
|
try {
|
|
const response = await fetch('/api/nextcloud/status');
|
|
if (!response.ok) {
|
|
throw new Error('Failed to fetch Nextcloud folders');
|
|
}
|
|
const data = await response.json();
|
|
const folders = data.folders || [];
|
|
|
|
// Update both localStorage and memory cache
|
|
localStorage.setItem('nextcloud_folders', JSON.stringify({
|
|
folders,
|
|
timestamp: Date.now()
|
|
}));
|
|
|
|
foldersCache.current = {
|
|
folders,
|
|
timestamp: Date.now()
|
|
};
|
|
|
|
setNextcloudFolders(folders);
|
|
} catch (err) {
|
|
console.error('Error fetching Nextcloud folders:', err);
|
|
setNextcloudFolders([]);
|
|
}
|
|
};
|
|
|
|
if (status === "authenticated") {
|
|
fetchNextcloudFolders();
|
|
}
|
|
}, [status]);
|
|
|
|
useEffect(() => {
|
|
if (status === "unauthenticated") {
|
|
redirect("/signin");
|
|
}
|
|
if (status === "authenticated" && session?.user?.id) {
|
|
// Initialize all folders when user logs in
|
|
fetch('/api/storage/init', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
}
|
|
}).then(response => {
|
|
if (response.ok) {
|
|
console.log('All folders initialized successfully');
|
|
} else {
|
|
console.error('Failed to initialize folders');
|
|
}
|
|
}).catch(error => {
|
|
console.error('Error initializing folders:', error);
|
|
});
|
|
}
|
|
if (status !== "loading") {
|
|
setIsLoading(false);
|
|
}
|
|
}, [status, session?.user?.id]);
|
|
|
|
useEffect(() => {
|
|
if (isSmallScreen) {
|
|
setIsMobile(true);
|
|
setShowNav(false);
|
|
setShowNotes(false);
|
|
} else if (isMediumScreen) {
|
|
setIsMobile(false);
|
|
setShowNav(true);
|
|
setShowNotes(false);
|
|
} else {
|
|
setIsMobile(false);
|
|
setShowNav(true);
|
|
setShowNotes(true);
|
|
}
|
|
}, [isSmallScreen, isMediumScreen]);
|
|
|
|
useEffect(() => {
|
|
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
|
|
fetchNotes();
|
|
}
|
|
}, [selectedFolder, session?.user?.id]);
|
|
|
|
const parseVCardContent = (content: string): Contact[] => {
|
|
try {
|
|
// Split the content into individual vCards
|
|
const vcards = content.split('BEGIN:VCARD').filter(section => section.trim());
|
|
|
|
return vcards.map(section => {
|
|
const vcard = parseVCard('BEGIN:VCARD' + section);
|
|
|
|
// Extract contact properties with proper type handling
|
|
const uid = vcard.uid?.[0]?.value;
|
|
const fullName = vcard.fn?.[0]?.value;
|
|
const email = vcard.email?.[0]?.value;
|
|
const phone = vcard.tel?.[0]?.value;
|
|
const organization = vcard.org?.[0]?.value;
|
|
const address = vcard.adr?.[0]?.value;
|
|
const notes = vcard.note?.[0]?.value;
|
|
const group = vcard.categories?.[0]?.value;
|
|
|
|
// Create a clean contact object
|
|
const contact: Contact = {
|
|
id: uid || Math.random().toString(36).substr(2, 9),
|
|
fullName: fullName || 'Unknown Contact',
|
|
email: email || '',
|
|
phone: phone || '',
|
|
organization: organization || '',
|
|
address: address || '',
|
|
notes: notes || '',
|
|
group: group || ''
|
|
};
|
|
|
|
return contact;
|
|
});
|
|
} catch (error) {
|
|
console.error('Error parsing VCF content:', error);
|
|
return [];
|
|
}
|
|
};
|
|
|
|
const fetchContacts = async (folder: string) => {
|
|
try {
|
|
setIsLoadingContacts(true);
|
|
|
|
// Use lowercase for consistency
|
|
const folderLowercase = folder.toLowerCase();
|
|
|
|
// First, check if we're looking at a specific VCF file
|
|
if (folder.endsWith('.vcf')) {
|
|
const response = await fetch(`/api/storage/files/content?path=${encodeURIComponent(`user-${session?.user?.id}/${folderLowercase}/${folder}`)}`);
|
|
if (response.ok) {
|
|
const { content } = await response.json();
|
|
const contacts = parseVCardContent(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/storage/files?folder=${folderLowercase}`);
|
|
if (response.ok) {
|
|
const files = await response.json();
|
|
const vcfFiles = files.filter((file: any) =>
|
|
file.basename?.endsWith('.vcf') || file.title?.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/storage/files/content?path=${encodeURIComponent(file.id)}`);
|
|
if (contentResponse.ok) {
|
|
const { content } = await contentResponse.json();
|
|
const contacts = parseVCardContent(content);
|
|
return contacts.map(contact => ({
|
|
...contact,
|
|
group: (file.basename || file.title)?.replace('.vcf', '')
|
|
}));
|
|
}
|
|
return [];
|
|
} catch (error) {
|
|
console.error('Error fetching VCF content:', error);
|
|
return [];
|
|
}
|
|
})
|
|
);
|
|
|
|
// Flatten the array of contact arrays
|
|
setContacts(parsedContacts.flat().filter(Boolean));
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('Error fetching contacts:', error);
|
|
setContacts([]);
|
|
} finally {
|
|
setIsLoadingContacts(false);
|
|
}
|
|
};
|
|
|
|
// Fetch notes based on the selected folder
|
|
const fetchNotes = async () => {
|
|
try {
|
|
setIsLoadingNotes(true);
|
|
|
|
// Convert folder name to lowercase for consistent storage access
|
|
const folderLowercase = selectedFolder.toLowerCase();
|
|
console.log(`Fetching notes from folder: ${folderLowercase}`);
|
|
|
|
// Check in-memory cache first
|
|
const cacheKey = `${session?.user?.id}-${folderLowercase}`;
|
|
const cachedNotes = notesCache.current[cacheKey];
|
|
const CACHE_EXPIRATION = 5 * 60 * 1000; // 5 minutes in milliseconds
|
|
|
|
if (cachedNotes && (Date.now() - cachedNotes.timestamp) < CACHE_EXPIRATION) {
|
|
console.log(`Using cached notes for ${folderLowercase} folder`);
|
|
setNotes(cachedNotes.notes);
|
|
setIsLoadingNotes(false);
|
|
return;
|
|
}
|
|
|
|
// Check localStorage cache if in-memory cache is not available
|
|
try {
|
|
const localStorageKey = `notes-cache-${cacheKey}`;
|
|
const storedCache = localStorage.getItem(localStorageKey);
|
|
|
|
if (storedCache) {
|
|
const { notes, timestamp } = JSON.parse(storedCache);
|
|
|
|
if ((Date.now() - timestamp) < CACHE_EXPIRATION) {
|
|
console.log(`Using localStorage cached notes for ${folderLowercase} folder`);
|
|
setNotes(notes);
|
|
|
|
// Update in-memory cache
|
|
notesCache.current[cacheKey] = { notes, timestamp };
|
|
|
|
setIsLoadingNotes(false);
|
|
return;
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('Error accessing localStorage notes cache:', error);
|
|
}
|
|
|
|
// Use direct storage API instead of adapter if cache is not available or expired
|
|
const response = await fetch(`/api/storage/files?folder=${folderLowercase}`);
|
|
|
|
if (response.ok) {
|
|
const data = await response.json();
|
|
console.log(`Fetched ${data.length} notes from ${folderLowercase} folder`);
|
|
|
|
// Update state
|
|
setNotes(data);
|
|
|
|
// Update both caches
|
|
const newTimestamp = Date.now();
|
|
notesCache.current[cacheKey] = { notes: data, timestamp: newTimestamp };
|
|
|
|
try {
|
|
localStorage.setItem(`notes-cache-${cacheKey}`, JSON.stringify({
|
|
notes: data,
|
|
timestamp: newTimestamp
|
|
}));
|
|
} catch (error) {
|
|
console.error('Error saving notes to localStorage:', error);
|
|
}
|
|
} else {
|
|
console.error('Error fetching notes:', await response.text());
|
|
setNotes([]);
|
|
}
|
|
} catch (error) {
|
|
console.error('Error fetching notes:', error);
|
|
setNotes([]);
|
|
} finally {
|
|
setIsLoadingNotes(false);
|
|
}
|
|
};
|
|
|
|
// Handle saving changes to a note
|
|
const handleSaveNote = async (note: Note) => {
|
|
try {
|
|
setIsSaving(true);
|
|
// Construct API payload with lowercase folder name
|
|
const payload = {
|
|
id: note.id || `user-${session?.user?.id}/${selectedFolder.toLowerCase()}/${note.title}${note.title.endsWith('.md') ? '' : '.md'}`,
|
|
title: note.title,
|
|
content: note.content,
|
|
folder: selectedFolder.toLowerCase(), // Use lowercase for storage consistency
|
|
mime: "text/markdown"
|
|
};
|
|
|
|
// Use direct storage API endpoint
|
|
const endpoint = '/api/storage/files';
|
|
const method = note.id ? 'PUT' : 'POST';
|
|
|
|
console.log(`Saving note to ${selectedFolder.toLowerCase()} using ${method}`);
|
|
|
|
const response = await fetch(endpoint, {
|
|
method,
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify(payload)
|
|
});
|
|
|
|
if (response.ok) {
|
|
// Invalidate the cache for this folder to ensure fresh data on next fetch
|
|
const cacheKey = `${session?.user?.id}-${selectedFolder.toLowerCase()}`;
|
|
|
|
// Remove from in-memory cache
|
|
if (notesCache.current[cacheKey]) {
|
|
delete notesCache.current[cacheKey];
|
|
}
|
|
|
|
// Remove from localStorage cache
|
|
try {
|
|
localStorage.removeItem(`notes-cache-${cacheKey}`);
|
|
} catch (error) {
|
|
console.error('Error removing notes from localStorage:', error);
|
|
}
|
|
|
|
// Update the content cache for this note
|
|
if (payload.id) {
|
|
noteContentCache.current[payload.id] = {
|
|
content: payload.content,
|
|
timestamp: Date.now()
|
|
};
|
|
|
|
// Update localStorage cache
|
|
try {
|
|
localStorage.setItem(`note-content-${payload.id}`, JSON.stringify({
|
|
content: payload.content,
|
|
timestamp: Date.now()
|
|
}));
|
|
} catch (error) {
|
|
console.error('Error saving note content to localStorage:', error);
|
|
}
|
|
}
|
|
|
|
// Refresh the list of notes
|
|
fetchNotes();
|
|
} else {
|
|
console.error('Error saving note:', await response.text());
|
|
}
|
|
} catch (error) {
|
|
console.error('Error saving note:', error);
|
|
} finally {
|
|
setIsSaving(false);
|
|
}
|
|
};
|
|
|
|
// Handle panel resizing
|
|
const handleNavResize = (e: MouseEvent) => {
|
|
if (!isDraggingNav) return;
|
|
const newWidth = e.clientX;
|
|
if (newWidth >= 48 && newWidth <= 400) {
|
|
setNavWidth(newWidth);
|
|
}
|
|
};
|
|
|
|
const handleNotesResize = (e: MouseEvent) => {
|
|
if (!isDraggingNotes) return;
|
|
const newWidth = e.clientX - navWidth - 2; // 2px for the resizer
|
|
if (newWidth >= 200) {
|
|
setNotesWidth(newWidth);
|
|
}
|
|
};
|
|
|
|
const handleNoteSelect = (note: Note) => {
|
|
setSelectedNote(note);
|
|
if (isMobile) {
|
|
setShowNotes(false);
|
|
}
|
|
};
|
|
|
|
const handleNoteSave = async (note: Note) => {
|
|
try {
|
|
const endpoint = note.id ? '/api/nextcloud/files' : '/api/nextcloud/files';
|
|
const method = note.id ? 'PUT' : 'POST';
|
|
|
|
const response = await fetch(endpoint, {
|
|
method,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({
|
|
id: note.id,
|
|
title: note.title,
|
|
content: note.content,
|
|
folder: selectedFolder
|
|
}),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error('Failed to save note');
|
|
}
|
|
|
|
// After successful save, refresh the notes list
|
|
const notesResponse = await fetch(`/api/nextcloud/files?folder=${selectedFolder}`);
|
|
if (notesResponse.ok) {
|
|
const updatedNotes = await notesResponse.json();
|
|
setNotes(updatedNotes);
|
|
}
|
|
} catch (error) {
|
|
console.error('Error saving note:', error);
|
|
}
|
|
};
|
|
|
|
const handleFolderSelect = async (folder: string) => {
|
|
console.log('Selected folder:', folder);
|
|
setSelectedFolder(folder);
|
|
setLayoutMode("item-selection");
|
|
|
|
// Reset selected contact when changing folders
|
|
setSelectedContact(null);
|
|
|
|
// Ensure folder exists in storage before fetching
|
|
try {
|
|
// Create the folder if it doesn't exist
|
|
const lowerFolder = folder.toLowerCase();
|
|
console.log(`Ensuring folder exists: ${lowerFolder}`);
|
|
|
|
const response = await fetch(`/api/storage/init/folder`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({ folder: lowerFolder })
|
|
});
|
|
|
|
if (!response.ok) {
|
|
console.warn(`Failed to create folder ${lowerFolder}: ${await response.text()}`);
|
|
}
|
|
} catch (error) {
|
|
console.error('Error creating folder:', error);
|
|
}
|
|
};
|
|
|
|
const handleContactSelect = (contact: Contact) => {
|
|
setSelectedContact(contact);
|
|
if (isMobile) {
|
|
setShowNotes(false);
|
|
}
|
|
};
|
|
|
|
const handleNewNote = () => {
|
|
setSelectedNote({
|
|
id: '',
|
|
title: '',
|
|
content: '',
|
|
lastModified: new Date().toISOString(),
|
|
type: 'file',
|
|
mime: 'text/markdown',
|
|
etag: ''
|
|
});
|
|
if (isMobile) {
|
|
setShowNotes(false);
|
|
}
|
|
};
|
|
|
|
const handleDeleteNote = async (note: Note) => {
|
|
try {
|
|
const response = await fetch(`/api/nextcloud/files`, {
|
|
method: 'DELETE',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({
|
|
id: note.id,
|
|
folder: selectedFolder
|
|
}),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error('Failed to delete note');
|
|
}
|
|
|
|
// Refresh the notes list
|
|
const notesResponse = await fetch(`/api/nextcloud/files?folder=${selectedFolder}`);
|
|
if (notesResponse.ok) {
|
|
const updatedNotes = await notesResponse.json();
|
|
setNotes(updatedNotes);
|
|
}
|
|
|
|
// If the deleted note was selected, clear the selection
|
|
if (selectedNote?.id === note.id) {
|
|
setSelectedNote(null);
|
|
}
|
|
} catch (error) {
|
|
console.error('Error deleting note:', error);
|
|
}
|
|
};
|
|
|
|
const handleContactSave = async (contact: Contact) => {
|
|
if (!session?.user?.id) return;
|
|
|
|
try {
|
|
setIsLoading(true);
|
|
|
|
// Always use Allemanique.vcf for new contacts
|
|
const basePath = `/files/cube-${session.user.id}/Private/Contacts`;
|
|
const vcfFile = 'Allemanique.vcf';
|
|
const path = `${basePath}/${vcfFile}`;
|
|
|
|
let vcfContent = '';
|
|
let existingContacts: string[] = [];
|
|
|
|
try {
|
|
// Try to get existing contacts from the VCF file
|
|
const response = await fetch(`/api/nextcloud/files/content?path=${encodeURIComponent(path)}`);
|
|
if (response.ok) {
|
|
const { content } = await response.json();
|
|
// Split the content into individual vCards
|
|
existingContacts = content.split('BEGIN:VCARD')
|
|
.filter((section: string) => section.trim())
|
|
.map((section: string) => 'BEGIN:VCARD' + section.trim());
|
|
}
|
|
} catch (error) {
|
|
// If the file doesn't exist, we'll create it with just the new contact
|
|
console.log('No existing VCF file found, will create a new one');
|
|
}
|
|
|
|
// Update or add the contact
|
|
let updatedVcards: string[] = [];
|
|
let contactUpdated = false;
|
|
|
|
for (const vcard of existingContacts) {
|
|
const parsed = parseVCard(vcard);
|
|
if (parsed.uid?.[0]?.value === contact.id) {
|
|
// Replace the existing 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}`] : []),
|
|
'END:VCARD'
|
|
].join('\n');
|
|
updatedVcards.push(newVcard);
|
|
contactUpdated = true;
|
|
} else {
|
|
// Keep the existing contact
|
|
updatedVcards.push(vcard);
|
|
}
|
|
}
|
|
|
|
// If contact wasn't found, add it as new
|
|
if (!contactUpdated) {
|
|
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}`] : []),
|
|
'END:VCARD'
|
|
].join('\n');
|
|
updatedVcards.push(newVcard);
|
|
}
|
|
|
|
// Join all vCards back together with proper spacing
|
|
vcfContent = updatedVcards.join('\n\n');
|
|
|
|
// Save the updated VCF file
|
|
const saveResponse = await fetch('/api/nextcloud/files', {
|
|
method: 'PUT',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({
|
|
id: path,
|
|
title: vcfFile,
|
|
content: vcfContent,
|
|
folder: 'Contacts',
|
|
mime: 'text/vcard'
|
|
}),
|
|
});
|
|
|
|
if (!saveResponse.ok) {
|
|
throw new Error('Failed to save contact');
|
|
}
|
|
|
|
// Refresh the contacts list
|
|
await fetchContacts(selectedFolder);
|
|
|
|
// Update the selected contact if it was edited
|
|
if (contactUpdated) {
|
|
setSelectedContact(contact);
|
|
}
|
|
} catch (error) {
|
|
console.error('Error saving contact:', error);
|
|
} finally {
|
|
setIsLoading(false);
|
|
}
|
|
};
|
|
|
|
const handleContactDelete = async (contact: Contact) => {
|
|
if (!confirm('Êtes-vous sûr de vouloir supprimer ce contact ?')) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
setIsLoading(true);
|
|
|
|
// Determine the correct VCF file path
|
|
const basePath = `/files/cube-${session?.user?.id}/Private/Contacts`;
|
|
const vcfFile = contact.group ? `${contact.group}.vcf` : 'contacts.vcf';
|
|
const path = `${basePath}/${vcfFile}`;
|
|
|
|
// Get existing contacts from the VCF file
|
|
const response = await fetch(`/api/nextcloud/files/content?path=${encodeURIComponent(path)}`);
|
|
if (!response.ok) {
|
|
throw new Error('Failed to fetch contacts');
|
|
}
|
|
|
|
const { content } = await response.json();
|
|
|
|
// Split the content into individual vCards and filter out the one to delete
|
|
const vcards = content.split('BEGIN:VCARD').filter((section: string) => section.trim());
|
|
const updatedVcards = vcards.filter((section: string) => {
|
|
const vcard = parseVCard('BEGIN:VCARD' + section);
|
|
return vcard.uid?.[0]?.value !== contact.id;
|
|
});
|
|
|
|
// Join the remaining vCards back together
|
|
const vcfContent = updatedVcards.map((section: string) => 'BEGIN:VCARD' + section).join('\n');
|
|
|
|
// Save the updated VCF file
|
|
const saveResponse = await fetch('/api/nextcloud/files', {
|
|
method: 'PUT',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({
|
|
id: path,
|
|
title: vcfFile,
|
|
content: vcfContent,
|
|
folder: 'Contacts',
|
|
mime: 'text/vcard'
|
|
}),
|
|
});
|
|
|
|
if (!saveResponse.ok) {
|
|
throw new Error('Failed to delete contact');
|
|
}
|
|
|
|
// Clear selected contact and refresh list
|
|
setSelectedContact(null);
|
|
await fetchContacts(selectedFolder);
|
|
} catch (error) {
|
|
console.error('Error deleting contact:', error);
|
|
} finally {
|
|
setIsLoading(false);
|
|
}
|
|
};
|
|
|
|
const generateVCardContent = (contact: Contact): string => {
|
|
const vcard = {
|
|
version: '3.0',
|
|
uid: contact.id,
|
|
fn: contact.fullName,
|
|
email: contact.email ? [{ value: contact.email, type: 'INTERNET' }] : undefined,
|
|
tel: contact.phone ? [{ value: contact.phone, type: 'CELL' }] : undefined,
|
|
org: contact.organization ? [{ value: contact.organization }] : undefined,
|
|
adr: contact.address ? [{ value: contact.address }] : undefined,
|
|
note: contact.notes ? [{ value: contact.notes }] : undefined,
|
|
categories: contact.group ? [{ value: contact.group }] : undefined
|
|
};
|
|
|
|
return formatVCard(vcard);
|
|
};
|
|
|
|
if (isLoading) {
|
|
return (
|
|
<div className="flex h-screen items-center justify-center">
|
|
<div className="h-32 w-32 animate-spin rounded-full border-t-2 border-b-2 border-gray-900"></div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<main className="w-full h-screen bg-black">
|
|
<div className="w-full h-full px-4 pt-12 pb-4">
|
|
<div className="flex h-full bg-carnet-bg">
|
|
{/* Navigation Panel */}
|
|
{showNav && (
|
|
<>
|
|
<div className="flex-none" style={{ width: navWidth }}>
|
|
<Navigation
|
|
nextcloudFolders={nextcloudFolders}
|
|
onFolderSelect={handleFolderSelect}
|
|
/>
|
|
</div>
|
|
<PanelResizer
|
|
isDragging={isDraggingNav}
|
|
onDragStart={() => setIsDraggingNav(true)}
|
|
onDragEnd={() => setIsDraggingNav(false)}
|
|
onDrag={handleNavResize}
|
|
/>
|
|
</>
|
|
)}
|
|
|
|
{/* Notes/Contacts Panel */}
|
|
{showNotes && (
|
|
<>
|
|
<div className="flex-1 overflow-hidden">
|
|
{selectedFolder === 'Contacts' ? (
|
|
<ContactsView
|
|
contacts={contacts}
|
|
onContactSelect={handleContactSelect}
|
|
selectedContact={selectedContact}
|
|
loading={isLoadingNotes}
|
|
/>
|
|
) : (
|
|
<NotesView
|
|
notes={notes}
|
|
loading={isLoadingNotes}
|
|
onNoteSelect={handleNoteSelect}
|
|
currentFolder={selectedFolder}
|
|
onNewNote={handleNewNote}
|
|
onDeleteNote={handleDeleteNote}
|
|
/>
|
|
)}
|
|
</div>
|
|
|
|
{/* Notes Resizer */}
|
|
<PanelResizer
|
|
isDragging={isDraggingNotes}
|
|
onDragStart={() => setIsDraggingNotes(true)}
|
|
onDragEnd={() => setIsDraggingNotes(false)}
|
|
onDrag={handleNotesResize}
|
|
/>
|
|
</>
|
|
)}
|
|
|
|
{/* Editor Panel */}
|
|
<div className="flex-1 overflow-hidden">
|
|
{selectedFolder === 'Contacts' || selectedFolder.endsWith('.vcf') ? (
|
|
<ContactDetails
|
|
contact={selectedContact}
|
|
onSave={handleContactSave}
|
|
onDelete={handleContactDelete}
|
|
/>
|
|
) : (
|
|
<Editor
|
|
note={selectedNote}
|
|
onSave={handleSaveNote}
|
|
currentFolder={selectedFolder}
|
|
onRefresh={() => {
|
|
// Refresh the notes list
|
|
fetchNotes();
|
|
}}
|
|
/>
|
|
)}
|
|
</div>
|
|
|
|
{/* Mobile Navigation Toggle */}
|
|
{isMobile && (
|
|
<button
|
|
onClick={() => setShowNav(!showNav)}
|
|
className="fixed bottom-4 right-4 bg-primary text-white p-3 rounded-full shadow-lg"
|
|
>
|
|
{showNav ? <X className="h-6 w-6" /> : <Menu className="h-6 w-6" />}
|
|
</button>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</main>
|
|
);
|
|
} |