Pages corrections pages missions
This commit is contained in:
parent
ea379e7e72
commit
f57cb42c79
@ -547,8 +547,37 @@ export default function CarnetPage() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// For other folders, use existing id or construct from title
|
// For other folders (Bloc-notes), check if a note with the same title already exists
|
||||||
fileKey = note.id || (noteTitle ? `user-${session?.user?.id}/${selectedFolder.toLowerCase()}/${noteTitle}${noteTitle.endsWith('.md') ? '' : '.md'}` : undefined);
|
// This prevents duplicates when creating new notes
|
||||||
|
if (!note.id || note.id.startsWith('temp-')) {
|
||||||
|
// This is a new note (no id or temporary id), check for duplicates
|
||||||
|
const sanitizedTitle = noteTitle ? noteTitle.replace(/[^a-zA-Z0-9._-]/g, '_') : 'Untitled';
|
||||||
|
const expectedFileKey = `user-${session?.user?.id}/${selectedFolder.toLowerCase()}/${sanitizedTitle}${sanitizedTitle.endsWith('.md') ? '' : '.md'}`;
|
||||||
|
|
||||||
|
// Check if a note with this title already exists
|
||||||
|
const existingNote = notes.find(n => {
|
||||||
|
const nTitle = n.title || '';
|
||||||
|
const nId = n.id || '';
|
||||||
|
// Check if title matches or if the id matches the expected fileKey
|
||||||
|
return (nTitle === noteTitle && noteTitle !== '') ||
|
||||||
|
(noteTitle === '' && (nTitle === 'Untitled' || nTitle === '' || nTitle.trim() === '')) ||
|
||||||
|
nId === expectedFileKey;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (existingNote) {
|
||||||
|
// Update the existing note instead of creating a new one
|
||||||
|
console.log(`[handleSaveNote] Found existing note with same title "${noteTitle}", updating: ${existingNote.id}`);
|
||||||
|
note.id = existingNote.id;
|
||||||
|
noteTitle = existingNote.title || noteTitle || 'Untitled';
|
||||||
|
fileKey = existingNote.id;
|
||||||
|
} else {
|
||||||
|
// No existing note found, use the expected fileKey
|
||||||
|
fileKey = expectedFileKey;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Note has an id, use it (this is an update to an existing note)
|
||||||
|
fileKey = note.id;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!fileKey) {
|
if (!fileKey) {
|
||||||
@ -861,7 +890,10 @@ export default function CarnetPage() {
|
|||||||
setShowNotes(false);
|
setShowNotes(false);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// For other folders (Bloc-notes), check if "Untitled" already exists
|
// For other folders (Bloc-notes), refresh notes list first
|
||||||
|
await fetchNotes(true);
|
||||||
|
|
||||||
|
// Check if "Untitled" already exists
|
||||||
const untitledNote = notes.find(note => {
|
const untitledNote = notes.find(note => {
|
||||||
const title = note.title || '';
|
const title = note.title || '';
|
||||||
return title === 'Untitled' || title === '' || title.trim() === '';
|
return title === 'Untitled' || title === '' || title.trim() === '';
|
||||||
@ -874,9 +906,11 @@ export default function CarnetPage() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a blank note
|
// Create a blank note with a temporary unique id to track it
|
||||||
|
// This id will be used in handleSaveNote to check for duplicates
|
||||||
|
const tempId = `temp-${Date.now()}`;
|
||||||
setSelectedNote({
|
setSelectedNote({
|
||||||
id: '',
|
id: tempId, // Temporary id to track this new note
|
||||||
title: '',
|
title: '',
|
||||||
content: '',
|
content: '',
|
||||||
lastModified: new Date().toISOString(),
|
lastModified: new Date().toISOString(),
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user