Skip to content

Build your personal knowledge base with Trilium Notes

fix(client): refresh launchers & template switcher on protected session change (closes #9701)

Elian Dorand7a2a9f667e0
diff --git a/apps/client/src/widgets/launch_bar/LauncherContainer.spec.tsx b/apps/client/src/widgets/launch_bar/LauncherContainer.spec.tsx
new file mode 100644
index 0000000000..9e2d6a177a
--- /dev/null
+++ b/apps/client/src/widgets/launch_bar/LauncherContainer.spec.tsx
@@ -0,0 +1,73 @@
+import { render } from "preact";
+import { act } from "preact/test-utils";
+import { afterEach, describe, expect, it } from "vitest";
+
+import Component from "../../components/component.js";
+import type FNote from "../../entities/fnote.js";
+import { buildNote } from "../../test/easy-froca.js";
+import { ParentComponent } from "../react/react_utils.js";
+import { useLauncherChildNotes } from "./LauncherContainer.js";
+
+let container: HTMLDivElement;
+
+afterEach(() => {
+ act(() => render(null, container));
+ container.remove();
+});
+
+function mountHook() {
+ const host = new Component();
+ let childNotes: FNote[] | undefined;
+
+ function Harness() {
+ childNotes = useLauncherChildNotes();
+ return null;
+ }
+
+ container = document.createElement("div");
+ document.body.appendChild(container);
+ act(() => render(<ParentComponent.Provider value={host}><Harness /></ParentComponent.Provider>, container));
+
+ return { host, getChildNotes: () => childNotes };
+}
+
+// The hook resolves the root note, re-renders, then resolves the children — each async step
+// needs its own act() round so the intermediate state commits and the next effect fires.
+async function flush() {
+ for (let i = 0; i < 3; i++) {
+ await act(async () => { await new Promise((resolve) => setTimeout(resolve)); });
+ }
+}
+
+describe("useLauncherChildNotes", () => {
+ it("swaps to fresh FNote refs on frocaReloaded (protected launcher titles decrypt after unlock)", async () => {
+ // Locked protected session: the launcher note's title is the encrypted placeholder.
+ buildNote({
+ id: "_lbVisibleLaunchers",
+ title: "Visible Launchers",
+ children: [ { id: "_lbTestLauncher", title: "[protected]", type: "launcher" } ]
+ });
+
+ const { host, getChildNotes } = mountHook();
+ await flush();
+
+ const staleNote = getChildNotes()?.[0];
+ expect(staleNote?.title).toBe("[protected]");
+
+ // Unlocking rebuilds froca from scratch: same noteIds, brand-new FNote instances,
+ // now with decrypted titles. The old instances stay orphaned in the hook's state
+ // unless frocaReloaded triggers a re-resolve.
+ buildNote({
+ id: "_lbVisibleLaunchers",
+ title: "Visible Launchers",
+ children: [ { id: "_lbTestLauncher", title: "My secret launcher", type: "launcher" } ]
+ });
+
+ await act(async () => { await host.handleEvent("frocaReloaded", {}); });
+ await flush();
+
+ const freshNote = getChildNotes()?.[0];
+ expect(freshNote?.title).toBe("My secret launcher");
+ expect(freshNote).not.toBe(staleNote);
+ });
+});
diff --git a/apps/client/src/widgets/launch_bar/LauncherContainer.tsx b/apps/client/src/widgets/launch_bar/LauncherContainer.tsx
index 4d7007b559..a334d741b5 100644
--- a/apps/client/src/widgets/launch_bar/LauncherContainer.tsx
+++ b/apps/client/src/widgets/launch_bar/LauncherContainer.tsx
@@ -110,15 +110,16 @@ function initBuiltinWidget(note: FNote, isHorizontalLayout: boolean) {
}
}
-function useLauncherChildNotes() {
+export function useLauncherChildNotes() {
const [ visibleLaunchersRoot, setVisibleLaunchersRoot ] = useState<FNote | undefined | null>();
const [ childNotes, setChildNotes ] = useState<FNote[]>();
// Load the root note.
- useLayoutEffect(() => {
+ const loadRoot = useCallback(() => {
const visibleLaunchersRootId = isMobile() ? "_lbMobileVisibleLaunchers" : "_lbVisibleLaunchers";
froca.getNote(visibleLaunchersRootId, true).then(setVisibleLaunchersRoot);
}, []);
+ useLayoutEffect(loadRoot, [ loadRoot ]);
// Load the children.
const refresh = useCallback(() => {
@@ -127,6 +128,12 @@ function useLauncherChildNotes() {
}, [ visibleLaunchersRoot, setChildNotes ]);
useLayoutEffect(refresh, [ visibleLaunchersRoot ]);
+ // Swap to fresh FNote refs after a full froca reload (e.g. entering a protected session
+ // clears the cache and creates new instances — old refs are orphaned with stale titles,
+ // leaving launcher tooltips stuck at "[protected]" when the launchers themselves are
+ // protected notes). Re-resolving the root also refreshes the children via the effect above.
+ useTriliumEvent("frocaReloaded", loadRoot);
+
// React to position changes.
useTriliumEvent("entitiesReloaded", ({loadResults}) => {
if (loadResults.getBranchRows().find((branch) => branch.parentNoteId && froca.getNoteFromCache(branch.parentNoteId)?.isLaunchBarConfig())) {
diff --git a/apps/client/src/widgets/layout/NoteTypeSwitcher.spec.tsx b/apps/client/src/widgets/layout/NoteTypeSwitcher.spec.tsx
new file mode 100644
index 0000000000..75d579aa82
--- /dev/null
+++ b/apps/client/src/widgets/layout/NoteTypeSwitcher.spec.tsx
@@ -0,0 +1,81 @@
+import { render } from "preact";
+import { act } from "preact/test-utils";
+import { afterEach, describe, expect, it } from "vitest";
+
+import Component from "../../components/component.js";
+import type FNote from "../../entities/fnote.js";
+import { buildNote } from "../../test/easy-froca.js";
+import { ParentComponent } from "../react/react_utils.js";
+import { useBuiltinTemplates } from "./NoteTypeSwitcher.js";
+
+let container: HTMLDivElement;
+
+afterEach(() => {
+ act(() => render(null, container));
+ container.remove();
+});
+
+function mountHook() {
+ const host = new Component();
+ let templates: { builtinTemplates: FNote[]; collectionTemplates: FNote[] } | undefined;
+
+ function Harness() {
+ templates = useBuiltinTemplates();
+ return null;
+ }
+
+ container = document.createElement("div");
+ document.body.appendChild(container);
+ act(() => render(<ParentComponent.Provider value={host}><Harness /></ParentComponent.Provider>, container));
+
+ return { host, getTemplates: () => templates };
+}
+
+// The hook resolves the root note, re-renders, then resolves the children — each async step
+// needs its own act() round so the intermediate state commits and the next effect fires.
+async function flush() {
+ for (let i = 0; i < 3; i++) {
+ await act(async () => { await new Promise((resolve) => setTimeout(resolve)); });
+ }
+}
+
+describe("useBuiltinTemplates", () => {
+ it("swaps to fresh FNote refs on frocaReloaded (protected template titles decrypt after unlock)", async () => {
+ // Locked protected session: the built-in template titles are the encrypted placeholder.
+ buildNote({
+ id: "_templates",
+ title: "Templates",
+ children: [
+ { id: "_templateA", title: "[protected]", "#template": "" },
+ { id: "_templateB", title: "[protected]", "#template": "", "#collection": "" }
+ ]
+ });
+
+ const { host, getTemplates } = mountHook();
+ await flush();
+
+ const stale = getTemplates();
+ expect(stale?.builtinTemplates.map((note) => note.title)).toEqual([ "[protected]" ]);
+ expect(stale?.collectionTemplates.map((note) => note.title)).toEqual([ "[protected]" ]);
+
+ // Unlocking rebuilds froca from scratch: same noteIds, brand-new FNote instances,
+ // now with decrypted titles. The old instances stay orphaned in the hook's state
+ // unless frocaReloaded triggers a re-resolve.
+ buildNote({
+ id: "_templates",
+ title: "Templates",
+ children: [
+ { id: "_templateA", title: "Grid View", "#template": "" },
+ { id: "_templateB", title: "Board", "#template": "", "#collection": "" }
+ ]
+ });
+
+ await act(async () => { await host.handleEvent("frocaReloaded", {}); });
+ await flush();
+
+ const fresh = getTemplates();
+ expect(fresh?.builtinTemplates.map((note) => note.title)).toEqual([ "Grid View" ]);
+ expect(fresh?.collectionTemplates.map((note) => note.title)).toEqual([ "Board" ]);
+ expect(fresh?.builtinTemplates[0]).not.toBe(stale?.builtinTemplates[0]);
+ });
+});
diff --git a/apps/client/src/widgets/layout/NoteTypeSwitcher.tsx b/apps/client/src/widgets/layout/NoteTypeSwitcher.tsx
index 1392fef7cf..3f7cbaef3a 100644
--- a/apps/client/src/widgets/layout/NoteTypeSwitcher.tsx
+++ b/apps/client/src/widgets/layout/NoteTypeSwitcher.tsx
@@ -121,6 +121,13 @@ function TemplateNoteTypes({ noteId, builtinTemplates }: { noteId: string, built
}
});
+ // Swap to fresh FNote refs after a full froca reload (e.g. entering a protected session
+ // clears the cache and creates new instances — old refs are orphaned with stale titles,
+ // leaving protected templates stuck at "[protected]" after unlock).
+ useTriliumEvent("frocaReloaded", () => {
+ refreshTemplates();
+ });
+
return (
<BadgeWithDropdown
text={t("note_title.note_type_switcher_templates")}
@@ -150,7 +157,7 @@ function setTemplate(noteId: string, templateId: string) {
return attributes.setRelation(noteId, "template", templateId);
}
-function useBuiltinTemplates() {
+export function useBuiltinTemplates() {
const [ templates, setTemplates ] = useState<{
builtinTemplates: FNote[];
collectionTemplates: FNote[];
@@ -180,5 +187,12 @@ function useBuiltinTemplates() {
loadBuiltinTemplates();
}, []);
+ // Swap to fresh FNote refs after a full froca reload (e.g. entering a protected session
+ // clears the cache and creates new instances — old refs are orphaned with stale titles,
+ // leaving protected templates stuck at "[protected]" after unlock).
+ useTriliumEvent("frocaReloaded", () => {
+ loadBuiltinTemplates();
+ });
+
return templates;
}