diff --git a/app/pages/page.tsx b/app/pages/page.tsx index 603d378..932d6a7 100644 --- a/app/pages/page.tsx +++ b/app/pages/page.tsx @@ -384,7 +384,7 @@ export default function CarnetPage() { }; // Fetch notes based on the selected folder - const fetchNotes = async () => { + const fetchNotes = async (skipCache = false) => { if (!session?.user?.id) { setIsLoadingNotes(false); return; @@ -395,17 +395,18 @@ export default function CarnetPage() { // Convert folder name to lowercase for consistent storage access const folderLowercase = selectedFolder.toLowerCase(); - console.log(`Fetching notes from folder: ${folderLowercase}`); + console.log(`[fetchNotes] Fetching notes from folder: ${folderLowercase}, skipCache: ${skipCache}`); - // Check cache first + // Check cache first (unless skipCache is true) const cacheKey = `${session.user.id}-${folderLowercase}`; - const cachedNotes = notesCache.get(cacheKey); - - if (cachedNotes) { - console.log(`Using cached notes for ${folderLowercase} folder`); - setNotes(cachedNotes); - setIsLoadingNotes(false); - return; + if (!skipCache) { + const cachedNotes = notesCache.get(cacheKey); + if (cachedNotes) { + console.log(`[fetchNotes] Using cached notes for ${folderLowercase} folder`); + setNotes(cachedNotes); + setIsLoadingNotes(false); + return; + } } // Fetch from API @@ -413,7 +414,7 @@ export default function CarnetPage() { if (response.ok) { const data = await response.json(); - console.log(`Fetched ${data.length} files from ${folderLowercase} folder`, data); + console.log(`[fetchNotes] Fetched ${data.length} files from ${folderLowercase} folder`, data); // Map API response to Note format // API returns: { key, name, size, lastModified } @@ -433,20 +434,25 @@ export default function CarnetPage() { }; }); - console.log(`Mapped ${mappedNotes.length} notes from ${folderLowercase} folder`); + // Remove duplicates based on id + const uniqueNotes = mappedNotes.filter((note, index, self) => + index === self.findIndex(n => n.id === note.id) + ); + + console.log(`[fetchNotes] Mapped ${uniqueNotes.length} unique notes from ${folderLowercase} folder (${mappedNotes.length} total before deduplication)`); // Update state - setNotes(mappedNotes); + setNotes(uniqueNotes); // Update cache - notesCache.set(cacheKey, mappedNotes); + notesCache.set(cacheKey, uniqueNotes); } else { const errorData = await response.json().catch(() => ({})); - console.error('Error fetching notes:', errorData.message || response.statusText); + console.error('[fetchNotes] Error fetching notes:', errorData.message || response.statusText); setNotes([]); } } catch (error) { - console.error('Error fetching notes:', error); + console.error('[fetchNotes] Error fetching notes:', error); setNotes([]); } finally { setIsLoadingNotes(false); @@ -492,8 +498,10 @@ export default function CarnetPage() { noteContentCache.set(payload.id, payload.content); } - // Refresh the list of notes - fetchNotes(); + // Refresh the list of notes (skip cache to get fresh data) + // Note: onRefresh from Editor will also call fetchNotes, but with skipCache=false + // So we call it with skipCache=true here to ensure fresh data + fetchNotes(true); } else { const errorData = await response.json().catch(() => ({})); const errorMessage = errorData.message || errorData.error || 'Failed to save note'; diff --git a/components/carnet/editor.tsx b/components/carnet/editor.tsx index 03584ef..ded4801 100644 --- a/components/carnet/editor.tsx +++ b/components/carnet/editor.tsx @@ -180,7 +180,9 @@ export const Editor: React.FC = ({ note, onSave, currentFolder = 'N ...savedNote, content }); - onRefresh?.(); + // Note: onRefresh is called, but handleSaveNote already calls fetchNotes + // So we don't need to call onRefresh here to avoid double fetch + // onRefresh?.(); } catch (error) { console.error('Error saving note:', error); } finally {