Skip to content

Commit a9c52c6

Browse files
authored
Merge pull request #522 from iitzIrFan/bug/leaderboard_pagination
Implement pagination with ellipsis for improved navigation in leaderboard
2 parents d4ba9b8 + ceda8dc commit a9c52c6

File tree

2 files changed

+122
-2
lines changed

2 files changed

+122
-2
lines changed

src/components/dashboard/LeaderBoard/leaderboard.css

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,27 @@
540540
.page-numbers {
541541
display: flex;
542542
gap: 8px;
543+
flex-wrap: wrap;
544+
justify-content: center;
545+
}
546+
547+
/* Pagination ellipsis */
548+
.pagination-ellipsis {
549+
display: flex;
550+
justify-content: center;
551+
align-items: center;
552+
width: 36px;
553+
height: 36px;
554+
font-weight: bold;
555+
user-select: none;
556+
}
557+
558+
.light .pagination-ellipsis {
559+
color: #666;
560+
}
561+
562+
.dark .pagination-ellipsis {
563+
color: #b3b3b3;
543564
}
544565

545566
/* CTA Footer */
@@ -643,4 +664,29 @@
643664
.points-cell {
644665
justify-self: center;
645666
}
667+
668+
/* Pagination mobile styles */
669+
.pagination {
670+
flex-wrap: wrap;
671+
gap: 6px;
672+
padding: 16px 0;
673+
}
674+
675+
.page-numbers {
676+
order: 2;
677+
width: 100%;
678+
justify-content: center;
679+
margin-top: 8px;
680+
}
681+
682+
.pagination-btn {
683+
width: 36px;
684+
height: 36px;
685+
}
686+
687+
.page-btn, .pagination-ellipsis {
688+
width: 32px;
689+
height: 32px;
690+
font-size: 14px;
691+
}
646692
}

src/components/dashboard/LeaderBoard/leaderboard.tsx

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,91 @@ export default function LeaderBoard(): JSX.Element {
9494

9595
const renderPaginationButtons = () => {
9696
const pages = [];
97-
for (let i = 1; i <= totalPages; i++) {
97+
const maxVisibleButtons = 5; // Maximum number of page buttons to show directly
98+
99+
// Special case: if we have 7 or fewer pages, show all of them without ellipsis
100+
if (totalPages <= 7) {
101+
for (let i = 1; i <= totalPages; i++) {
102+
pages.push(
103+
<button
104+
key={i}
105+
onClick={() => paginate(i)}
106+
className={`page-btn ${currentPage === i ? "active" : ""}`}
107+
>
108+
{i}
109+
</button>
110+
);
111+
}
112+
return pages;
113+
}
114+
115+
// For more than 7 pages, use the ellipsis approach
116+
117+
// Always show first page
118+
pages.push(
119+
<button
120+
key={1}
121+
onClick={() => paginate(1)}
122+
className={`page-btn ${currentPage === 1 ? "active" : ""}`}
123+
>
124+
1
125+
</button>
126+
);
127+
128+
// Calculate the range of pages to show (middle section)
129+
// We want to show current page and 1-2 pages before and after when possible
130+
let startPage = Math.max(2, currentPage - 1);
131+
let endPage = Math.min(totalPages - 1, currentPage + 1);
132+
133+
// Adjust start and end page if we're near the beginning or end
134+
if (currentPage <= 3) {
135+
// Near the beginning, show pages 2, 3, 4
136+
startPage = 2;
137+
endPage = Math.min(4, totalPages - 1);
138+
} else if (currentPage >= totalPages - 2) {
139+
// Near the end, show the three pages before the last page
140+
endPage = totalPages - 1;
141+
startPage = Math.max(2, totalPages - 3);
142+
}
143+
144+
// Show ellipsis if needed before the middle range
145+
if (startPage > 2) {
146+
pages.push(
147+
<span key="ellipsis-1" className="pagination-ellipsis">...</span>
148+
);
149+
}
150+
151+
// Show pages in the middle range
152+
for (let i = startPage; i <= endPage; i++) {
98153
pages.push(
99154
<button
100155
key={i}
101156
onClick={() => paginate(i)}
102-
className={`page-btn ${currentPage === i ? "active" : ""} `}
157+
className={`page-btn ${currentPage === i ? "active" : ""}`}
103158
>
104159
{i}
105160
</button>
106161
);
107162
}
163+
164+
// Show ellipsis if needed after the middle range
165+
if (endPage < totalPages - 1) {
166+
pages.push(
167+
<span key="ellipsis-2" className="pagination-ellipsis">...</span>
168+
);
169+
}
170+
171+
// Always show last page
172+
pages.push(
173+
<button
174+
key={totalPages}
175+
onClick={() => paginate(totalPages)}
176+
className={`page-btn ${currentPage === totalPages ? "active" : ""}`}
177+
>
178+
{totalPages}
179+
</button>
180+
);
181+
108182
return pages;
109183
};
110184

0 commit comments

Comments
 (0)