diff --git a/app/pages/page.tsx b/app/pages/page.tsx index e12b38f..5234304 100644 --- a/app/pages/page.tsx +++ b/app/pages/page.tsx @@ -498,37 +498,40 @@ export default function CarnetPage() { // For Diary and Health, ensure title is formatted with date let noteTitle = note.title || 'Untitled'; - if ((selectedFolder === 'Diary' || selectedFolder === 'Health') && !note.id) { - // For new notes in Diary/Health, use today's date as title (formatted for display) + let fileKey: string | undefined; + + if (selectedFolder === 'Diary' || selectedFolder === 'Health') { + // For Diary/Health, always use today's date as title (formatted for display) const today = new Date(); const dateStr = format(today, 'yyyy-MM-dd'); // For filename matching const dateTitle = format(today, 'd MMMM yyyy', { locale: fr }); // For display: "16 janvier 2026" noteTitle = dateTitle; - // Check if a note with this date already exists + // ALWAYS check if a note with this date already exists (even if note.id is set) + // This ensures we update the existing note instead of creating a duplicate const existingNote = notes.find(n => { const title = n.title || ''; - return title.startsWith(dateStr) || + const noteId = n.id || ''; + // Check multiple patterns to find the existing note + return title === dateTitle || + title === dateStr || + title.startsWith(dateStr) || title.startsWith(dateTitle) || - n.id?.includes(dateStr); + noteId.includes(dateStr) || + noteId.includes(dateTitle.replace(/\s/g, '_')); }); if (existingNote) { // Update the existing note instead of creating a new one console.log(`[handleSaveNote] Found existing note for today, updating: ${existingNote.id}`); note.id = existingNote.id; - noteTitle = existingNote.title; // Keep the existing title format - } - } - - // For Diary/Health, always reconstruct filename from formatted title to ensure new format - // For other folders, use existing id if available, otherwise construct from title - let fileKey: string | undefined; - if (selectedFolder === 'Diary' || selectedFolder === 'Health') { - // Always use the formatted date title for the filename (will be sanitized by API) - // This ensures we use "16_janvier_2026.md" instead of "2026-01-16.md" - if (noteTitle) { - fileKey = `user-${session?.user?.id}/${selectedFolder.toLowerCase()}/${noteTitle}${noteTitle.endsWith('.md') ? '' : '.md'}`; + noteTitle = existingNote.title || dateTitle; // Keep the existing title format + // Use the existing note's id as the fileKey + fileKey = existingNote.id; + } else { + // No existing note found, create new one with formatted date title + // The fileKey will be constructed from the formatted title + fileKey = `user-${session?.user?.id}/${selectedFolder.toLowerCase()}/${dateTitle}${dateTitle.endsWith('.md') ? '' : '.md'}`; } } else { // For other folders, use existing id or construct from title @@ -552,9 +555,13 @@ export default function CarnetPage() { // Use direct storage API endpoint const endpoint = '/api/storage/files'; - const method = note.id ? 'PUT' : 'POST'; + // For Diary/Health, always use PUT (we always have a fileKey, either from existing note or constructed) + // For other folders, use PUT if note.id exists, otherwise POST + const method = (selectedFolder === 'Diary' || selectedFolder === 'Health') + ? 'PUT' // Always PUT for Diary/Health since we always have a known fileKey + : (note.id ? 'PUT' : 'POST'); - console.log(`Saving note to ${selectedFolder.toLowerCase()} using ${method}, title: ${noteTitle}`); + console.log(`Saving note to ${selectedFolder.toLowerCase()} using ${method}, title: ${noteTitle}, fileKey: ${fileKey}`); const response = await fetch(endpoint, { method,