Skip to content

Build your personal knowledge base with Trilium Notes

refactor(client): build both note type submenus from one fetch

Elian Doran67847224c22b
diff --git a/apps/client/src/menus/tree_context_menu.ts b/apps/client/src/menus/tree_context_menu.ts
index c1a4914fcd..b64c13899e 100644
--- a/apps/client/src/menus/tree_context_menu.ts
+++ b/apps/client/src/menus/tree_context_menu.ts
@@ -127,11 +127,16 @@ export async function buildTreeContextMenuItems(ctx: TreeContextMenuContext): Pr
const parentNotSearch = !parentNote || parentNote.type !== "search";
const insertNoteAfterEnabled = isNotRoot && !isHoisted && parentNotSearch;
- // Both submenus hit the server, so they are resolved together rather than one after the other.
- const [ insertNoteAfterItems, insertChildNoteItems ] = await Promise.all([
- insertNoteAfterEnabled ? noteTypesService.getNoteTypeItems("insertNoteAfter") : null,
- notSearch ? noteTypesService.getNoteTypeItems("insertChildNote") : null
- ]);
+ // Both submenus are built from the same templates, so they are fetched once and shared.
+ const noteTypeData = insertNoteAfterEnabled || notSearch
+ ? await noteTypesService.loadNoteTypeData()
+ : null;
+ const insertNoteAfterItems = noteTypeData && insertNoteAfterEnabled
+ ? noteTypesService.buildNoteTypeItems(noteTypeData, "insertNoteAfter")
+ : null;
+ const insertChildNoteItems = noteTypeData && notSearch
+ ? noteTypesService.buildNoteTypeItems(noteTypeData, "insertChildNote")
+ : null;
const items: (MenuItem<TreeCommandNames> | null)[] = [
{ title: t("tree-context-menu.open-in-a-new-tab"), command: "openInTab", shortcut: "Ctrl+Click", uiIcon: "bx bx-link-external", enabled: noSelectedNotes },
diff --git a/apps/client/src/services/note_types.spec.ts b/apps/client/src/services/note_types.spec.ts
index 05bb45f045..d971fdd929 100644
--- a/apps/client/src/services/note_types.spec.ts
+++ b/apps/client/src/services/note_types.spec.ts
@@ -308,4 +308,25 @@ describe("new template badges", () => {
restore();
}
});
+
+ it("builds several menus from one fetch", async () => {
+ // The tree context menu has two note type submenus. Building them from shared data
+ // must not fetch the templates twice.
+ const get = withTemplates();
+ const restore = withTemplatesRoot([fakeTemplate("tpl-plain", ["template"], "Plain")]);
+ try {
+ const data = await noteTypesService.loadNoteTypeData();
+ const first = noteTypesService.buildNoteTypeItems(data, "insertNoteAfter" as never);
+ const second = noteTypesService.buildNoteTypeItems(data, "insertChildNote" as never);
+
+ expect(get.mock.calls.map((c) => c[0])).toEqual(["search-templates"]);
+ // Same items, each bound to its own command.
+ const ids = (items: any[]) => items.map((i) => i.templateNoteId);
+ expect(ids(first)).toEqual(ids(second));
+ expect(first.every((i: any) => !i.type || i.command === "insertNoteAfter")).toBe(true);
+ expect(second.every((i: any) => !i.type || i.command === "insertChildNote")).toBe(true);
+ } finally {
+ restore();
+ }
+ });
});
diff --git a/apps/client/src/services/note_types.ts b/apps/client/src/services/note_types.ts
index 721a766ee9..24741f71ae 100644
--- a/apps/client/src/services/note_types.ts
+++ b/apps/client/src/services/note_types.ts
@@ -75,27 +75,52 @@ const BETA_BADGE = {
const SEPARATOR: MenuSeparatorItem = { kind: "separator" };
-async function getNoteTypeItems(command?: TreeCommandNames) {
+/**
+ * The templates the note type menus are built from. Kept separate from the menu items so that a
+ * caller building several menus at once (e.g. the tree context menu, which has both an "insert
+ * note after" and an "insert child note" submenu) pays for the requests only once.
+ */
+export interface NoteTypeData {
+ builtInTemplateNotes: FNote[];
+ userTemplateNotes: FNote[];
+ /** The IDs of the templates to mark with the "New" badge. */
+ newTemplates: Set<string>;
+}
+
+async function loadNoteTypeData(): Promise<NoteTypeData> {
// A single request reports both the user templates and which templates are new, so that the
- // menu can be assembled without a round trip per template.
- const { templateNoteIds, newTemplateNoteIds } = await server.get<TemplatesResponse>("search-templates");
- const newTemplates = new Set(newTemplateNoteIds);
+ // menus can be assembled without a round trip per template.
+ const { templateNoteIds, newTemplateNoteIds } =
+ await server.get<TemplatesResponse>("search-templates");
const [ builtInTemplateNotes, userTemplateNotes ] = await Promise.all([
getBuiltInTemplateNotes(),
froca.getNotes(templateNoteIds)
]);
+ return { builtInTemplateNotes, userTemplateNotes, newTemplates: new Set(newTemplateNoteIds) };
+}
+
+function buildNoteTypeItems(data: NoteTypeData, command?: TreeCommandNames) {
+ const { builtInTemplateNotes, userTemplateNotes, newTemplates } = data;
+
const items: MenuItem<TreeCommandNames>[] = [
...getBlankNoteTypes(command),
...getBuiltInTemplates(null, command, builtInTemplateNotes, false, newTemplates),
- ...getBuiltInTemplates(t("note_types.collections"), command, builtInTemplateNotes, true, newTemplates),
+ ...getBuiltInTemplates(
+ t("note_types.collections"), command, builtInTemplateNotes, true, newTemplates
+ ),
...getUserTemplates(command, userTemplateNotes, newTemplates)
];
return items;
}
+/** Builds a single note type menu. Use {@link loadNoteTypeData} directly to build several. */
+async function getNoteTypeItems(command?: TreeCommandNames) {
+ return buildNoteTypeItems(await loadNoteTypeData(), command);
+}
+
function getBlankNoteTypes(command?: TreeCommandNames): MenuItem<TreeCommandNames>[] {
return NOTE_TYPES
.filter((nt) => !nt.reserved && nt.type !== "book")
@@ -208,5 +233,7 @@ function getBuiltInTemplates(title: string | null, command: TreeCommandNames | u
}
export default {
+ loadNoteTypeData,
+ buildNoteTypeItems,
getNoteTypeItems
};