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"
noteTitle = dateTitle;
// 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
console.log(`[handleSaveNote] Checking for existing note. Date: ${dateTitle}, DateStr: ${dateStr}, Notes count: ${notes.length}`);
console.log(`[handleSaveNote] Current notes:`, notes.map(n => ({ title: n.title, id: n.id })));
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}`);
// If note.id is already set (from handleNewNote), use it directly
// This ensures we use the same fileKey that was pre-set
if (note.id && note.id.includes(`user-${session?.user?.id}/${selectedFolder.toLowerCase()}/`)) {
fileKey = note.id;
console.log(`[handleSaveNote] Using pre-set fileKey from note.id: ${fileKey}`);
} 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'}`;
console.log(`[handleSaveNote] No existing note found, creating new with fileKey: ${fileKey}`);
// ALWAYS check if a note with this date already exists
// This ensures we update the existing note instead of creating a duplicate
console.log(`[handleSaveNote] Checking for existing note. Date: ${dateTitle}, DateStr: ${dateStr}, Notes count: ${notes.length}`);
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 {
// 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
if (selectedFolder === 'Diary' || selectedFolder === 'Health') {
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"
// Refresh notes list first to ensure we have the latest data
await fetchNotes(true);
// Check if a note with this date already exists
// We check both the date string (YYYY-MM-DD) and the formatted title
const existingNote = notes.find(note => {
// Check if note title matches today's date in any format
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) ||
note.id?.includes(dateStr);
noteId.includes(dateStr) ||
noteId.includes(dateTitle.replace(/\s/g, '_')) ||
noteId.includes(dateTitle.replace(/\s/g, '-'));
});
if (existingNote) {
// 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);
return;
}
// 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 = {
id: '',
id: expectedFileKey, // Pre-set the id to the expected fileKey
title: dateTitle, // Use formatted date for display: "16 janvier 2026"
content: '',
lastModified: today.toISOString(),
@ -843,12 +855,26 @@ export default function CarnetPage() {
etag: ''
};
console.log(`[handleNewNote] Creating new note for today with id: ${expectedFileKey}`);
setSelectedNote(newNote);
if (isMobile) {
setShowNotes(false);
}
} 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({
id: '',
title: '',