From c147890138833906882ac1e1bddd0b8975980b9e Mon Sep 17 00:00:00 2001 From: drelich Date: Wed, 18 Mar 2026 17:42:39 +0100 Subject: [PATCH] Fix preview mode stale content bug when switching notes (v0.1.1) - Fixed race condition where image processing ran before localContent updated - Added synchronization guard to prevent processing stale content - Added comprehensive logging for debugging note switches - Bumped version to 0.1.1 --- package.json | 2 +- src-tauri/tauri.conf.json | 2 +- src/api/nextcloud.ts | 2 +- src/components/NoteEditor.tsx | 17 ++++++++++++++++- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 67160fb..8760d76 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "nextcloud-notes-tauri", "private": true, - "version": "0.1.0", + "version": "0.1.1", "type": "module", "scripts": { "dev": "vite", diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 3c7d1d9..b31cc83 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -1,7 +1,7 @@ { "$schema": "https://schema.tauri.app/config/2", "productName": "Nextcloud Notes", - "version": "0.1.0", + "version": "0.1.1", "identifier": "com.davidrelich.nextcloud-notes", "build": { "beforeDevCommand": "npm run dev", diff --git a/src/api/nextcloud.ts b/src/api/nextcloud.ts index d60cf1a..8243b4b 100644 --- a/src/api/nextcloud.ts +++ b/src/api/nextcloud.ts @@ -77,7 +77,7 @@ export class NextcloudAPI { webdavPath += `/${path}`; const url = `${this.serverURL}${webdavPath}`; - console.log('Fetching attachment via WebDAV:', url); + console.log(`[Note ${_noteId}] Fetching attachment via WebDAV:`, url); const response = await tauriFetch(url, { headers: { diff --git a/src/components/NoteEditor.tsx b/src/components/NoteEditor.tsx index eae5e3c..784fde2 100644 --- a/src/components/NoteEditor.tsx +++ b/src/components/NoteEditor.tsx @@ -77,13 +77,23 @@ export function NoteEditor({ note, onUpdateNote, onUnsavedChanges, categories, i return; } + // Guard: Only process if localContent has been updated for the current note + // This prevents processing stale content from the previous note + if (previousNoteIdRef.current !== note.id) { + console.log(`[Note ${note.id}] Skipping image processing - waiting for content to sync (previousNoteIdRef: ${previousNoteIdRef.current})`); + return; + } + const processImages = async () => { + console.log(`[Note ${note.id}] Processing images in preview mode. Content length: ${localContent.length}`); setIsLoadingImages(true); + setProcessedContent(''); // Clear old content immediately // Find all image references in markdown: ![alt](path) const imageRegex = /!\[([^\]]*)\]\(([^)]+)\)/g; let content = localContent; const matches = [...localContent.matchAll(imageRegex)]; + console.log(`[Note ${note.id}] Found ${matches.length} images to process`); for (const match of matches) { const [fullMatch, alt, imagePath] = match; @@ -121,11 +131,13 @@ export function NoteEditor({ note, onUpdateNote, onUnsavedChanges, categories, i useEffect(() => { const loadNewNote = () => { if (note) { + console.log(`[Note ${note.id}] Loading note. Title: "${note.title}", Content length: ${note.content.length}`); setLocalTitle(note.title); setLocalContent(note.content); setLocalCategory(note.category || ''); setLocalFavorite(note.favorite); setHasUnsavedChanges(false); + setProcessedContent(''); // Clear preview content immediately const firstLine = note.content.split('\n')[0].replace(/^#+\s*/, '').trim(); const titleMatchesFirstLine = note.title === firstLine || note.title === firstLine.substring(0, 50); @@ -136,10 +148,13 @@ export function NoteEditor({ note, onUpdateNote, onUnsavedChanges, categories, i }; if (previousNoteIdRef.current !== null && previousNoteIdRef.current !== note?.id) { + console.log(`Switching from note ${previousNoteIdRef.current} to note ${note?.id}`); + // Clear preview content immediately when switching notes + setProcessedContent(''); if (hasUnsavedChanges) { handleSave(); } - setTimeout(loadNewNote, 100); + loadNewNote(); } else { loadNewNote(); }