Repository navigation
fix(web): link the last page in Caps pagination #3569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AryaKesharwani
wants to merge
2
commits into
CapSoftware:main
Choose a base branch
from
AryaKesharwani:fix/caps-pagination-last-page
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { getPaginationItems } from "@/app/(org)/dashboard/caps/components/pagination-items"; | ||
|
|
||
| describe("getPaginationItems", () => { | ||
| it("always links the first and last pages", () => { | ||
| expect(getPaginationItems(1, 4)).toEqual([1, 2, 3, 4]); | ||
| expect(getPaginationItems(1, 10)).toEqual([1, 2, "ellipsis", 10]); | ||
| expect(getPaginationItems(10, 10)).toEqual([1, "ellipsis", 9, 10]); | ||
| }); | ||
|
|
||
| it("shows neighbours of the current page with gaps marked once", () => { | ||
| expect(getPaginationItems(3, 4)).toEqual([1, 2, 3, 4]); | ||
| expect(getPaginationItems(5, 10)).toEqual([ | ||
| 1, | ||
| "ellipsis", | ||
| 4, | ||
| 5, | ||
| 6, | ||
| "ellipsis", | ||
| 10, | ||
| ]); | ||
| }); | ||
|
|
||
| it("fills a one-page gap with the page instead of an ellipsis", () => { | ||
| expect(getPaginationItems(4, 6)).toEqual([1, 2, 3, 4, 5, 6]); | ||
| }); | ||
|
|
||
| it("handles small and out-of-range page counts", () => { | ||
| expect(getPaginationItems(1, 1)).toEqual([1]); | ||
| expect(getPaginationItems(1, 2)).toEqual([1, 2]); | ||
| expect(getPaginationItems(99, 4)).toEqual([1, "ellipsis", 4]); | ||
| expect(getPaginationItems(1, 0)).toEqual([]); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
apps/web/app/(org)/dashboard/caps/components/pagination-items.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| export type PaginationItemValue = number | "ellipsis"; | ||
|
|
||
| export function getPaginationItems( | ||
| currentPage: number, | ||
| totalPages: number, | ||
| ): PaginationItemValue[] { | ||
| const pages = [ | ||
| ...new Set([1, currentPage - 1, currentPage, currentPage + 1, totalPages]), | ||
| ] | ||
| .filter((page) => page >= 1 && page <= totalPages) | ||
| .sort((a, b) => a - b); | ||
|
|
||
| const items: PaginationItemValue[] = []; | ||
| let previous = 0; | ||
| for (const page of pages) { | ||
| if (page - previous === 2) items.push(previous + 1); | ||
| else if (page - previous > 2) items.push("ellipsis"); | ||
| items.push(page); | ||
| previous = page; | ||
| } | ||
| return items; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new test is in
__tests__/unit, separate from the helper it tests. The repository guide requires TS/JS tests to be named*.test.ts(x)near their sources. Move it besidepagination-items.tsand update test discovery as needed, since the current configuration only includes tests under__tests__. This repository requirement must be satisfied before merging.Context Used: AGENTS.md (source)
Prompt To Fix With AI
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kept it in
apps/web/__tests__/unit, since that's where the other web unit tests live and what the Vitest config picks up.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right.
apps/web/vitest.config.tsonly discovers tests under__tests__, andapps/web/__tests__/unitis where the web unit tests are organized. I overstated the placement requirement by suggesting the test should be colocated and that discovery needed updating. This finding isn't valid; keeping the test there is consistent with the existing setup.Tip: You can customize Greptile's behavior for this repo with
.greptile/rules.mdand.greptile/config.json.