diff --git a/components/carnet/editor.tsx b/components/carnet/editor.tsx index c848753..47fac7d 100644 --- a/components/carnet/editor.tsx +++ b/components/carnet/editor.tsx @@ -180,23 +180,30 @@ export const Editor: React.FC = ({ note, onSave, currentFolder = 'N // Handle content change from HealthForm (direct string update) const handleHealthContentChange = (newContent: string) => { setContent(newContent); - debouncedSave(); + // For Health folder, pass the newContent directly to debouncedSave to ensure we use the latest content + // This is important because setContent is async and content state might not be updated when debouncedSave fires + debouncedSave(newContent); }; - const debouncedSave = () => { + const debouncedSave = (contentToSave?: string) => { if (saveTimeout.current) { clearTimeout(saveTimeout.current); } saveTimeout.current = setTimeout(() => { console.log(`[Editor] debouncedSave triggered for folder: ${currentFolder}`); - handleSave(); + // Use the provided contentToSave if available, otherwise use state content + handleSave(contentToSave); }, 1000); // Save after 1 second of inactivity }; - const handleSave = async () => { - 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}"`); + const handleSave = async (contentToSave?: string) => { + // Use the provided contentToSave if available, otherwise use state content + // This ensures we use the latest content from HealthForm even if state hasn't updated yet + const contentToUse = contentToSave !== undefined ? contentToSave : content; + + console.log(`[Editor] handleSave called - folder: ${currentFolder}, title: "${title}", content length: ${contentToUse?.length || 0}`); + if (!title || !contentToUse) { + console.log(`[Editor] handleSave: Skipping save - title: "${title}", content: "${contentToUse}"`); return; } if (!session) { @@ -208,9 +215,10 @@ export const Editor: React.FC = ({ note, onSave, currentFolder = 'N // For Health folder, don't save if content is just empty JSON if (currentFolder === 'Health') { try { - const parsed = JSON.parse(content); + const parsed = JSON.parse(contentToUse); const isEmpty = Object.keys(parsed).length === 0 || (Object.keys(parsed).length === 1 && parsed.date); if (isEmpty) { + console.log('[Editor] handleSave: Skipping save for empty Health form'); return; // Don't save empty health forms } } catch { @@ -228,14 +236,14 @@ export const Editor: React.FC = ({ note, onSave, currentFolder = 'N 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 + content: contentToUse, // Use the provided content or state content lastModified: note?.lastModified || new Date().toISOString(), type: note?.type || 'file', mime: note?.mime || 'text/markdown', etag: note?.etag || '' }; - console.log('[Editor] Calling onSave with note (delegating to handleSaveNote):', noteToSave); + console.log('[Editor] Calling onSave with note (delegating to handleSaveNote):', { ...noteToSave, content: `[${contentToUse.length} chars]` }); // 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);