Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

fix(notes): resolve race conditions when switching folders quickly

Gabriel Steincca3aa9be43d
diff --git a/src/components/notes/PersonalNotesView.tsx b/src/components/notes/PersonalNotesView.tsx
index ffaf7e83f..eacab9d06 100644
--- a/src/components/notes/PersonalNotesView.tsx
+++ b/src/components/notes/PersonalNotesView.tsx
@@ -170,41 +170,43 @@ export default function PersonalNotesView({
}, [stopTranscription]);
useEffect(() => {
- if (activeNote && activeNote.id !== activeNoteRef.current) {
- // Flush any pending save for the previous note before switching
- if (saveTimeoutRef.current) {
- clearTimeout(saveTimeoutRef.current);
- saveTimeoutRef.current = null;
- if (activeNoteRef.current) {
- window.electronAPI.updateNote(activeNoteRef.current, {
- title: localTitleRef.current,
- content: localContentRef.current,
- });
+ const syncNote = async () => {
+ if (activeNote && activeNote.id !== activeNoteRef.current) {
+ if (saveTimeoutRef.current) {
+ clearTimeout(saveTimeoutRef.current);
+ saveTimeoutRef.current = null;
+ if (activeNoteRef.current) {
+ await window.electronAPI.updateNote(activeNoteRef.current, {
+ title: localTitleRef.current,
+ content: localContentRef.current,
+ });
+ }
}
+ if (enhancedSaveTimeoutRef.current) {
+ clearTimeout(enhancedSaveTimeoutRef.current);
+ enhancedSaveTimeoutRef.current = null;
+ }
+ activeNoteRef.current = activeNote.id;
+ setLocalTitle(activeNote.title);
+ setLocalContent(activeNote.content);
+ setLocalEnhancedContent(activeNote.enhanced_content ?? null);
}
- if (enhancedSaveTimeoutRef.current) {
- clearTimeout(enhancedSaveTimeoutRef.current);
- enhancedSaveTimeoutRef.current = null;
- }
- activeNoteRef.current = activeNote.id;
- setLocalTitle(activeNote.title);
- setLocalContent(activeNote.content);
- setLocalEnhancedContent(activeNote.enhanced_content ?? null);
- }
- if (!activeNote) {
- if (saveTimeoutRef.current) {
- clearTimeout(saveTimeoutRef.current);
- saveTimeoutRef.current = null;
- }
- if (enhancedSaveTimeoutRef.current) {
- clearTimeout(enhancedSaveTimeoutRef.current);
- enhancedSaveTimeoutRef.current = null;
+ if (!activeNote) {
+ if (saveTimeoutRef.current) {
+ clearTimeout(saveTimeoutRef.current);
+ saveTimeoutRef.current = null;
+ }
+ if (enhancedSaveTimeoutRef.current) {
+ clearTimeout(enhancedSaveTimeoutRef.current);
+ enhancedSaveTimeoutRef.current = null;
+ }
+ activeNoteRef.current = null;
+ setLocalTitle("");
+ setLocalContent("");
+ setLocalEnhancedContent(null);
}
- activeNoteRef.current = null;
- setLocalTitle("");
- setLocalContent("");
- setLocalEnhancedContent(null);
- }
+ };
+ syncNote();
}, [activeNote]);
const debouncedSave = useCallback((noteId: number, title: string, content: string) => {
@@ -483,8 +485,13 @@ export default function PersonalNotesView({
prevTranscribingRef.current = isTranscribing;
}, [isTranscribing, realtimeTranscript, realtimeSegments]);
+ const isLocalSynced = activeNoteRef.current === activeNote?.id;
const editorNote = activeNote
- ? { ...activeNote, title: localTitle, content: localContent }
+ ? {
+ ...activeNote,
+ title: isLocalSynced ? localTitle : activeNote.title,
+ content: isLocalSynced ? localContent : activeNote.content,
+ }
: null;
if (!isOnboardingComplete) {
diff --git a/src/hooks/useFolderManagement.ts b/src/hooks/useFolderManagement.ts
index b9bdf2a3f..fa3fe6858 100644
--- a/src/hooks/useFolderManagement.ts
+++ b/src/hooks/useFolderManagement.ts
@@ -116,14 +116,23 @@ export function useFolderManagement(): UseFolderManagementReturn {
// Re-initialize notes when active folder changes
useEffect(() => {
if (!activeFolderId || isLoading) return;
- // Skip if folder hasn't changed (e.g., initial load completing)
if (prevFolderIdRef.current === activeFolderId) return;
prevFolderIdRef.current = activeFolderId;
+ setActiveNoteId(null);
const loadForFolder = async () => {
- const notes = await initializeNotes(null, 50, activeFolderId);
- const presetNoteId = getActiveNoteIdValue();
- if (!presetNoteId || !notes.some((n) => n.id === presetNoteId)) {
- setActiveNoteId(notes.length > 0 ? notes[0].id : null);
+ try {
+ const notes = await initializeNotes(null, 50, activeFolderId);
+ if (getActiveFolderIdValue() !== activeFolderId) return;
+ const presetNoteId = getActiveNoteIdValue();
+ if (!presetNoteId || !notes.some((n) => n.id === presetNoteId)) {
+ setActiveNoteId(notes.length > 0 ? notes[0].id : null);
+ }
+ } catch (err) {
+ logger.warn(
+ "Failed to load notes for folder",
+ { folderId: activeFolderId, error: (err as Error).message },
+ "notes"
+ );
}
};
loadForFolder();
@@ -192,11 +201,11 @@ export function useFolderManagement(): UseFolderManagementReturn {
async (folderId: number) => {
const result = await window.electronAPI.deleteFolder(folderId);
if (result.success) {
- const personalFolder = findDefaultFolder(folders);
- if (activeFolderId === folderId && personalFolder) {
- setActiveFolderId(personalFolder.id);
+ const items = await loadFolders();
+ if (getActiveFolderIdValue() === folderId) {
+ const personalFolder = findDefaultFolder(items);
+ if (personalFolder) setActiveFolderId(personalFolder.id);
}
- await loadFolders();
} else if (result.error) {
toast({
title: t("notes.folders.couldNotDelete"),
@@ -205,7 +214,7 @@ export function useFolderManagement(): UseFolderManagementReturn {
});
}
},
- [folders, activeFolderId, loadFolders, toast, t]
+ [loadFolders, toast, t]
);
return {
diff --git a/src/stores/noteStore.ts b/src/stores/noteStore.ts
index 9c387044d..6e5a2ff6b 100644
--- a/src/stores/noteStore.ts
+++ b/src/stores/noteStore.ts
@@ -18,6 +18,7 @@ const useNoteStore = create<NoteState>()(() => ({
let hasBoundIpcListeners = false;
const DEFAULT_LIMIT = 50;
let currentLimit = DEFAULT_LIMIT;
+let loadGeneration = 0;
function ensureIpcListeners() {
if (hasBoundIpcListeners || typeof window === "undefined") {
@@ -69,9 +70,11 @@ export async function initializeNotes(
limit = DEFAULT_LIMIT,
folderId?: number | null
): Promise<NoteItem[]> {
+ const gen = ++loadGeneration;
currentLimit = limit;
ensureIpcListeners();
const items = (await window.electronAPI?.getNotes(noteType, limit, folderId)) ?? [];
+ if (gen !== loadGeneration) return items;
useNoteStore.setState({ notes: items });
return items;
}