Skip to content

Commit 9655368

Browse files
committed
chore: quick refactor for buildPagedList
1 parent 183cdf7 commit 9655368

File tree

1 file changed

+23
-24
lines changed
  • site/src/components/PaginationWidget

1 file changed

+23
-24
lines changed

site/src/components/PaginationWidget/utils.ts

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,36 +29,35 @@ export const buildPagedList = (
2929
numPages: number,
3030
activePage: number,
3131
): ("left" | "right" | number)[] => {
32-
if (numPages > TOTAL_PAGE_BLOCKS) {
33-
let pages = [];
34-
const leftBound = activePage - PAGE_NEIGHBORS;
35-
const rightBound = activePage + PAGE_NEIGHBORS;
36-
const beforeLastPage = numPages - 1;
37-
const startPage = leftBound > 2 ? leftBound : 2;
38-
const endPage = rightBound < beforeLastPage ? rightBound : beforeLastPage;
32+
if (numPages <= TOTAL_PAGE_BLOCKS) {
33+
return range(1, numPages);
34+
}
3935

40-
pages = range(startPage, endPage);
36+
const leftBound = activePage - PAGE_NEIGHBORS;
37+
const rightBound = activePage + PAGE_NEIGHBORS;
38+
const beforeLastPage = numPages - 1;
39+
const startPage = leftBound > 2 ? leftBound : 2;
40+
const endPage = rightBound < beforeLastPage ? rightBound : beforeLastPage;
4141

42-
const singleSpillOffset = PAGES_TO_DISPLAY - pages.length - 1;
43-
const hasLeftOverflow = startPage > 2;
44-
const hasRightOverflow = endPage < beforeLastPage;
45-
const leftOverflowPage = "left" as const;
46-
const rightOverflowPage = "right" as const;
42+
let pages: ReturnType<typeof buildPagedList> = range(startPage, endPage);
4743

48-
if (hasLeftOverflow && !hasRightOverflow) {
49-
const extraPages = range(startPage - singleSpillOffset, startPage - 1);
50-
pages = [leftOverflowPage, ...extraPages, ...pages];
51-
} else if (!hasLeftOverflow && hasRightOverflow) {
52-
const extraPages = range(endPage + 1, endPage + singleSpillOffset);
53-
pages = [...pages, ...extraPages, rightOverflowPage];
54-
} else if (hasLeftOverflow && hasRightOverflow) {
55-
pages = [leftOverflowPage, ...pages, rightOverflowPage];
56-
}
44+
const singleSpillOffset = PAGES_TO_DISPLAY - pages.length - 1;
45+
const hasLeftOverflow = startPage > 2;
46+
const hasRightOverflow = endPage < beforeLastPage;
47+
const leftOverflowPage = "left";
48+
const rightOverflowPage = "right";
5749

58-
return [1, ...pages, numPages];
50+
if (hasLeftOverflow && !hasRightOverflow) {
51+
const extraPages = range(startPage - singleSpillOffset, startPage - 1);
52+
pages = [leftOverflowPage, ...extraPages, ...pages];
53+
} else if (!hasLeftOverflow && hasRightOverflow) {
54+
const extraPages = range(endPage + 1, endPage + singleSpillOffset);
55+
pages = [...pages, ...extraPages, rightOverflowPage];
56+
} else if (hasLeftOverflow && hasRightOverflow) {
57+
pages = [leftOverflowPage, ...pages, rightOverflowPage];
5958
}
6059

61-
return range(1, numPages);
60+
return [1, ...pages, numPages];
6261
};
6362

6463
/**

0 commit comments

Comments
 (0)