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/space usage: fix an issue
Adorian Doranb3c3d258155c
diff --git a/apps/client/src/widgets/type_widgets/space_usage/selection.spec.tsx b/apps/client/src/widgets/type_widgets/space_usage/selection.spec.tsxindex 7740be3034..a448f2fc97 100644--- a/apps/client/src/widgets/type_widgets/space_usage/selection.spec.tsx+++ b/apps/client/src/widgets/type_widgets/space_usage/selection.spec.tsx@@ -53,10 +53,12 @@ describe("SelectionStrip", () => {});it("stands in with the note IDs until the titles arrive", () => {- const probe = renderStrip({ markId: "n1", notePath: [ "root", "n1" ], size: 10 });+ const probe = renderStrip({ markId: "n1", notePath: [ "root", "p1", "n1" ], size: 10 });- // Rendered before froca has answered: the strip draws rather than waiting.- expect(textOf(probe, ".space-usage-selection-name")).toBe("");+ // Rendered before froca has answered: the strip draws rather than waiting, and what it draws+ // is the path it was given rather than nothing.+ expect(textOf(probe, ".space-usage-selection-name")).toBe("n1");+ expect(textOf(probe, ".space-usage-selection-path")).toBe("root › p1");});it("names a mark that stands for no note by its own label, with no location to show", () => {@@ -67,6 +69,42 @@ describe("SelectionStrip", () => {expect(textOf(probe, ".space-usage-selection-size")).toBe("2 KiB");});+ it("never names one mark with another's titles, however late they arrive", async () => {+ const probe = renderStrip({ markId: "n1", notePath: [ "root", "p1", "n1" ], size: 10 });++ await vi.waitFor(() =>+ expect(textOf(probe, ".space-usage-selection-name")).toBe("title:n1"));++ // A second mark, drawn before froca has answered for it: it stands in with its own note IDs+ // rather than wearing the last mark's name beside its own size.+ render(+ <SelectionStrip selection={{ markId: "n2", notePath: [ "root", "p2", "n2" ], size: 20 }} />,+ probe+ );++ expect(textOf(probe, ".space-usage-selection-name")).toBe("n2");+ expect(textOf(probe, ".space-usage-selection-path")).toBe("root › p2");++ await vi.waitFor(() =>+ expect(textOf(probe, ".space-usage-selection-name")).toBe("title:n2"));+ });++ it("leaves a mark that names no note without a path, whatever was named before it", async () => {+ const probe = renderStrip({ markId: "n1", notePath: [ "root", "p1", "n1" ], size: 10 });++ await vi.waitFor(() =>+ expect(textOf(probe, ".space-usage-selection-path")).toBe("title:root › title:p1"));++ render(+ <SelectionStrip selection={{ markId: "/deleted-notes", label: "Deleted items", size: 20 }} />,+ probe+ );++ // The crowd it stands for sits nowhere, so the line above its name is empty at once.+ expect(textOf(probe, ".space-usage-selection-path")).toBe("");+ expect(textOf(probe, ".space-usage-selection-name")).toBe("Deleted items");+ });+it("says what to do while nothing is chosen, naming nothing", () => {const probe = renderStrip(null);diff --git a/apps/client/src/widgets/type_widgets/space_usage/selection.tsx b/apps/client/src/widgets/type_widgets/space_usage/selection.tsxindex e362582932..2c4d087b0b 100644--- a/apps/client/src/widgets/type_widgets/space_usage/selection.tsx+++ b/apps/client/src/widgets/type_widgets/space_usage/selection.tsx@@ -102,11 +102,10 @@ export default function SelectionStrip({ selection, containerRef }: {* strip asks again only when a different mark is picked.*/function usePathTitles(notePath: string[] | undefined) {- const [ titles, setTitles ] = useState<string[]>([]);+ const [ resolved, setResolved ] = useState<{ notePath: string[], titles: string[] }>();useEffect(() => {if (!notePath) {- setTitles([]);return;}@@ -117,7 +116,7 @@ function usePathTitles(notePath: string[] | undefined) {if (!cancelled) {const byNoteId = new Map(notes.map((note) => [ note.noteId, note.title ]));- setTitles(notePath.map((noteId) => byNoteId.get(noteId) ?? noteId));+ setResolved({ notePath, titles: notePath.map((noteId) => byNoteId.get(noteId) ?? noteId) });}});@@ -126,5 +125,10 @@ function usePathTitles(notePath: string[] | undefined) {};}, [ notePath ]);- return titles;+ // Kept with the path they were asked for, and handed back only for that one. Titles arrive a+ // tick or more after the mark that wanted them, and the rest of the strip is drawn from the mark+ // itself: without this, the moment between one selection and its titles is drawn with the last+ // one's, which names the wrong note beside the right size — and a mark that names no note at all+ // would go on wearing the note's ancestors under its label.+ return resolved && resolved.notePath === notePath ? resolved.titles : notePath ?? [];}