diff --git a/app/pages/page.tsx b/app/pages/page.tsx index f3e2ed9..678f3fd 100644 --- a/app/pages/page.tsx +++ b/app/pages/page.tsx @@ -551,8 +551,26 @@ export default function CarnetPage() { // Refresh notes list to ensure we have the latest data await fetchNotes(true); - // If note has a valid id (not temp), use it directly - if (note.id && !note.id.startsWith('temp-') && note.id.includes(`user-${session?.user?.id}/`)) { + // Check if the current note.id corresponds to an "Untitled" note + // and if we're giving it a new title (renaming scenario) + const isRenamingUntitled = note.id && + !note.id.startsWith('temp-') && + note.id.includes(`user-${session?.user?.id}/`) && + note.id.includes('/Untitled.md') && + noteTitle && + noteTitle.trim() !== '' && + noteTitle !== 'Untitled'; + + if (isRenamingUntitled) { + // We're renaming an Untitled note to a new title + const sanitizedTitle = noteTitle.replace(/[^a-zA-Z0-9._-]/g, '_'); + fileKey = `user-${session?.user?.id}/${selectedFolder.toLowerCase()}/${sanitizedTitle}.md`; + console.log(`[handleSaveNote] Renaming Untitled note to "${noteTitle}": ${note.id} -> ${fileKey}`); + // Store the old id to delete it later + (note as any)._oldUntitledId = note.id; + note.id = fileKey; // Update note.id to the new fileKey + } else if (note.id && !note.id.startsWith('temp-') && note.id.includes(`user-${session?.user?.id}/`)) { + // Note has a valid id and we're not renaming, use it directly fileKey = note.id; console.log(`[handleSaveNote] Using existing note id: ${fileKey}`); } else {