Skip to content

Commit 183cdf7

Browse files
committed
docs: clean up comments
1 parent a08a149 commit 183cdf7

File tree

1 file changed

+20
-13
lines changed
  • site/src/components/PaginationWidget

1 file changed

+20
-13
lines changed

site/src/components/PaginationWidget/utils.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,35 @@
11
/**
22
* Generates a ranged array with an option to step over values.
33
* Shamelessly stolen from:
4-
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from#sequence_generator_range
4+
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from#sequence_generator_range}
55
*/
66
const range = (start: number, stop: number, step = 1) =>
77
Array.from({ length: (stop - start) / step + 1 }, (_, i) => start + i * step);
88

99
export const DEFAULT_RECORDS_PER_PAGE = 25;
10-
// Number of pages to the left or right of the current page selection.
10+
11+
// Number of pages to display on either side of the current page selection
1112
const PAGE_NEIGHBORS = 1;
12-
// Number of pages displayed for cases where there are multiple ellipsis showing. This can be
13-
// thought of as the minimum number of page numbers to display when multiple ellipsis are showing.
13+
14+
// Minimum number of pages to display at all times (assuming there are enough
15+
// pages). Needed to handle case where there are the left and right
16+
// placeholder/ellipsis elements are both showing
1417
const PAGES_TO_DISPLAY = PAGE_NEIGHBORS * 2 + 3;
15-
// Total page blocks(page numbers or ellipsis) displayed, including the maximum number of ellipsis (2).
16-
// This gives us maximum number of 7 page blocks to be displayed when the page neighbors value is 1.
17-
const NUM_PAGE_BLOCKS = PAGES_TO_DISPLAY + 2;
18+
19+
// Total number of pagination elements to display (accounting for visible pages
20+
// and up to two ellipses placeholders). With 1 page neighbor on either side,
21+
// the UI will show up to seven elements total
22+
const TOTAL_PAGE_BLOCKS = PAGES_TO_DISPLAY + 2;
1823

1924
/**
20-
* Builds a list of pages based on how many pages exist and where the user is in their navigation of those pages.
21-
* List result is used to from the buttons that make up the Pagination Widget
25+
* Takes the total number of pages from a pagination result, and truncates it
26+
* into a UI-friendly list.
2227
*/
2328
export const buildPagedList = (
2429
numPages: number,
2530
activePage: number,
2631
): ("left" | "right" | number)[] => {
27-
if (numPages > NUM_PAGE_BLOCKS) {
32+
if (numPages > TOTAL_PAGE_BLOCKS) {
2833
let pages = [];
2934
const leftBound = activePage - PAGE_NEIGHBORS;
3035
const rightBound = activePage + PAGE_NEIGHBORS;
@@ -56,12 +61,14 @@ export const buildPagedList = (
5661
return range(1, numPages);
5762
};
5863

59-
// pages count from 1
60-
export const getOffset = (page: number, limit: number): number => {
64+
/**
65+
* Calculates the current offset from the start of a paginated dataset
66+
*/
67+
export const getOffset = (page: number, pageSize: number): number => {
6168
const pageIsValid = Number.isInteger(page) && page >= 1;
6269
const pageToUse = pageIsValid ? page : 1;
6370

64-
return (pageToUse - 1) * limit;
71+
return (pageToUse - 1) * pageSize;
6572
};
6673

6774
export const isNonInitialPage = (searchParams: URLSearchParams): boolean => {

0 commit comments

Comments
 (0)