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/collections/board: add a tooltip for the item counter
Adorian Doran1254e13b7882
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 79a8035414..cb002bdb62 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -3355,6 +3355,10 @@"archive-column": "Archive column","unarchive-column": "Unarchive column","archived-badge": "Archived",+ "card-count_one": "{{count}} item",+ "card-count_other": "{{count}} items",+ "card-count-with-archived_one": "{{count}} active item, {{archived}} archived",+ "card-count-with-archived_other": "{{count}} active items, {{archived}} archived","delete-column-confirmation": "Are you sure you want to delete this column? The corresponding attribute will be deleted in the notes under this column as well.","new-item": "New item","new-item-placeholder": "Enter note title...",diff --git a/apps/client/src/widgets/collections/board/column.tsx b/apps/client/src/widgets/collections/board/column.tsxindex a89ca3de3b..eed1ad670c 100644--- a/apps/client/src/widgets/collections/board/column.tsx+++ b/apps/client/src/widgets/collections/board/column.tsx@@ -16,6 +16,7 @@ import { DragData, TREE_CLIPBOARD_TYPE } from "../../note_tree";import ActionButton from "../../react/ActionButton";import Icon from "../../react/Icon";import { IconPickerButton } from "../../react/IconPicker";+import { useStaticTooltip } from "../../react/hooks";import NoteLink from "../../react/NoteLink";import { BoardActionsContext, BoardDragStateContext, TitleEditor } from ".";import BoardApi from "./api";@@ -192,7 +193,7 @@ export default function Column({: column}</span><div className="spacer" />- <span className="counter-badge">{columnItems?.length ?? 0}</span>+ <CountBadge items={columnItems} /><ActionButtonclassName="column-menu"icon="bx bx-dots-vertical-rounded"@@ -259,6 +260,27 @@ export default function Column({* The editor a new card is named in, opened by the button below the column or by its menu. The* state is the column's rather than this component's, since the menu is raised from the header.*/+/**+ * How many cards a column holds, and what they are when hovered.+ *+ * The archived ones are among them only while the board is showing archived notes; where it is not,+ * there are none to count and the badge says how many cards there are and no more.+ */+function CountBadge({ items }: { items?: { note: FNote }[] }) {+ const badgeRef = useRef<HTMLSpanElement>(null);+ const archived = items?.filter(({ note }) => note.isArchived).length ?? 0;+ const total = items?.length ?? 0;++ // Named on the element rather than handed to the tooltip, so the count still reads on hover+ // wherever the scripted tooltip does not come up.+ const title = archived+ ? t("board_view.card-count-with-archived", { count: total - archived, archived })+ : t("board_view.card-count", { count: total });+ useStaticTooltip(badgeRef);++ return <span ref={badgeRef} className="counter-badge" title={title}>{total}</span>;+}+function AddNewItem({ column, api, itemCount, isCreating, setIsCreating }: {column: string,api: BoardApi,diff --git a/apps/client/src/widgets/collections/board/index.spec.tsx b/apps/client/src/widgets/collections/board/index.spec.tsxindex 3179cf21b2..124a412a4d 100644--- a/apps/client/src/widgets/collections/board/index.spec.tsx+++ b/apps/client/src/widgets/collections/board/index.spec.tsx@@ -26,7 +26,9 @@ import { DEFAULT_COLUMN_ICON } from "./columns";// which is what makes the old column empty rather than merely renamed.vi.mock("../../../services/i18n", () => ({// i18next is never initialised under test, so a stock name the board writes would be undefined.- t: (key: string) => key,+ // What is interpolated is carried along, for the strings a test is about.+ t: (key: string, opts?: Record<string, unknown>) =>+ opts ? `${key}:${JSON.stringify(opts)}` : key,// Awaited by whatever waits for the catalogue; a mock without it rejects where it is read.translationsInitializedPromise: $.Deferred().resolve()}));@@ -437,6 +439,61 @@ describe("Board column rename", () => {expect(first.querySelector("h3 input")).not.toBeNull();});+ /** The tooltip is set on the element, which is where `useStaticTooltip` reads it back from. */+ function badgeTooltip(container: HTMLElement, index: number) {+ const badge = [ ...container.querySelectorAll(".board-column .counter-badge") ][index];+ // Bootstrap moves the attribute aside once it takes the tooltip over.+ return badge?.getAttribute("title") ?? badge?.getAttribute("data-bs-original-title");+ }++ it("counts the cards a column holds, and says so when hovered", async () => {+ const { container } = await setup();++ expect([ ...container.querySelectorAll(".board-column .counter-badge") ]+ .map(el => el.textContent)).toEqual([ "1", "1", "1" ]);+ expect(badgeTooltip(container, 0)).toBe('board_view.card-count:{"count":1}');+ });++ /**+ * Archived cards are among a column's only while the board is showing them; where it is not,+ * there are none to count and the badge says how many cards there are and no more.+ */+ it("tells the archived cards apart from the rest where the board shows them", async () => {+ const note = buildNote({+ title: "Board",+ "#collection": "",+ "#viewType": "board",+ "#includeArchived": "true",+ "#label:status(inheritable)":+ "promoted,alias=Status,single,select,options=To Do",+ children: [+ { title: "First", "#status": "To Do" },+ { title: "Second", "#status": "To Do" },+ { title: "Old", "#status": "To Do", "#archived": "" }+ ]+ });++ const mountPoint = document.createElement("div");+ container = mountPoint;+ document.body.appendChild(mountPoint);+ await act(async () => {+ render(+ <ParentComponent.Provider value={new Component()}>+ <Harness+ note={note}+ noteIds={[ ...note.getChildNoteIds() ]}+ initialConfig={{ columns: [ { value: "To Do" } ] }}+ />+ </ParentComponent.Provider>,+ mountPoint+ );+ });+ await act(async () => { await flush(); });++ expect(badgeTooltip(mountPoint, 0))+ .toBe('board_view.card-count-with-archived:{"count":2,"archived":1}');+ });+/** Renames the middle column, so a slot that is not the last one has to survive. */async function renameSecondColumn(container: HTMLElement, newName: string) {return renameColumnAt(container, 1, newName);