Pages corrections pages missions
This commit is contained in:
parent
ebe1f97acc
commit
ea379e7e72
@ -507,27 +507,27 @@ 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 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 {
|
||||||
|
// ALWAYS check if a note with this date already exists
|
||||||
// This ensures we update the existing note instead of creating a duplicate
|
// 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] 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 existingNote = notes.find(n => {
|
||||||
const title = n.title || '';
|
const title = n.title || '';
|
||||||
const noteId = n.id || '';
|
const noteId = n.id || '';
|
||||||
// Check multiple patterns to find the existing note
|
// Check multiple patterns to find the existing note
|
||||||
const matches = title === dateTitle ||
|
return title === dateTitle ||
|
||||||
title === dateStr ||
|
title === dateStr ||
|
||||||
title.startsWith(dateStr) ||
|
title.startsWith(dateStr) ||
|
||||||
title.startsWith(dateTitle) ||
|
title.startsWith(dateTitle) ||
|
||||||
noteId.includes(dateStr) ||
|
noteId.includes(dateStr) ||
|
||||||
noteId.includes(dateTitle.replace(/\s/g, '_')) ||
|
noteId.includes(dateTitle.replace(/\s/g, '_')) ||
|
||||||
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) {
|
if (existingNote) {
|
||||||
@ -540,10 +540,12 @@ export default function CarnetPage() {
|
|||||||
console.log(`[handleSaveNote] Using existing fileKey: ${fileKey}`);
|
console.log(`[handleSaveNote] Using existing fileKey: ${fileKey}`);
|
||||||
} else {
|
} else {
|
||||||
// No existing note found, create new one with formatted date title
|
// No existing note found, create new one with formatted date title
|
||||||
// The fileKey will be constructed from the formatted title
|
// Sanitize the title to match the format used in S3
|
||||||
fileKey = `user-${session?.user?.id}/${selectedFolder.toLowerCase()}/${dateTitle}${dateTitle.endsWith('.md') ? '' : '.md'}`;
|
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}`);
|
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
|
||||||
fileKey = note.id || (noteTitle ? `user-${session?.user?.id}/${selectedFolder.toLowerCase()}/${noteTitle}${noteTitle.endsWith('.md') ? '' : '.md'}` : undefined);
|
fileKey = note.id || (noteTitle ? `user-${session?.user?.id}/${selectedFolder.toLowerCase()}/${noteTitle}${noteTitle.endsWith('.md') ? '' : '.md'}` : undefined);
|
||||||
@ -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: '',
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user