-
Notifications
You must be signed in to change notification settings - Fork 198
pagination page.tsx #4
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,13 +3,16 @@ import CompanionCard from "@/components/CompanionCard"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import {getSubjectColor} from "@/lib/utils"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import SearchInput from "@/components/SearchInput"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import SubjectFilter from "@/components/SubjectFilter"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import Link from "next/link"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const CompanionsLibrary = async ({ searchParams }: SearchParams) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const filters = await searchParams; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const subject = filters.subject ? filters.subject : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const topic = filters.topic ? filters.topic : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const subject = filters.subject ?? ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const topic = filters.topic ?? ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const page = parseInt(filters.page) || 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const limit = 9; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const companions = await getAllCompanions({ subject, topic }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const companions = await getAllCompanions({ subject, topic, page, limit }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <main> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -20,6 +23,7 @@ const CompanionsLibrary = async ({ searchParams }: SearchParams) => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <SubjectFilter /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </section> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <section className="companions-grid"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {companions.map((companion) => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <CompanionCard | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -29,8 +33,25 @@ const CompanionsLibrary = async ({ searchParams }: SearchParams) => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ))} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </section> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <section className="flex justify-center gap-4 mb-5"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {page > 1 && ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Link href={`?page=${page - 1}&subject=${subject}&topic=${topic}`} className="px-4 py-2 rounded-xl bg-[#fe5933] border border-gray-300 shadow-sm text-sm font-medium text-gray-700 hover:bg-gray-100 hover:text-orange-300 transition-all duration-200 relative z-10"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ← Previous | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Link> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| )} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {companions.length === limit && ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Link | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| href={`?page=${page + 1}&subject=${subject}&topic=${topic}`} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| className="px-4 py-2 rounded-xl bg-[#fe5933] border border-gray-300 shadow-sm text-sm font-medium text-gray-700 hover:bg-gray-100 hover:text-orange-300 transition-all duration-200 relative z-10" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| > | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Next → | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Link> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| )} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </section> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+37
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Verification agent 🧩 Analysis chainImprove pagination logic and query string handling. The pagination implementation has several areas for improvement:
Consider this improved approach: - {companions.length === limit && (
+ {companions.length >= limit && (
<Link
- href={`?page=${page + 1}&subject=${subject}&topic=${topic}`}
+ href={`?page=${page + 1}&subject=${encodeURIComponent(subject)}&topic=${encodeURIComponent(topic)}`}
className="px-4 py-2 rounded-xl bg-[#fe5933] border border-gray-300 shadow-sm text-sm font-medium text-gray-700 hover:bg-gray-100 hover:text-orange-300 transition-all duration-200 relative z-10"
>
Next →
</Link>
)}Also apply URL encoding to the Previous link: - <Link href={`?page=${page - 1}&subject=${subject}&topic=${topic}`} className="px-4 py-2 rounded-xl bg-[#fe5933] border border-gray-300 shadow-sm text-sm font-medium text-gray-700 hover:bg-gray-100 hover:text-orange-300 transition-all duration-200 relative z-10">
+ <Link href={`?page=${page - 1}&subject=${encodeURIComponent(subject)}&topic=${encodeURIComponent(topic)}`} className="px-4 py-2 rounded-xl bg-[#fe5933] border border-gray-300 shadow-sm text-sm font-medium text-gray-700 hover:bg-gray-100 hover:text-orange-300 transition-all duration-200 relative z-10">Consider modifying the 🏁 Script executed: #!/bin/bash
# Locate and inspect the `getAllCompanions` implementation to verify its return values
rg -n "getAllCompanions" -n .
rg -C3 "getAllCompanions" -n .Length of output: 2423 🏁 Script executed: #!/bin/bash
# Display the implementation of getAllCompanions
sed -n '21,100p' lib/actions/companion.actions.ts
# Check for select parameters (to see if count is requested)
rg -n "select" -n lib/actions/companion.actions.ts
# Check for range usage (to see how pagination is applied)
rg -n "range" -n lib/actions/companion.actions.ts
# Check for explicit count usage
rg -n "count" -n lib/actions/companion.actions.tsLength of output: 3056 Refine pagination logic, encode query parameters, and address hover contrast To ensure accurate “Next” detection and safe URLs, fetch one extra item (or return a total count) from • lib/actions/companion.actions.ts Suggested diff: --- lib/actions/companion.actions.ts
@@ export const getAllCompanions = async ({ limit = 10, page = 1, subject, topic }: GetAllCompanions) => {
- query = query.range((page - 1) * limit, page * limit - 1);
- const { data: companions, error } = await query;
- if(error) throw new Error(error.message);
- return companions;
+ // fetch one extra to detect next page
+ query = query.range((page - 1) * limit, page * limit);
+ const { data, error } = await query;
+ if (error) throw new Error(error.message);
+ const hasNextPage = (data?.length ?? 0) > limit;
+ return {
+ companions: (data ?? []).slice(0, limit),
+ hasNextPage,
+ };--- app/companions/page.tsx
@@
- const companions = await getAllCompanions({ subject, topic, page, limit });
+ const { companions, hasNextPage } = await getAllCompanions({ subject, topic, page, limit });
@@
- {companions.length === limit && (
+ {hasNextPage && (
<Link
- href={`?page=${page + 1}&subject=${subject}&topic=${topic}`}
+ href={`?page=${page + 1}&subject=${encodeURIComponent(subject)}&topic=${encodeURIComponent(topic)}`}
className="px-4 py-2 rounded-xl bg-[#fe5933] border border-gray-300 shadow-sm text-sm font-medium text-gray-700 hover:bg-gray-100 hover:text-orange-300 transition-all duration-200 relative z-10"
>
Next →
</Link>And update the Previous link similarly: -<Link href={`?page=${page - 1}&subject=${subject}&topic=${topic}`} …
+<Link href={`?page=${page - 1}&subject=${encodeURIComponent(subject)}&topic=${encodeURIComponent(topic)}`} …Lastly, confirm your hover colors (background vs. text) meet WCAG contrast requirements. 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </main> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export default CompanionsLibrary | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
🛠️ Refactor suggestion
Improve page parameter validation and clarify limit choice.
Page validation issue:
parseInt(filters.page)can returnNaNfor invalid inputs, and negative numbers aren't handled.Limit discrepancy: The PR objectives mention displaying buttons when cards exceed 10, but the limit is set to 9.
Apply this diff to improve page validation:
Please clarify if the limit should be 9 or 10 to align with the PR objectives.
📝 Committable suggestion
🤖 Prompt for AI Agents