Skip to content

Commit cf89b9b

Browse files
committed
refactor: clean up math calculations for buildPagedList
1 parent 9655368 commit cf89b9b

File tree

1 file changed

+7
-11
lines changed
  • site/src/components/PaginationWidget

1 file changed

+7
-11
lines changed

site/src/components/PaginationWidget/utils.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,24 @@ export const buildPagedList = (
3333
return range(1, numPages);
3434
}
3535

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;
36+
const pageBeforeLast = numPages - 1;
37+
const startPage = Math.max(activePage - PAGE_NEIGHBORS, 2);
38+
const endPage = Math.min(activePage + PAGE_NEIGHBORS, pageBeforeLast);
4139

4240
let pages: ReturnType<typeof buildPagedList> = range(startPage, endPage);
4341

4442
const singleSpillOffset = PAGES_TO_DISPLAY - pages.length - 1;
4543
const hasLeftOverflow = startPage > 2;
46-
const hasRightOverflow = endPage < beforeLastPage;
47-
const leftOverflowPage = "left";
48-
const rightOverflowPage = "right";
44+
const hasRightOverflow = endPage < pageBeforeLast;
4945

5046
if (hasLeftOverflow && !hasRightOverflow) {
5147
const extraPages = range(startPage - singleSpillOffset, startPage - 1);
52-
pages = [leftOverflowPage, ...extraPages, ...pages];
48+
pages = ["left", ...extraPages, ...pages];
5349
} else if (!hasLeftOverflow && hasRightOverflow) {
5450
const extraPages = range(endPage + 1, endPage + singleSpillOffset);
55-
pages = [...pages, ...extraPages, rightOverflowPage];
51+
pages = [...pages, ...extraPages, "right"];
5652
} else if (hasLeftOverflow && hasRightOverflow) {
57-
pages = [leftOverflowPage, ...pages, rightOverflowPage];
53+
pages = ["left", ...pages, "right"];
5854
}
5955

6056
return [1, ...pages, numPages];

0 commit comments

Comments
 (0)