Skip to content

Build your personal knowledge base with Trilium Notes

feat(client): configurable search page size and visible result count

perfectra1nba38e8411cc6
diff --git a/apps/client/src/widgets/collections/Pagination.spec.tsx b/apps/client/src/widgets/collections/Pagination.spec.tsx
new file mode 100644
index 0000000000..5260fddbcf
--- /dev/null
+++ b/apps/client/src/widgets/collections/Pagination.spec.tsx
@@ -0,0 +1,77 @@
+/**
+ * Tests for the additive `defaultPageSize` parameter added to {@link usePagination} for C7:
+ * the caller-provided default (e.g. the synced `searchResultsPageSize` option) drives the page size
+ * when the note has no explicit `#pageSize` label, but an explicit label always keeps winning.
+ */
+import { render } from "preact";
+import { act } from "preact/test-utils";
+import { afterEach, beforeEach, describe, expect, it } from "vitest";
+
+import Component from "../../components/component";
+import type FNote from "../../entities/fnote";
+import { buildNote, buildNotes } from "../../test/easy-froca";
+import { ParentComponent } from "../react/react_utils";
+import { usePagination } from "./Pagination";
+
+let observed: { pageSize: number; pageCount: number; page: number } | undefined;
+
+// 25 real froca notes so usePagination's froca.getNotes() slice never round-trips to the server.
+const NOTE_IDS = buildNotes(Array.from({ length: 25 }, (_, i) => ({ id: `pag-${i}`, title: `N${i}` })));
+
+function Harness({ note, defaultPageSize }: { note: FNote; defaultPageSize?: number }) {
+ const { page, pageSize, pageCount } = usePagination(note, NOTE_IDS, defaultPageSize);
+ observed = { pageSize, pageCount, page };
+ return null;
+}
+
+describe("usePagination default page size", () => {
+ let container: HTMLElement | undefined;
+
+ beforeEach(() => {
+ observed = undefined;
+ });
+
+ afterEach(() => {
+ if (container) {
+ render(null, container);
+ container.remove();
+ container = undefined;
+ }
+ });
+
+ async function mount(note: FNote, defaultPageSize?: number) {
+ const parent = new Component();
+ // Capture in a local const so the type stays narrowed to HTMLElement inside the act() closure.
+ const el = document.createElement("div");
+ container = el;
+ document.body.appendChild(el);
+ await act(async () => {
+ render(
+ <ParentComponent.Provider value={parent}>
+ <Harness note={note} defaultPageSize={defaultPageSize} />
+ </ParentComponent.Provider>,
+ el
+ );
+ });
+ }
+
+ it("uses the provided default page size when the note has no #pageSize label", async () => {
+ const note = buildNote({ title: "Search", type: "search" });
+ await mount(note, 10);
+ expect(observed?.pageSize).toBe(10);
+ expect(observed?.pageCount).toBe(3); // 25 items / 10
+ });
+
+ it("lets an explicit #pageSize label override the provided default", async () => {
+ const note = buildNote({ title: "Search", type: "search", "#pageSize": "5" });
+ await mount(note, 50);
+ expect(observed?.pageSize).toBe(5);
+ expect(observed?.pageCount).toBe(5); // 25 items / 5
+ });
+
+ it("falls back to 20 when neither a label nor a positive default is given", async () => {
+ const note = buildNote({ title: "Search", type: "search" });
+ await mount(note, Number.NaN);
+ expect(observed?.pageSize).toBe(20);
+ });
+});
diff --git a/apps/client/src/widgets/collections/Pagination.tsx b/apps/client/src/widgets/collections/Pagination.tsx
index 256d3a39f9..76075668e6 100644
--- a/apps/client/src/widgets/collections/Pagination.tsx
+++ b/apps/client/src/widgets/collections/Pagination.tsx
@@ -122,25 +122,38 @@ function createSegment(start: number, length: number, currentPage: number, setPa
return children;
}
-export function usePagination(note: FNote, noteIds: string[]): PaginationContext {
+export function usePagination(note: FNote, noteIds: string[], defaultPageSize = 20): PaginationContext {
const [ page, setPage ] = useState(1);
const [ pageNotes, setPageNotes ] = useState<FNote[]>();
- // Parse page size.
- const [ pageSize ] = useNoteLabelInt(note, "pageSize");
- const normalizedPageSize = (pageSize && pageSize > 0 ? pageSize : 20);
+ // Parse page size. An explicit `#pageSize` label always wins; otherwise fall back to the
+ // caller-supplied default (e.g. the synced `searchResultsPageSize` option), and finally to 20
+ // when neither yields a usable positive size.
+ const [ labelPageSize ] = useNoteLabelInt(note, "pageSize");
+ const normalizedPageSize = (labelPageSize && labelPageSize > 0)
+ ? labelPageSize
+ : (defaultPageSize > 0 ? defaultPageSize : 20);
// Calculate start/end index.
const startIdx = (page - 1) * normalizedPageSize;
const endIdx = startIdx + normalizedPageSize;
const pageCount = Math.ceil(noteIds.length / normalizedPageSize);
+ // If the page size grows (or the result set shrinks) enough that the current page no longer
+ // exists, step back onto the last valid page so the slice below can't silently yield an empty
+ // page. A no-op in steady state, so existing list/grid callers are unaffected.
+ useEffect(() => {
+ if (pageCount > 0 && page > pageCount) {
+ setPage(pageCount);
+ }
+ }, [ page, pageCount ]);
+
// Obtain notes within the range.
const pageNoteIds = noteIds.slice(startIdx, Math.min(endIdx, noteIds.length));
useEffect(() => {
froca.getNotes(pageNoteIds).then(setPageNotes);
- }, [ note, noteIds, page, pageSize ]);
+ }, [ note, noteIds, page, normalizedPageSize ]);
return {
page, setPage, pageNotes, pageCount,