Skip to content

Build your personal knowledge base with Trilium Notes

fix(client): unpin and empty tabs whose note a froca rebuild dropped

Elian Doran0b2b4d590d05
diff --git a/apps/client/src/components/note_context.ts b/apps/client/src/components/note_context.ts
index e9859d45ac..3f0d336841 100644
--- a/apps/client/src/components/note_context.ts
+++ b/apps/client/src/components/note_context.ts
@@ -466,17 +466,27 @@ class NoteContext extends Component implements EventListener<"entitiesReloaded">
const noteRow = loadResults.getEntityRow("notes", this.noteId);
if (noteRow.isDeleted) {
- this.noteId = null;
- this.notePath = null;
-
- this.triggerEvent("noteSwitched", {
- noteContext: this,
- notePath: this.notePath
- });
+ this.clearRemovedNote();
}
}
}
+ frocaReloadedEvent() {
+ if (this.noteId && !(this.noteId in froca.notes)) {
+ this.clearRemovedNote();
+ }
+ }
+
+ private clearRemovedNote() {
+ this.noteId = null;
+ this.notePath = null;
+
+ this.triggerEvent("noteSwitched", {
+ noteContext: this,
+ notePath: this.notePath
+ });
+ }
+
hasNoteList() {
const note = this.note;
diff --git a/apps/client/src/components/tab_manager.spec.ts b/apps/client/src/components/tab_manager.spec.ts
index de8d3404e9..f8f5fb0626 100644
--- a/apps/client/src/components/tab_manager.spec.ts
+++ b/apps/client/src/components/tab_manager.spec.ts
@@ -1,5 +1,6 @@
import { describe, expect, it, vi } from "vitest";
+import { buildNote } from "../test/easy-froca.js";
import appContext from "./app_context.js";
import NoteContext from "./note_context.js";
import TabManager, { buildNoteContextStatesFromUrl } from "./tab_manager.js";
@@ -157,6 +158,28 @@ describe("handing a tab over to a window of its own", () => {
});
});
+describe("TabManager after froca is rebuilt", () => {
+ it("unpins and empties a tab whose note the rebuild no longer holds", async () => {
+ vi.spyOn(appContext, "triggerEvent").mockResolvedValue(undefined as any);
+ const tm = new TabManager();
+ appContext.tabManager = tm;
+ const kept = buildNote({ title: "Kept" });
+ const gone = await tm.openEmptyTab(null, "root", null, true);
+ const stays = await tm.openEmptyTab(null, "root", null, true);
+ gone.noteId = "goneAfterRebuild";
+ gone.notePath = "root/goneAfterRebuild";
+ stays.noteId = kept.noteId;
+ stays.notePath = `root/${kept.noteId}`;
+
+ await tm.handleEvent("frocaReloaded", {});
+
+ expect(gone.pinned).toBe(false);
+ expect(gone.noteId).toBeNull();
+ expect(stays.pinned).toBe(true);
+ expect(stays.noteId).toBe(kept.noteId);
+ });
+});
+
describe("buildNoteContextStatesFromUrl", () => {
it("rebuilds the panes as one tab, with the focused one active", () => {
const states = buildNoteContextStatesFromUrl({
diff --git a/apps/client/src/components/tab_manager.ts b/apps/client/src/components/tab_manager.ts
index e3bacd622d..6860be6a50 100644
--- a/apps/client/src/components/tab_manager.ts
+++ b/apps/client/src/components/tab_manager.ts
@@ -979,6 +979,16 @@ export default class TabManager extends Component {
}
async frocaReloadedEvent() {
+ // A rebuild drops the notes the backend no longer has. Collected before the per-context
+ // handlers clear noteId, as in entitiesReloadedEvent().
+ const removedPinnedNtxIds = this.mainNoteContexts
+ .filter((nc) => nc.pinned && nc.noteId && !(nc.noteId in froca.notes))
+ .map((nc) => nc.ntxId);
+
+ for (const ntxId of removedPinnedNtxIds) {
+ await this.setTabPinned(ntxId, false);
+ }
+
const activeContext = this.getActiveContext();
if (activeContext) {
await this.updateDocumentTitle(activeContext);