Skip to content

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

fix: deleting note delay

Chadpiha4cde24f71cc9
diff --git a/src/components/notes/PersonalNotesView.tsx b/src/components/notes/PersonalNotesView.tsx
index 5b9cebb17..8efbf06ad 100644
--- a/src/components/notes/PersonalNotesView.tsx
+++ b/src/components/notes/PersonalNotesView.tsx
@@ -1,19 +1,6 @@
import { useState, useEffect, useRef, useCallback, useMemo } from "react";
import { useTranslation } from "react-i18next";
import { Plus, SquarePen, Search, Sparkles } from "lucide-react";
-import { Button } from "../ui/button";
-import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from "../ui/dialog";
-import {
- Select,
- SelectTrigger,
- SelectValue,
- SelectContent,
- SelectGroup,
- SelectLabel,
- SelectItem,
- SelectSeparator,
-} from "../ui/select";
-import { Input } from "../ui/input";
import { useToast } from "../ui/useToast";
import NoteEditor from "./NoteEditor";
import SpacesTree from "./SpacesTree";
@@ -30,7 +17,6 @@ import {
selectResolvedNoteFormatting,
} from "../../stores/settingsStore";
import { cn } from "../lib/utils";
-import { findDefaultFolder } from "./shared";
import logger from "../../utils/logger";
import { parseTranscriptSegments } from "../../utils/parseTranscriptSegments";
import { serializeTranscriptSegments } from "../../utils/transcriptSpeakerState";
@@ -110,11 +96,7 @@ export default function PersonalNotesView({
const [localContent, setLocalContent] = useState("");
const [localEnhancedContent, setLocalEnhancedContent] = useState<string | null>(null);
const [showActionManager, setShowActionManager] = useState(false);
- const [showNewNoteDialog, setShowNewNoteDialog] = useState(false);
const [showAddNotesDialog, setShowAddNotesDialog] = useState(false);
- const [newNoteTarget, setNewNoteTarget] = useState<string>("");
- const [isCreatingNewNoteFolder, setIsCreatingNewNoteFolder] = useState(false);
- const [newNoteFolderName, setNewNoteFolderName] = useState("");
const saveTimeoutRef = useRef<NodeJS.Timeout | null>(null);
const enhancedSaveTimeoutRef = useRef<NodeJS.Timeout | null>(null);
const activeNoteRef = useRef<number | null>(null);
@@ -429,76 +411,20 @@ export default function PersonalNotesView({
[t]
);
- const handleNewNote = useCallback(() => {
- if (!activeContext) return;
- handleNewNoteIn(activeContext.spaceId, activeContext.folderId);
- }, [activeContext, handleNewNoteIn]);
-
const privateSpaceId = useMemo(
() => spaces.find((s) => s.kind === "private")?.id ?? null,
[spaces]
);
- const spaceIdForTarget = useCallback(
- (target: string): number | null => {
- if (target.startsWith("f:")) {
- return folders.find((f) => f.id === Number(target.slice(2)))?.space_id ?? null;
- }
- if (target.startsWith("s:")) return Number(target.slice(2));
- return null;
- },
- [folders]
- );
+ const handleNewNoteInPrivate = useCallback(() => {
+ if (privateSpaceId == null) return;
+ handleNewNoteIn(privateSpaceId, null);
+ }, [privateSpaceId, handleNewNoteIn]);
- const handleOpenNewNoteDialog = useCallback(() => {
- let target = "";
- if (activeContext?.folderId != null) {
- target = `f:${activeContext.folderId}`;
- } else if (activeContext && activeContext.spaceId !== privateSpaceId) {
- target = `s:${activeContext.spaceId}`;
- }
- if (!target) {
- const personal = findDefaultFolder(folders) ?? folders[0];
- target = personal ? `f:${personal.id}` : "";
- }
- setNewNoteTarget(target);
- setShowNewNoteDialog(true);
- }, [activeContext, privateSpaceId, folders]);
-
- const handleNewNoteTargetChange = useCallback((val: string) => {
- if (val === "__create_new__") {
- setIsCreatingNewNoteFolder(true);
- return;
- }
- setNewNoteTarget(val);
- }, []);
-
- const handleCreateNewNoteFolder = useCallback(async () => {
- const trimmed = newNoteFolderName.trim();
- if (!trimmed) return;
- const spaceId = spaceIdForTarget(newNoteTarget) ?? privateSpaceId;
- if (spaceId == null) return;
- const res = await createFolder(trimmed, spaceId);
- if (res.success && res.folder) {
- setNewNoteTarget(`f:${res.folder.id}`);
- } else if (res.error) {
- toast({
- title: t("notes.folders.couldNotCreate"),
- description: res.error,
- variant: "destructive",
- });
- }
- setNewNoteFolderName("");
- setIsCreatingNewNoteFolder(false);
- }, [newNoteFolderName, newNoteTarget, spaceIdForTarget, privateSpaceId, toast, t]);
-
- const handleConfirmNewNote = useCallback(async () => {
- const spaceId = spaceIdForTarget(newNoteTarget);
- if (spaceId == null) return;
- const folderId = newNoteTarget.startsWith("f:") ? Number(newNoteTarget.slice(2)) : null;
- await handleNewNoteIn(spaceId, folderId);
- setShowNewNoteDialog(false);
- }, [newNoteTarget, spaceIdForTarget, handleNewNoteIn]);
+ const handleNewNote = useCallback(() => {
+ if (activeContext) handleNewNoteIn(activeContext.spaceId, activeContext.folderId);
+ else handleNewNoteInPrivate();
+ }, [activeContext, handleNewNoteIn, handleNewNoteInPrivate]);
const handleNotesAdded = useCallback(async () => {
if (activeFolderId) {
@@ -674,7 +600,7 @@ export default function PersonalNotesView({
<div className="w-52 shrink-0 border-r border-border/15 dark:border-white/4 flex flex-col h-full">
<div className="px-2 pt-2 pb-1 shrink-0 space-y-0.5">
<button
- onClick={handleOpenNewNoteDialog}
+ onClick={handleNewNoteInPrivate}
className={cn(
"flex items-center gap-2 w-full px-2 py-1.5 rounded-md text-xs",
"text-muted-foreground/80 hover:text-foreground hover:bg-foreground/5",
@@ -985,118 +911,6 @@ export default function PersonalNotesView({
/>
)}
- <Dialog
- open={showNewNoteDialog}
- onOpenChange={(open) => {
- setShowNewNoteDialog(open);
- if (!open) {
- setIsCreatingNewNoteFolder(false);
- setNewNoteFolderName("");
- }
- }}
- >
- <DialogContent className="sm:max-w-95 p-6 gap-5">
- <DialogHeader>
- <DialogTitle>
- {isCreatingNewNoteFolder ? t("notes.upload.newFolder") : t("notes.sidebar.newNote")}
- </DialogTitle>
- </DialogHeader>
-
- {isCreatingNewNoteFolder ? (
- <div className="space-y-1.5">
- <label className="text-xs font-medium text-foreground/50">
- {t("notes.upload.folderName")}
- </label>
- <Input
- value={newNoteFolderName}
- onChange={(e) => setNewNoteFolderName(e.target.value)}
- placeholder={t("notes.folders.folderName")}
- autoFocus
- onKeyDown={(e) => {
- if (e.key === "Enter") handleCreateNewNoteFolder();
- }}
- />
- </div>
- ) : (
- folders.length > 0 && (
- <div className="space-y-1.5">
- <label className="text-xs font-medium text-foreground/50">
- {t("notes.folders.title")}
- </label>
- <Select value={newNoteTarget} onValueChange={handleNewNoteTargetChange}>
- <SelectTrigger>
- <SelectValue placeholder={t("notes.upload.selectFolder")} />
- </SelectTrigger>
- <SelectContent>
- {spaces.map((space) => {
- const spaceFolders = folders.filter((f) => f.space_id === space.id);
- const isTeam = space.kind === "team";
- if (!isTeam && spaceFolders.length === 0) return null;
- return (
- <SelectGroup key={space.id}>
- {spaces.length > 1 && (
- <SelectLabel>
- {isTeam
- ? `${space.emoji ? `${space.emoji} ` : ""}${space.name}`
- : t("notes.spaces.personal")}
- </SelectLabel>
- )}
- {isTeam && (
- <SelectItem value={`s:${space.id}`}>
- {t("notes.spaces.spaceRoot")}
- </SelectItem>
- )}
- {spaceFolders.map((f) => (
- <SelectItem key={f.id} value={`f:${f.id}`}>
- {f.name}
- </SelectItem>
- ))}
- </SelectGroup>
- );
- })}
- <SelectSeparator />
- <SelectItem value="__create_new__">
- <span className="flex items-center gap-1.5 text-primary/60">
- <Plus size={13} />
- {t("notes.upload.newFolder")}
- </span>
- </SelectItem>
- </SelectContent>
- </Select>
- </div>
- )
- )}
-
- <DialogFooter>
- {isCreatingNewNoteFolder ? (
- <>
- <Button
- variant="ghost"
- onClick={() => {
- setIsCreatingNewNoteFolder(false);
- setNewNoteFolderName("");
- }}
- >
- {t("common.back")}
- </Button>
- <Button onClick={handleCreateNewNoteFolder} disabled={!newNoteFolderName.trim()}>
- {t("notes.upload.create")}
- </Button>
- </>
- ) : (
- <>
- <Button variant="ghost" onClick={() => setShowNewNoteDialog(false)}>
- {t("notes.upload.cancel")}
- </Button>
- <Button onClick={handleConfirmNewNote} disabled={!newNoteTarget}>
- {t("notes.upload.create")}
- </Button>
- </>
- )}
- </DialogFooter>
- </DialogContent>
- </Dialog>
-
<NotesStructureIntroDialog
open={showStructureIntro}
onOpenChange={handleStructureIntroOpenChange}
diff --git a/src/stores/noteListOps.ts b/src/stores/noteListOps.ts
new file mode 100644
index 000000000..5ca9a296d
--- /dev/null
+++ b/src/stores/noteListOps.ts
@@ -0,0 +1,47 @@
+import type { NoteItem } from "../types/electron";
+
+export interface NoteListsState {
+ notesByContainer: Record<string, NoteItem[]>;
+ notes: NoteItem[];
+ activeNoteId: number | null;
+}
+
+export interface RemoveNoteResult extends NoteListsState {
+ changed: boolean;
+}
+
+/**
+ * Remove a note from every container list AND the flat `notes` list. The flat
+ * list can hold notes absent from every loaded container (flat-only loads,
+ * divergence after partial refreshes), so it must be filtered independently —
+ * relying on the active-container mirror alone leaves a deleted note visible.
+ */
+export function removeNoteFromLists(state: NoteListsState, id: number): RemoveNoteResult {
+ const notesByContainer = { ...state.notesByContainer };
+ let sourceItems: NoteItem[] | null = null;
+ let sourceKey: string | null = null;
+ let changed = false;
+ for (const [key, items] of Object.entries(state.notesByContainer)) {
+ if (!items.some((n) => n.id === id)) continue;
+ sourceItems = items;
+ sourceKey = key;
+ notesByContainer[key] = items.filter((n) => n.id !== id);
+ changed = true;
+ }
+
+ const inFlat = state.notes.some((n) => n.id === id);
+ const notes = inFlat ? state.notes.filter((n) => n.id !== id) : state.notes;
+ changed = changed || inFlat;
+
+ let activeNoteId = state.activeNoteId;
+ if (changed && state.activeNoteId === id) {
+ // Pick the neighbor from what the user is looking at: the flat list when
+ // it held the note, else the note's source container.
+ const previous = inFlat ? state.notes : sourceItems;
+ const next = inFlat ? notes : sourceKey ? notesByContainer[sourceKey] : [];
+ const idx = previous ? previous.findIndex((n) => n.id === id) : -1;
+ activeNoteId = idx === -1 ? null : (next[Math.min(idx, next.length - 1)]?.id ?? null);
+ }
+
+ return { notesByContainer, notes, activeNoteId, changed };
+}
diff --git a/src/stores/noteStore.ts b/src/stores/noteStore.ts
index ceb6e6e3c..c059f919a 100644
--- a/src/stores/noteStore.ts
+++ b/src/stores/noteStore.ts
@@ -1,5 +1,6 @@
import { create } from "zustand";
import { syncService } from "../services/SyncService.js";
+import { removeNoteFromLists } from "./noteListOps";
import { addNoteConflictId, removeNoteConflictId } from "../lib/noteConflictRegistry";
import { findDefaultFolder } from "../components/notes/shared";
import type { CloudNote } from "../services/NotesService.js";
@@ -513,25 +514,15 @@ export function updateNoteInStore(note: NoteItem): void {
export function removeNote(id: number): void {
if (id == null) return;
const state = useNoteStore.getState();
- const notesByContainer = { ...state.notesByContainer };
- let sourceItems: NoteItem[] | null = null;
- let sourceKey: string | null = null;
- let changed = false;
- for (const [key, items] of Object.entries(state.notesByContainer)) {
- if (!items.some((n) => n.id === id)) continue;
- sourceItems = items;
- sourceKey = key;
- notesByContainer[key] = items.filter((n) => n.id !== id);
- changed = true;
- }
- if (!changed) return;
- const extra: Partial<NoteState> = {};
- if (state.activeNoteId === id && sourceItems && sourceKey) {
- const idx = sourceItems.findIndex((n) => n.id === id);
- const next = notesByContainer[sourceKey];
- extra.activeNoteId = next[Math.min(idx, next.length - 1)]?.id ?? null;
- }
- applyContainers(notesByContainer, extra);
+ const result = removeNoteFromLists(state, id);
+ if (!result.changed) return;
+ // applyContainers only mirrors the active container into `notes`; pass the
+ // filtered flat list explicitly so flat-only notes disappear too.
+ const extra: Partial<NoteState> = { notes: result.notes };
+ if (result.activeNoteId !== state.activeNoteId) {
+ extra.activeNoteId = result.activeNoteId;
+ }
+ applyContainers(result.notesByContainer, extra);
}
function handleSpacePurged(spaceId: number): void {
diff --git a/test/helpers/noteListOps.test.js b/test/helpers/noteListOps.test.js
new file mode 100644
index 000000000..0e72b3519
--- /dev/null
+++ b/test/helpers/noteListOps.test.js
@@ -0,0 +1,103 @@
+const test = require("node:test");
+const assert = require("node:assert/strict");
+
+// Requires Node's native TypeScript type-stripping (Node >= 22.6 with
+// --experimental-strip-types, on by default in Node 23.6+/24). CI runs Node 24.
+
+const load = () => import("../../src/stores/noteListOps.ts");
+
+const note = (id, folderId = 1, spaceId = 1) => ({
+ id,
+ folder_id: folderId,
+ space_id: spaceId,
+});
+
+test("removes the note from every container list that holds it", async () => {
+ const { removeNoteFromLists } = await load();
+ const state = {
+ notesByContainer: {
+ "f:1": [note(1), note(2)],
+ "s:1": [note(2), note(3)],
+ },
+ notes: [note(1), note(2)],
+ activeNoteId: null,
+ };
+
+ const result = removeNoteFromLists(state, 2);
+
+ assert.equal(result.changed, true);
+ assert.deepEqual(
+ result.notesByContainer["f:1"].map((n) => n.id),
+ [1]
+ );
+ assert.deepEqual(
+ result.notesByContainer["s:1"].map((n) => n.id),
+ [3]
+ );
+});
+
+test("filters the flat notes list even when no container holds the note", async () => {
+ // Regression: the container-model rewrite dropped the flat-list filter, so a
+ // note present only in `notes` (e.g. a flat-only initializeNotes load)
+ // survived deletion until an app reload (count updated, row stayed).
+ const { removeNoteFromLists } = await load();
+ const state = {
+ notesByContainer: { "f:9": [note(7, 9)] },
+ notes: [note(1), note(2)],
+ activeNoteId: null,
+ };
+
+ const result = removeNoteFromLists(state, 2);
+
+ assert.equal(result.changed, true);
+ assert.deepEqual(
+ result.notes.map((n) => n.id),
+ [1]
+ );
+ // Untouched containers keep their identity.
+ assert.equal(result.notesByContainer["f:9"], state.notesByContainer["f:9"]);
+});
+
+test("reassigns activeNoteId to the visible neighbor when the deleted note was active", async () => {
+ const { removeNoteFromLists } = await load();
+ const state = {
+ notesByContainer: { "f:1": [note(1), note(2), note(3)] },
+ notes: [note(1), note(2), note(3)],
+ activeNoteId: 2,
+ };
+
+ const result = removeNoteFromLists(state, 2);
+
+ assert.equal(result.activeNoteId, 3);
+});
+
+test("reassigns activeNoteId from the flat list when the note is in no container", async () => {
+ const { removeNoteFromLists } = await load();
+ const state = {
+ notesByContainer: {},
+ notes: [note(1), note(2), note(3)],
+ activeNoteId: 3,
+ };
+
+ const result = removeNoteFromLists(state, 3);
+
+ assert.equal(result.activeNoteId, 2);
+ assert.deepEqual(
+ result.notes.map((n) => n.id),
+ [1, 2]
+ );
+});
+
+test("no-ops when the note is nowhere", async () => {
+ const { removeNoteFromLists } = await load();
+ const state = {
+ notesByContainer: { "f:1": [note(1)] },
+ notes: [note(1)],
+ activeNoteId: 1,
+ };
+
+ const result = removeNoteFromLists(state, 99);
+
+ assert.equal(result.changed, false);
+ assert.equal(result.activeNoteId, 1);
+});