From 441d0f31a4d69bb5054fda770ead21a7f2a84be0 Mon Sep 17 00:00:00 2001 From: alma Date: Fri, 16 Jan 2026 15:53:54 +0100 Subject: [PATCH] Pages corrections pages missions --- components/carnet/editor.tsx | 70 +++++++++--------------------------- 1 file changed, 17 insertions(+), 53 deletions(-) diff --git a/components/carnet/editor.tsx b/components/carnet/editor.tsx index 3a5e6e4..c848753 100644 --- a/components/carnet/editor.tsx +++ b/components/carnet/editor.tsx @@ -220,61 +220,25 @@ export const Editor: React.FC = ({ note, onSave, currentFolder = 'N setIsSaving(true); setError(null); + + // Instead of saving directly, construct a Note object and pass it to onSave + // This ensures handleSaveNote in page.tsx handles all the logic consistently try { - // Construct the full note ID if it doesn't exist yet - const noteId = note?.id || `user-${session.user.id}/${currentFolder.toLowerCase()}/${title}${title.endsWith('.md') ? '' : '.md'}`; + // Construct a Note object with current state + const noteToSave: Note = { + id: note?.id || '', // Use existing note.id if available, otherwise empty (will be determined in handleSaveNote) + title: title, // Use the title from local state + content: content, // Use the content from local state + lastModified: note?.lastModified || new Date().toISOString(), + type: note?.type || 'file', + mime: note?.mime || 'text/markdown', + etag: note?.etag || '' + }; - const endpoint = '/api/storage/files'; - const method = note?.id ? 'PUT' : 'POST'; - - console.log('Saving note:', { - id: noteId, - title, - folder: currentFolder, - contentLength: content.length - }); - - const response = await fetch(endpoint, { - method, - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - id: noteId, - title, - content, - folder: currentFolder.toLowerCase() - }), - }); - - if (response.status === 401) { - console.error('Authentication error, redirecting to login'); - router.push('/signin'); - return; - } - - if (!response.ok) { - const errorData = await response.text(); - console.error('Failed to save note:', { - status: response.status, - statusText: response.statusText, - data: errorData - }); - setError(`Failed to save note: ${response.statusText}`); - throw new Error(`Failed to save note: ${response.status} ${response.statusText}`); - } - - const savedNote = await response.json(); - console.log('Note saved successfully:', savedNote); - - // Update content cache after successful save - noteContentCache.set(noteId, content); - - setError(null); - onSave?.({ - ...savedNote, - content - }); + console.log('[Editor] Calling onSave with note (delegating to handleSaveNote):', noteToSave); + // Pass the note to onSave callback, which will call handleSaveNote in page.tsx + // handleSaveNote will handle the actual API call with proper duplicate detection + onSave?.(noteToSave); // Note: onRefresh is called, but handleSaveNote already calls fetchNotes // So we don't need to call onRefresh here to avoid double fetch // onRefresh?.();