From 05d97e2c5046c99472d4325ae842146bcd25d54f Mon Sep 17 00:00:00 2001 From: alma Date: Sun, 11 Jan 2026 23:18:00 +0100 Subject: [PATCH] pages --- app/pages/page.tsx | 29 +++++++++++++++++++++++++++++ components/notes-dialog.tsx | 23 +++++++++++++++++++++-- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/app/pages/page.tsx b/app/pages/page.tsx index bacada3..f8a9ea9 100644 --- a/app/pages/page.tsx +++ b/app/pages/page.tsx @@ -180,6 +180,35 @@ export default function CarnetPage() { } }, [selectedFolder, session?.user?.id]); + // Listen for note-saved events to refresh the list + useEffect(() => { + const handleNoteSaved = (event: CustomEvent) => { + const folder = event.detail?.folder; + if (folder && selectedFolder.toLowerCase() === folder.toLowerCase()) { + console.log('Note saved event received, refreshing notes list'); + // Clear cache and fetch fresh notes + const cacheKey = `${session?.user?.id}-${folder}`; + if (notesCache.current[cacheKey]) { + delete notesCache.current[cacheKey]; + } + try { + localStorage.removeItem(`notes-cache-${cacheKey}`); + } catch (error) { + console.error('Error removing cache:', error); + } + // Fetch notes will be called by the existing useEffect when selectedFolder changes + // But we can also call it directly here + fetchNotes(); + } + }; + + window.addEventListener('note-saved', handleNoteSaved as EventListener); + return () => { + window.removeEventListener('note-saved', handleNoteSaved as EventListener); + }; + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [selectedFolder, session?.user?.id]); + const parseVCardContent = (content: string): Contact[] => { try { // Split the content into individual vCards diff --git a/components/notes-dialog.tsx b/components/notes-dialog.tsx index c016c1e..f5b0ee6 100644 --- a/components/notes-dialog.tsx +++ b/components/notes-dialog.tsx @@ -55,11 +55,11 @@ export function NotesDialog({ open, onOpenChange }: NotesDialogProps) { setError(""); // Construct API payload with lowercase folder name (always "notes" for quick notes) + // Note: The API will construct the full path from folder and title const payload = { - id: `user-${session?.user?.id}/notes/${title}${title.endsWith('.md') ? '' : '.md'}`, title: title, content: content, - folder: "notes", // Always save to Notes folder + folder: "notes", // Always save to Notes folder (lowercase for consistency) mime: "text/markdown" }; @@ -73,9 +73,28 @@ export function NotesDialog({ open, onOpenChange }: NotesDialogProps) { }); if (!response.ok) { + const errorText = await response.text(); + console.error('Failed to save note:', errorText); throw new Error('Failed to save note'); } + // Invalidate cache and trigger refresh + try { + // Clear localStorage cache for notes + const userId = session?.user?.id; + if (userId) { + const cacheKey = `${userId}-notes`; + localStorage.removeItem(`notes-cache-${cacheKey}`); + } + + // Dispatch custom event to notify pages to refresh + window.dispatchEvent(new CustomEvent('note-saved', { + detail: { folder: 'notes' } + })); + } catch (cacheError) { + console.error('Error clearing cache:', cacheError); + } + // Reset form and close dialog setTitle(""); setContent("");