Pages corrections pages missions

This commit is contained in:
alma 2026-01-16 15:25:10 +01:00
parent ebe1f97acc
commit ea379e7e72

View File

@ -507,42 +507,44 @@ export default function CarnetPage() {
const dateTitle = format(today, 'd MMMM yyyy', { locale: fr }); // For display: "16 janvier 2026" const dateTitle = format(today, 'd MMMM yyyy', { locale: fr }); // For display: "16 janvier 2026"
noteTitle = dateTitle; noteTitle = dateTitle;
// ALWAYS check if a note with this date already exists (even if note.id is set) // If note.id is already set (from handleNewNote), use it directly
// This ensures we update the existing note instead of creating a duplicate // This ensures we use the same fileKey that was pre-set
console.log(`[handleSaveNote] Checking for existing note. Date: ${dateTitle}, DateStr: ${dateStr}, Notes count: ${notes.length}`); if (note.id && note.id.includes(`user-${session?.user?.id}/${selectedFolder.toLowerCase()}/`)) {
console.log(`[handleSaveNote] Current notes:`, notes.map(n => ({ title: n.title, id: n.id }))); fileKey = note.id;
console.log(`[handleSaveNote] Using pre-set fileKey from note.id: ${fileKey}`);
const existingNote = notes.find(n => {
const title = n.title || '';
const noteId = n.id || '';
// Check multiple patterns to find the existing note
const matches = title === dateTitle ||
title === dateStr ||
title.startsWith(dateStr) ||
title.startsWith(dateTitle) ||
noteId.includes(dateStr) ||
noteId.includes(dateTitle.replace(/\s/g, '_')) ||
noteId.includes(dateTitle.replace(/\s/g, '-'));
if (matches) {
console.log(`[handleSaveNote] Found matching note:`, { title, id: noteId });
}
return matches;
});
if (existingNote) {
// Update the existing note instead of creating a new one
console.log(`[handleSaveNote] Found existing note for today, updating: ${existingNote.id}, title: ${existingNote.title}`);
note.id = existingNote.id;
noteTitle = existingNote.title || dateTitle; // Keep the existing title format
// Use the existing note's id as the fileKey - this is critical!
fileKey = existingNote.id;
console.log(`[handleSaveNote] Using existing fileKey: ${fileKey}`);
} else { } else {
// No existing note found, create new one with formatted date title // ALWAYS check if a note with this date already exists
// The fileKey will be constructed from the formatted title // This ensures we update the existing note instead of creating a duplicate
fileKey = `user-${session?.user?.id}/${selectedFolder.toLowerCase()}/${dateTitle}${dateTitle.endsWith('.md') ? '' : '.md'}`; console.log(`[handleSaveNote] Checking for existing note. Date: ${dateTitle}, DateStr: ${dateStr}, Notes count: ${notes.length}`);
console.log(`[handleSaveNote] No existing note found, creating new with fileKey: ${fileKey}`);
const existingNote = notes.find(n => {
const title = n.title || '';
const noteId = n.id || '';
// Check multiple patterns to find the existing note
return title === dateTitle ||
title === dateStr ||
title.startsWith(dateStr) ||
title.startsWith(dateTitle) ||
noteId.includes(dateStr) ||
noteId.includes(dateTitle.replace(/\s/g, '_')) ||
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}, title: ${existingNote.title}`);
note.id = existingNote.id;
noteTitle = existingNote.title || dateTitle; // Keep the existing title format
// Use the existing note's id as the fileKey - this is critical!
fileKey = existingNote.id;
console.log(`[handleSaveNote] Using existing fileKey: ${fileKey}`);
} else {
// No existing note found, create new one with formatted date title
// Sanitize the title to match the format used in S3
const sanitizedTitle = dateTitle.replace(/[^a-zA-Z0-9._-]/g, '_');
fileKey = `user-${session?.user?.id}/${selectedFolder.toLowerCase()}/${sanitizedTitle}.md`;
console.log(`[handleSaveNote] No existing note found, creating new with fileKey: ${fileKey}`);
}
} }
} else { } else {
// For other folders, use existing id or construct from title // For other folders, use existing id or construct from title
@ -812,29 +814,39 @@ export default function CarnetPage() {
// For Diary and Health folders, check if a note exists for today // For Diary and Health folders, check if a note exists for today
if (selectedFolder === 'Diary' || selectedFolder === 'Health') { if (selectedFolder === 'Diary' || selectedFolder === 'Health') {
const today = new Date(); const today = new Date();
const dateStr = format(today, 'yyyy-MM-dd'); // For filename const dateStr = format(today, 'yyyy-MM-dd'); // For filename matching
const dateTitle = format(today, 'd MMMM yyyy', { locale: fr }); // For display: "16 janvier 2026" const dateTitle = format(today, 'd MMMM yyyy', { locale: fr }); // For display: "16 janvier 2026"
// Refresh notes list first to ensure we have the latest data
await fetchNotes(true);
// Check if a note with this date already exists // Check if a note with this date already exists
// We check both the date string (YYYY-MM-DD) and the formatted title // We check both the date string (YYYY-MM-DD) and the formatted title
const existingNote = notes.find(note => { const existingNote = notes.find(note => {
// Check if note title matches today's date in any format
const title = note.title || ''; const title = note.title || '';
return title.startsWith(dateStr) || const noteId = note.id || '';
// Check multiple patterns to find the existing note
return title === dateTitle ||
title === dateStr ||
title.startsWith(dateStr) ||
title.startsWith(dateTitle) || title.startsWith(dateTitle) ||
note.id?.includes(dateStr); noteId.includes(dateStr) ||
noteId.includes(dateTitle.replace(/\s/g, '_')) ||
noteId.includes(dateTitle.replace(/\s/g, '-'));
}); });
if (existingNote) { if (existingNote) {
// Open the existing note for today // Open the existing note for today
console.log(`[handleNewNote] Found existing note for today: ${existingNote.title}`); console.log(`[handleNewNote] Found existing note for today: ${existingNote.title}, id: ${existingNote.id}`);
handleNoteSelect(existingNote); handleNoteSelect(existingNote);
return; return;
} }
// Create a new note with today's date as title (formatted for display) // Create a new note with today's date as title (formatted for display)
// Set the id to the expected fileKey so handleSaveNote will use it
const expectedFileKey = `user-${session?.user?.id}/${selectedFolder.toLowerCase()}/${dateTitle.replace(/\s/g, '_')}.md`;
const newNote: Note = { const newNote: Note = {
id: '', id: expectedFileKey, // Pre-set the id to the expected fileKey
title: dateTitle, // Use formatted date for display: "16 janvier 2026" title: dateTitle, // Use formatted date for display: "16 janvier 2026"
content: '', content: '',
lastModified: today.toISOString(), lastModified: today.toISOString(),
@ -843,12 +855,26 @@ export default function CarnetPage() {
etag: '' etag: ''
}; };
console.log(`[handleNewNote] Creating new note for today with id: ${expectedFileKey}`);
setSelectedNote(newNote); setSelectedNote(newNote);
if (isMobile) { if (isMobile) {
setShowNotes(false); setShowNotes(false);
} }
} else { } else {
// For other folders, create a blank note // For other folders (Bloc-notes), check if "Untitled" already exists
const untitledNote = notes.find(note => {
const title = note.title || '';
return title === 'Untitled' || title === '' || title.trim() === '';
});
if (untitledNote) {
// Open the existing untitled note
console.log(`[handleNewNote] Found existing untitled note: ${untitledNote.id}`);
handleNoteSelect(untitledNote);
return;
}
// Create a blank note
setSelectedNote({ setSelectedNote({
id: '', id: '',
title: '', title: '',