Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix app crashing when deleting notes with children #21

Merged
merged 1 commit into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/renderer/utils/deleteNoteIfConfirmed.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { keyBy } from "lodash";
import { flatten, getNoteById } from "../../shared/domain/note";
import { filterOutStaleNoteIds } from "../../shared/ui/app";
import { StoreContext } from "../store";
Expand All @@ -14,7 +15,10 @@ export async function deleteNoteIfConfirmed(
if (confirmed) {
await window.ipc("notes.moveToTrash", note.id);

const otherNotes = flatten(notes).filter(n => n.id !== note.id);
// Flatten so we can remove children from state as well.
const deletedNotes = keyBy(flatten([note]), "id");

const otherNotes = flatten(notes).filter(n => deletedNotes[n.id] == null);
ctx.setUI(ui => filterOutStaleNoteIds(ui, otherNotes, false));

ctx.setNotes(notes => {
Expand Down
1 change: 0 additions & 1 deletion src/shared/ui/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export interface Editor {
export interface EditorTab {
note: Note;
lastActive?: Date;
fromPreviousSession?: boolean;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some cleanup, not related to this PR.

}

// If a note was deleted but was referenced elsewhere in the ui state we need to
Expand Down
11 changes: 10 additions & 1 deletion test/renderer/components/Sidebar.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -608,10 +608,15 @@ test("sidebar.deleteNote", async () => {
const noteAId = uuid();
const noteBId = uuid();
const noteCId = uuid();
const noteDId = uuid();

const store = createStore({
notes: [
createNote({ id: noteAId, name: "A" }),
createNote({
id: noteAId,
name: "A",
children: [createNote({ id: noteDId, name: "D" })],
}),
createNote({ id: noteBId, name: "B" }),
createNote({ id: noteCId, name: "C" }),
],
Expand Down Expand Up @@ -644,6 +649,10 @@ test("sidebar.deleteNote", async () => {
const { notes } = store.current.state;
expect(notes).toContainEqual(expect.objectContaining({ id: noteBId }));
expect(notes).toContainEqual(expect.objectContaining({ id: noteCId }));

// Note D was a child of deleted note A
expect(notes).not.toContainEqual(expect.objectContaining({ id: noteAId }));
expect(notes).not.toContainEqual(expect.objectContaining({ id: noteDId }));
});

test("sidebar.deleteSelectedNote", async () => {
Expand Down