Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/content manager: fix some issues
Adorian Doran604bf1add62e
diff --git a/apps/client/src/services/content_manager.spec.ts b/apps/client/src/services/content_manager.spec.tsindex 55de1d7eff..46fd226fd6 100644--- a/apps/client/src/services/content_manager.spec.ts+++ b/apps/client/src/services/content_manager.spec.ts@@ -9,6 +9,7 @@ import {findCategoryNotes,isCategoryEnabled,isUserContent,+ ownsCategoryTrigger,parseFilterTriggers,resolveProperties,setCategoryEnabled@@ -365,8 +366,8 @@ describe("setCategoryEnabled", () => {describe("findCategoryNotes", () => {it("runs the category's ordered query and drops built-in notes", async () => {- const mine = buildNote({ id: "mine123", title: "My theme" });- const builtIn = buildNote({ id: "_builtInTheme", title: "Built-in" });+ const mine = buildNote({ id: "mine123", title: "My theme", "#appTheme": "mine" });+ const builtIn = buildNote({ id: "_builtInTheme", title: "Built-in", "#appTheme": "built-in" });const search = vi.spyOn(searchService, "searchForNotes").mockResolvedValue([ mine, builtIn ]);const category = categoryById("themes");@@ -375,6 +376,37 @@ describe("findCategoryNotes", () => {expect(search).toHaveBeenCalledWith(buildCategoryQuery(category.filter, "title"));expect(notes).toEqual([ mine ]);});++ it("drops notes that merely inherit the trigger instead of owning it", async () => {+ // The search engine expands an inheritable attribute across the subtree, so the child comes+ // back as a match. It owns nothing to rename, so listing it would give a dead toggle.+ const parent = buildNote({+ title: "Owner",+ "#run(inheritable)": "daily",+ children: [ { title: "Inheritor" } ]+ });+ const [ child ] = await parent.getChildNotes();+ vi.spyOn(searchService, "searchForNotes").mockResolvedValue([ parent, child ]);++ expect(await findCategoryNotes(categoryById("backendScripts"), "title")).toEqual([ parent ]);+ });+});++describe("ownsCategoryTrigger", () => {+ it("accepts an owned trigger in either spelling, and rejects an inherited one", async () => {+ const scripts = categoryById("backendScripts");+ const parent = buildNote({+ title: "Owner",+ "#run(inheritable)": "daily",+ children: [ { title: "Inheritor" } ]+ });+ const [ child ] = await parent.getChildNotes();++ expect(ownsCategoryTrigger(parent, scripts)).toBe(true);+ expect(ownsCategoryTrigger(child, scripts)).toBe(false);+ expect(ownsCategoryTrigger(buildNote({ title: "Off", "#disabled:run": "daily" }), scripts)).toBe(true);+ expect(ownsCategoryTrigger(buildNote({ title: "Unrelated", "#widget": "" }), scripts)).toBe(false);+ });});describe("isUserContent", () => {diff --git a/apps/client/src/services/content_manager.ts b/apps/client/src/services/content_manager.tsindex c5cfa4bf05..21d797fba6 100644--- a/apps/client/src/services/content_manager.ts+++ b/apps/client/src/services/content_manager.ts@@ -220,7 +220,7 @@ export const CONTENT_CATEGORIES: ContentCategory[] = [export async function findCategoryNotes(category: ContentCategory, sortOrder: ContentSortOrder) {const notes = await search.searchForNotes(buildCategoryQuery(category.filter, sortOrder));- return notes.filter((note) => isUserContent(note));+ return notes.filter((note) => isUserContent(note) && ownsCategoryTrigger(note, category));}/**@@ -326,6 +326,18 @@ function toArray(value: string | string[]) {return Array.isArray(value) ? value : [ value ];}+/**+ * Whether the note itself carries one of the category's triggers.+ *+ * The search engine expands inheritable attributes across subtrees and templated notes, so a note+ * that merely *inherits* `#run` — every note using a template that declares it, for instance — comes+ * back as a match. Such a note owns no attribute to rename, so without this check it would be listed+ * as a permanently disabled row whose toggle does nothing.+ */+export function ownsCategoryTrigger(note: FNote, category: ContentCategory) {+ return getCategoryTriggers(note, category).length > 0;+}+/** Whether the note is currently active in this category, i.e. at least one trigger is not disabled. */export function isCategoryEnabled(note: FNote, category: ContentCategory) {return getCategoryTriggers(note, category).some(({ enabled }) => enabled);diff --git a/apps/client/src/widgets/type_widgets/options/content_manager.tsx b/apps/client/src/widgets/type_widgets/options/content_manager.tsxindex 51411b6d40..48b78def4e 100644--- a/apps/client/src/widgets/type_widgets/options/content_manager.tsx+++ b/apps/client/src/widgets/type_widgets/options/content_manager.tsx@@ -2,7 +2,7 @@ import "./content_manager.css";import clsx from "clsx";import type { TargetedMouseEvent } from "preact";-import { useCallback, useEffect, useState } from "preact/hooks";+import { useCallback, useEffect, useRef, useState } from "preact/hooks";import appContext from "../../../components/app_context";import type FNote from "../../../entities/fnote";@@ -51,10 +51,15 @@ function ContentItemMenu({ note }: { note: FNote }) {});} else if (command === "deleteNote") {// Search results carry virtual branches, which cannot be deleted.- const branchIds = note.getParentBranchIds().filter((branchId) => !branchId.startsWith("virt-"));- // `moveToParent` is off: the user is on the Content Manager, not on the note, so- // there is nothing to navigate away from.- void branches.deleteNotes(branchIds, false, false);+ const [ branchId ] = note.getParentBranchIds().filter((id) => !id.startsWith("virt-"));++ if (branchId) {+ // Deliberately one branch, not all of them: the dialog counts a note's+ // *other* placements to warn about clones and offer "delete all clones", so+ // passing every branch would zero that count and delete them all silently.+ // `moveToParent` is off — the user is on this page, not on the note.+ void branches.deleteNotes([ branchId ], false, false);+ }}}});@@ -226,12 +231,20 @@ function useCategoryNotes(sortOrder: ContentSortOrder) {// `null` until the first search resolves, so the page can tell "still loading" apart from// "genuinely nothing to show" instead of flashing the empty state on every open.const [ categories, setCategories ] = useState<CategoryNotes[] | null>(null);+ const latestRequest = useRef(0);const refresh = useCallback(async () => {- setCategories(await Promise.all(CONTENT_CATEGORIES.map(async (category) => ({+ const requestId = ++latestRequest.current;+ const refreshed = await Promise.all(CONTENT_CATEGORIES.map(async (category) => ({category,notes: await findCategoryNotes(category, sortOrder)- }))));+ })));++ // Both the sort order and `entitiesReloaded` trigger a refresh, so two can be in flight at+ // once. Drop a superseded response instead of letting it repaint over newer results.+ if (requestId === latestRequest.current) {+ setCategories(refreshed);+ }}, [ sortOrder ]);useEffect(() => { void refresh(); }, [ refresh ]);