MCPcopy Index your code
hub / github.com/coder/coder / buildPagedList

Function buildPagedList

site/src/components/PaginationWidget/utils.ts:29–63  ·  view source on GitHub ↗
(
	numPages: number,
	activePage: number,
)

Source from the content-addressed store, hash-verified

27 * into a UI-friendly list.
28 */
29export const buildPagedList = (
30 numPages: number,
31 activePage: number,
32): ("left" | "right" | number)[] => {
33 if (numPages <= TOTAL_PAGE_BLOCKS) {
34 return range(1, numPages);
35 }
36
37 const isInvalidActivePage = activePage > numPages || activePage < 1;
38 const pageBeforeLast = numPages - 1;
39 const startPage = isInvalidActivePage
40 ? 1 + PAGE_NEIGHBORS
41 : Math.max(activePage - PAGE_NEIGHBORS, 2);
42 const endPage = isInvalidActivePage
43 ? numPages - PAGE_NEIGHBORS
44 : Math.min(activePage + PAGE_NEIGHBORS, pageBeforeLast);
45
46 let pages: ReturnType<typeof buildPagedList> = range(startPage, endPage);
47
48 const singleSpillOffset = PAGES_TO_DISPLAY - pages.length - 1;
49 const hasLeftOverflow = startPage > 2;
50 const hasRightOverflow = endPage < pageBeforeLast;
51
52 if (hasLeftOverflow && !hasRightOverflow) {
53 const extraPages = range(startPage - singleSpillOffset, startPage - 1);
54 pages = ["left", ...extraPages, ...pages];
55 } else if (!hasLeftOverflow && hasRightOverflow) {
56 const extraPages = range(endPage + 1, endPage + singleSpillOffset);
57 pages = [...pages, ...extraPages, "right"];
58 } else if (hasLeftOverflow && hasRightOverflow) {
59 pages = ["left", ...pages, "right"];
60 }
61
62 return [1, ...pages, numPages];
63};
64
65/**
66 * Calculates the current offset from the start of a paginated dataset

Callers 2

utils.test.tsFile · 0.90
PaginationRowFunction · 0.90

Calls 1

rangeFunction · 0.85

Tested by

no test coverage detected