From 50a7b0c6969b38cd6769a6a4bba0b31f5cd205ad Mon Sep 17 00:00:00 2001 From: alma Date: Fri, 16 Jan 2026 15:49:34 +0100 Subject: [PATCH] Pages corrections pages missions --- app/pages/page.tsx | 78 +++++++++++++++++++++++++++++------- components/carnet/editor.tsx | 7 +++- 2 files changed, 69 insertions(+), 16 deletions(-) diff --git a/app/pages/page.tsx b/app/pages/page.tsx index 678f3fd..ff8155b 100644 --- a/app/pages/page.tsx +++ b/app/pages/page.tsx @@ -548,8 +548,18 @@ export default function CarnetPage() { } } else { // For other folders (Bloc-notes) + console.log(`[handleSaveNote] BLOC-NOTES: Starting save process`); + console.log(`[handleSaveNote] BLOC-NOTES: note.id = "${note.id}"`); + console.log(`[handleSaveNote] BLOC-NOTES: noteTitle = "${noteTitle}"`); + console.log(`[handleSaveNote] BLOC-NOTES: note.title = "${note.title}"`); + console.log(`[handleSaveNote] BLOC-NOTES: Current notes count: ${notes.length}`); + console.log(`[handleSaveNote] BLOC-NOTES: Current notes:`, notes.map(n => ({ id: n.id, title: n.title }))); + // Refresh notes list to ensure we have the latest data + console.log(`[handleSaveNote] BLOC-NOTES: Refreshing notes list...`); await fetchNotes(true); + console.log(`[handleSaveNote] BLOC-NOTES: After refresh, notes count: ${notes.length}`); + console.log(`[handleSaveNote] BLOC-NOTES: After refresh, notes:`, notes.map(n => ({ id: n.id, title: n.title }))); // Check if the current note.id corresponds to an "Untitled" note // and if we're giving it a new title (renaming scenario) @@ -561,19 +571,25 @@ export default function CarnetPage() { noteTitle.trim() !== '' && noteTitle !== 'Untitled'; + console.log(`[handleSaveNote] BLOC-NOTES: isRenamingUntitled = ${isRenamingUntitled}`); + if (isRenamingUntitled) { + console.log(`[handleSaveNote] BLOC-NOTES: Detected renaming scenario`); + } + 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}`); + console.log(`[handleSaveNote] BLOC-NOTES: 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}`); + console.log(`[handleSaveNote] BLOC-NOTES: Using existing note id directly: ${fileKey}`); } else { + console.log(`[handleSaveNote] BLOC-NOTES: Entering else branch (new note or temp id)`); // This is a new note or note with temp id // Strategy: If we have a title, check for existing note with that title // If no title or title is "Untitled", check for existing "Untitled" note @@ -581,33 +597,44 @@ export default function CarnetPage() { if (noteTitle && noteTitle.trim() !== '' && noteTitle !== 'Untitled') { // Note has a real title, check if a note with this title already exists + console.log(`[handleSaveNote] BLOC-NOTES: Note has real title: "${noteTitle}"`); const sanitizedTitle = noteTitle.replace(/[^a-zA-Z0-9._-]/g, '_'); const expectedFileKey = `user-${session?.user?.id}/${selectedFolder.toLowerCase()}/${sanitizedTitle}.md`; + console.log(`[handleSaveNote] BLOC-NOTES: Expected fileKey: ${expectedFileKey}`); const existingNoteWithTitle = notes.find(n => { const nTitle = n.title || ''; const nId = n.id || ''; // Check if title matches exactly or if the id matches the expected fileKey - return (nTitle === noteTitle && nTitle !== 'Untitled') || nId === expectedFileKey; + const matches = (nTitle === noteTitle && nTitle !== 'Untitled') || nId === expectedFileKey; + if (matches) { + console.log(`[handleSaveNote] BLOC-NOTES: Found matching note:`, { id: nId, title: nTitle }); + } + return matches; }); if (existingNoteWithTitle) { // Update the existing note with this title - console.log(`[handleSaveNote] Found existing note with title "${noteTitle}", updating: ${existingNoteWithTitle.id}`); + console.log(`[handleSaveNote] BLOC-NOTES: Found existing note with title "${noteTitle}", updating: ${existingNoteWithTitle.id}`); note.id = existingNoteWithTitle.id; fileKey = existingNoteWithTitle.id; } else { // Check if there's an "Untitled" note we should update (user added title to Untitled note) + console.log(`[handleSaveNote] BLOC-NOTES: No note with title "${noteTitle}" found, checking for Untitled note...`); const untitledNote = notes.find(n => { const nTitle = n.title || ''; - return (nTitle === 'Untitled' || nTitle === '' || nTitle.trim() === '') && + const isUntitled = (nTitle === 'Untitled' || nTitle === '' || nTitle.trim() === '') && n.id && n.id.includes(`user-${session?.user?.id}/${selectedFolder.toLowerCase()}/`); + if (isUntitled) { + console.log(`[handleSaveNote] BLOC-NOTES: Found Untitled note:`, { id: n.id, title: nTitle }); + } + return isUntitled; }); if (untitledNote) { // Rename the Untitled note with the new title // Use the new fileKey based on the new title - console.log(`[handleSaveNote] Renaming Untitled note to "${noteTitle}": ${untitledNote.id} -> ${expectedFileKey}`); + console.log(`[handleSaveNote] BLOC-NOTES: Renaming Untitled note to "${noteTitle}": ${untitledNote.id} -> ${expectedFileKey}`); // Store the old id to delete it later const oldUntitledId = untitledNote.id; fileKey = expectedFileKey; @@ -618,27 +645,33 @@ export default function CarnetPage() { } else { // No existing note found, create new one fileKey = expectedFileKey; - console.log(`[handleSaveNote] Creating new note with fileKey: ${fileKey}`); + console.log(`[handleSaveNote] BLOC-NOTES: No existing note found, creating new one with fileKey: ${fileKey}`); } } } else { + console.log(`[handleSaveNote] BLOC-NOTES: Note has no title or title is "Untitled"`); // No title or title is "Untitled", check for existing Untitled note + console.log(`[handleSaveNote] BLOC-NOTES: Searching for existing Untitled note...`); const untitledNote = notes.find(n => { const nTitle = n.title || ''; - return (nTitle === 'Untitled' || nTitle === '' || nTitle.trim() === '') && + const isUntitled = (nTitle === 'Untitled' || nTitle === '' || nTitle.trim() === '') && n.id && n.id.includes(`user-${session?.user?.id}/${selectedFolder.toLowerCase()}/`); + if (isUntitled) { + console.log(`[handleSaveNote] BLOC-NOTES: Found Untitled note:`, { id: n.id, title: nTitle }); + } + return isUntitled; }); if (untitledNote) { // Use existing Untitled note - console.log(`[handleSaveNote] Using existing Untitled note: ${untitledNote.id}`); + console.log(`[handleSaveNote] BLOC-NOTES: Using existing Untitled note: ${untitledNote.id}`); note.id = untitledNote.id; noteTitle = 'Untitled'; fileKey = untitledNote.id; } else { // Create new Untitled note fileKey = `user-${session?.user?.id}/${selectedFolder.toLowerCase()}/Untitled.md`; - console.log(`[handleSaveNote] Creating new Untitled note: ${fileKey}`); + console.log(`[handleSaveNote] BLOC-NOTES: No Untitled note found, creating new one: ${fileKey}`); } } } @@ -979,18 +1012,30 @@ export default function CarnetPage() { setShowNotes(false); } } else { - // For other folders (Bloc-notes), refresh notes list first + // For other folders (Bloc-notes) + console.log(`[handleNewNote] BLOC-NOTES: Starting new note creation`); + console.log(`[handleNewNote] BLOC-NOTES: Current notes count: ${notes.length}`); + console.log(`[handleNewNote] BLOC-NOTES: Current notes:`, notes.map(n => ({ id: n.id, title: n.title }))); + + // Refresh notes list first + console.log(`[handleNewNote] BLOC-NOTES: Refreshing notes list...`); await fetchNotes(true); + console.log(`[handleNewNote] BLOC-NOTES: After refresh, notes count: ${notes.length}`); + console.log(`[handleNewNote] BLOC-NOTES: After refresh, notes:`, notes.map(n => ({ id: n.id, title: n.title }))); // Check if "Untitled" already exists const untitledNote = notes.find(note => { const title = note.title || ''; - return title === 'Untitled' || title === '' || title.trim() === ''; + const isUntitled = title === 'Untitled' || title === '' || title.trim() === ''; + if (isUntitled) { + console.log(`[handleNewNote] BLOC-NOTES: Found potential Untitled note:`, { id: note.id, title: note.title }); + } + return isUntitled; }); if (untitledNote) { // Open the existing untitled note - console.log(`[handleNewNote] Found existing untitled note: ${untitledNote.id}`); + console.log(`[handleNewNote] BLOC-NOTES: Found existing untitled note, opening it:`, { id: untitledNote.id, title: untitledNote.title }); handleNoteSelect(untitledNote); return; } @@ -998,7 +1043,8 @@ export default function CarnetPage() { // 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({ + console.log(`[handleNewNote] BLOC-NOTES: No existing Untitled note found, creating new note with temp id: ${tempId}`); + const newNote = { id: tempId, // Temporary id to track this new note title: '', content: '', @@ -1006,7 +1052,9 @@ export default function CarnetPage() { type: 'file', mime: 'text/markdown', etag: '' - }); + }; + console.log(`[handleNewNote] BLOC-NOTES: Setting selectedNote:`, newNote); + setSelectedNote(newNote); if (isMobile) { setShowNotes(false); } diff --git a/components/carnet/editor.tsx b/components/carnet/editor.tsx index 7937ed8..3a5e6e4 100644 --- a/components/carnet/editor.tsx +++ b/components/carnet/editor.tsx @@ -188,12 +188,17 @@ export const Editor: React.FC = ({ note, onSave, currentFolder = 'N clearTimeout(saveTimeout.current); } saveTimeout.current = setTimeout(() => { + console.log(`[Editor] debouncedSave triggered for folder: ${currentFolder}`); handleSave(); }, 1000); // Save after 1 second of inactivity }; const handleSave = async () => { - if (!title || !content) return; + console.log(`[Editor] handleSave called - folder: ${currentFolder}, title: "${title}", content length: ${content?.length || 0}`); + if (!title || !content) { + console.log(`[Editor] handleSave: Skipping save - title: "${title}", content: "${content}"`); + return; + } if (!session) { console.error('No active session, cannot save'); setError('You must be logged in to save notes');