Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

web: don't close tab on trash if note is in tab history (#7933)

01zulfi251cc08a7d8b
diff --git a/apps/web/__e2e__/tabs.test.ts b/apps/web/__e2e__/tabs.test.ts
index 29325abc4..00e396535 100644
--- a/apps/web/__e2e__/tabs.test.ts
+++ b/apps/web/__e2e__/tabs.test.ts
@@ -428,5 +428,78 @@ test("activating pinned tab after page reload should not open a duplicate tab",
await expect(tabs[0].locator).toHaveClass(/active/);
});
+test("if note is active in a tab, moving the note to trash should close the tab", async ({
+ page
+}) => {
+ const app = new AppModel(page);
+ await app.goto();
+ const notes = await app.goToNotes();
+ const note = await notes.createNote({
+ title: "Note 1"
+ });
+
+ await note?.contextMenu.moveToTrash();
+
+ const tabs = await notes.editor.getTabs();
+ expect(tabs.length).toBe(1);
+ await expect(tabs[0].titleElement).toHaveText("Untitled");
+});
+
+test("if note is active in multiple tabs, moving the note to trash should close those tabs", async ({
+ page
+}) => {
+ const app = new AppModel(page);
+ await app.goto();
+ const notes = await app.goToNotes();
+ const note = await notes.createNote({
+ title: "Note 1"
+ });
+ await note?.contextMenu.openInNewTab();
+
+ await note?.contextMenu.moveToTrash();
+
+ const tabs = await notes.editor.getTabs();
+ expect(tabs.length).toBe(1);
+ await expect(tabs[0].titleElement).toHaveText("Untitled");
+});
+
+test("if note is present in tab history, moving the note to trash shouldn't close the tab", async ({
+ page
+}) => {
+ const app = new AppModel(page);
+ await app.goto();
+ const notes = await app.goToNotes();
+ const note = await notes.createNote({
+ title: "Note 1"
+ });
+ await notes.createNote({ title: "Note 2" });
+
+ await note?.contextMenu.moveToTrash();
+
+ const tabs = await notes.editor.getTabs();
+ expect(tabs.length).toBe(1);
+ await expect(tabs[0].titleElement).toHaveText("Note 2");
+});
+
+test("if note is active in a tab and present in other tab's history, moving the note to trash should only close the tab where note is active", async ({
+ page
+}) => {
+ const app = new AppModel(page);
+ await app.goto();
+ const notes = await app.goToNotes();
+ const note = await notes.createNote({
+ title: "Note 1"
+ });
+ await note?.contextMenu.openInNewTab();
+ await notes.editor.waitForLoading();
+ await notes.createNote({ title: "Note 2" });
+
+ await note?.contextMenu.moveToTrash();
+
+ const tabs = await notes.editor.getTabs();
+ expect(tabs.length).toBe(1);
+ await expect(tabs[1].titleElement).toHaveText("Note 2");
+});
+
test.skip("TODO: open a locked note, switch to another note and navigate back", () => {});
test.skip("TODO: open a locked note, switch to another note, unlock the note and navigate back", () => {});
diff --git a/apps/web/src/stores/editor-store.ts b/apps/web/src/stores/editor-store.ts
index 1561ee78e..1db2f2b51 100644
--- a/apps/web/src/stores/editor-store.ts
+++ b/apps/web/src/stores/editor-store.ts
@@ -333,7 +333,8 @@ class EditorStore extends BaseStore<EditorStore> {
db.eventManager.subscribe(
EVENTS.databaseUpdated,
async (event: DatabaseUpdatedEvent) => {
- const { sessions, openSession, closeTabs, updateSession } = this.get();
+ const { sessions, openSession, closeTabs, updateSession, tabs } =
+ this.get();
const clearIds: string[] = [];
if (event.collection === "notes") {
// when a note is permanently deleted from trash
@@ -342,7 +343,9 @@ class EditorStore extends BaseStore<EditorStore> {
...sessions
.filter(
(session) =>
- "note" in session && event.ids.includes(session.note.id)
+ "note" in session &&
+ event.ids.includes(session.note.id) &&
+ tabs.some((tab) => tab.sessionId === session.id)
)
.map((s) => s.tabId)
);
@@ -367,7 +370,11 @@ class EditorStore extends BaseStore<EditorStore> {
session.type !== "deleted" &&
event.item.type === "trash"
) {
- clearIds.push(session.tabId);
+ for (const tab of tabs) {
+ if (tab.sessionId === session.id) {
+ clearIds.push(session.tabId);
+ }
+ }
} else {
updateSession(session.id, [session.type], (session) => {
session.note.pinned =