-
Notifications
You must be signed in to change notification settings - Fork 5
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
feat: group pagination #92
Changes from all commits
89ead32
741413b
12a798b
3ff9869
bc023bb
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 | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -18,6 +18,7 @@ | |||||||||||||||||||||||||||
import { GroupInfo } from '../modals/groupInfo'; | ||||||||||||||||||||||||||||
import { MemberManagementModal } from '../modals/memberManagementModal'; | ||||||||||||||||||||||||||||
import { useChain } from '@cosmos-kit/react'; | ||||||||||||||||||||||||||||
import useIsMobile from '@/hooks/useIsMobile'; | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
export function YourGroups({ | ||||||||||||||||||||||||||||
groups, | ||||||||||||||||||||||||||||
|
@@ -31,6 +32,11 @@ | |||||||||||||||||||||||||||
refetch: () => void; | ||||||||||||||||||||||||||||
}) { | ||||||||||||||||||||||||||||
const [searchTerm, setSearchTerm] = useState(''); | ||||||||||||||||||||||||||||
const [currentPage, setCurrentPage] = useState(1); | ||||||||||||||||||||||||||||
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. Reset currentPage when search term changes The currentPage should be reset to 1 when the search term changes to avoid showing empty pages. Add a useEffect hook to handle this: +useEffect(() => {
+ setCurrentPage(1);
+}, [searchTerm]);
|
||||||||||||||||||||||||||||
const isMobile = useIsMobile(); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
const pageSize = isMobile ? 6 : 8; | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
const [selectedGroup, setSelectedGroup] = useState<{ | ||||||||||||||||||||||||||||
policyAddress: string; | ||||||||||||||||||||||||||||
name: string; | ||||||||||||||||||||||||||||
|
@@ -44,6 +50,12 @@ | |||||||||||||||||||||||||||
(group.ipfsMetadata?.title || 'Untitled Group').toLowerCase().includes(searchTerm.toLowerCase()) | ||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
const totalPages = Math.ceil(filteredGroups.length / pageSize); | ||||||||||||||||||||||||||||
const paginatedGroups = filteredGroups.slice( | ||||||||||||||||||||||||||||
(currentPage - 1) * pageSize, | ||||||||||||||||||||||||||||
currentPage * pageSize | ||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||
Comment on lines
+53
to
+57
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. 🛠️ Refactor suggestion Optimize pagination calculations with useMemo The paginatedGroups calculation should be memoized to prevent unnecessary recalculations on every render. - const totalPages = Math.ceil(filteredGroups.length / pageSize);
- const paginatedGroups = filteredGroups.slice(
- (currentPage - 1) * pageSize,
- currentPage * pageSize
- );
+ const { totalPages, paginatedGroups } = useMemo(() => {
+ const total = Math.max(1, Math.ceil(filteredGroups.length / pageSize));
+ const paginated = filteredGroups.slice(
+ (currentPage - 1) * pageSize,
+ currentPage * pageSize
+ );
+ return { totalPages: total, paginatedGroups: paginated };
+ }, [filteredGroups, currentPage, pageSize]); 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
useEffect(() => { | ||||||||||||||||||||||||||||
// Check if there's a policy address in the URL on component mount | ||||||||||||||||||||||||||||
const { policyAddress } = router.query; | ||||||||||||||||||||||||||||
|
@@ -134,7 +146,7 @@ | |||||||||||||||||||||||||||
</thead> | ||||||||||||||||||||||||||||
<tbody className="space-y-4" role="rowgroup"> | ||||||||||||||||||||||||||||
{isLoading | ||||||||||||||||||||||||||||
? Array(10) | ||||||||||||||||||||||||||||
? Array(8) | ||||||||||||||||||||||||||||
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. 🛠️ Refactor suggestion Use pageSize for skeleton row count The number of skeleton rows should match the pageSize variable to maintain consistency between loading and loaded states. - ? Array(8)
+ ? Array(pageSize) 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||
.fill(0) | ||||||||||||||||||||||||||||
.map((_, index) => ( | ||||||||||||||||||||||||||||
<tr key={index} data-testid="skeleton-row"> | ||||||||||||||||||||||||||||
|
@@ -174,12 +186,14 @@ | |||||||||||||||||||||||||||
</td> | ||||||||||||||||||||||||||||
</tr> | ||||||||||||||||||||||||||||
)) | ||||||||||||||||||||||||||||
: filteredGroups.map((group, index) => ( | ||||||||||||||||||||||||||||
: paginatedGroups.map((group, index) => ( | ||||||||||||||||||||||||||||
<GroupRow | ||||||||||||||||||||||||||||
key={index} | ||||||||||||||||||||||||||||
group={group} | ||||||||||||||||||||||||||||
proposals={ | ||||||||||||||||||||||||||||
group.policies && group.policies.length > 0 | ||||||||||||||||||||||||||||
group.policies && | ||||||||||||||||||||||||||||
group.policies.length > 0 && | ||||||||||||||||||||||||||||
proposals[group.policies[0].address] | ||||||||||||||||||||||||||||
? proposals[group.policies[0].address] | ||||||||||||||||||||||||||||
: [] | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
@@ -188,6 +202,70 @@ | |||||||||||||||||||||||||||
))} | ||||||||||||||||||||||||||||
</tbody> | ||||||||||||||||||||||||||||
</table> | ||||||||||||||||||||||||||||
{totalPages > 1 && ( | ||||||||||||||||||||||||||||
<div | ||||||||||||||||||||||||||||
className="flex items-center justify-center gap-2" | ||||||||||||||||||||||||||||
onClick={e => e.stopPropagation()} | ||||||||||||||||||||||||||||
role="navigation" | ||||||||||||||||||||||||||||
aria-label="Pagination" | ||||||||||||||||||||||||||||
> | ||||||||||||||||||||||||||||
<button | ||||||||||||||||||||||||||||
onClick={e => { | ||||||||||||||||||||||||||||
e.stopPropagation(); | ||||||||||||||||||||||||||||
setCurrentPage(prev => Math.max(1, prev - 1)); | ||||||||||||||||||||||||||||
}} | ||||||||||||||||||||||||||||
disabled={currentPage === 1 || isLoading} | ||||||||||||||||||||||||||||
className="p-2 hover:bg-[#FFFFFF1A] rounded-lg transition-colors disabled:opacity-50 disabled:cursor-not-allowed" | ||||||||||||||||||||||||||||
aria-label="Previous page" | ||||||||||||||||||||||||||||
> | ||||||||||||||||||||||||||||
‹ | ||||||||||||||||||||||||||||
</button> | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
{[...Array(totalPages)].map((_, index) => { | ||||||||||||||||||||||||||||
const pageNum = index + 1; | ||||||||||||||||||||||||||||
if ( | ||||||||||||||||||||||||||||
pageNum === 1 || | ||||||||||||||||||||||||||||
pageNum === totalPages || | ||||||||||||||||||||||||||||
(pageNum >= currentPage - 1 && pageNum <= currentPage + 1) | ||||||||||||||||||||||||||||
) { | ||||||||||||||||||||||||||||
return ( | ||||||||||||||||||||||||||||
<button | ||||||||||||||||||||||||||||
key={pageNum} | ||||||||||||||||||||||||||||
onClick={e => { | ||||||||||||||||||||||||||||
e.stopPropagation(); | ||||||||||||||||||||||||||||
setCurrentPage(pageNum); | ||||||||||||||||||||||||||||
}} | ||||||||||||||||||||||||||||
className={`w-8 h-8 flex items-center justify-center rounded-lg transition-colors | ||||||||||||||||||||||||||||
${currentPage === pageNum ? 'bg-[#FFFFFF1A] text-white' : 'hover:bg-[#FFFFFF1A]'}`} | ||||||||||||||||||||||||||||
aria-label={`Page ${pageNum}`} | ||||||||||||||||||||||||||||
aria-current={currentPage === pageNum ? 'page' : undefined} | ||||||||||||||||||||||||||||
> | ||||||||||||||||||||||||||||
{pageNum} | ||||||||||||||||||||||||||||
</button> | ||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||
} else if (pageNum === currentPage - 2 || pageNum === currentPage + 2) { | ||||||||||||||||||||||||||||
return ( | ||||||||||||||||||||||||||||
<span key={pageNum} aria-hidden="true"> | ||||||||||||||||||||||||||||
... | ||||||||||||||||||||||||||||
</span> | ||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
return null; | ||||||||||||||||||||||||||||
})} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
<button | ||||||||||||||||||||||||||||
onClick={e => { | ||||||||||||||||||||||||||||
e.stopPropagation(); | ||||||||||||||||||||||||||||
setCurrentPage(prev => Math.min(totalPages, prev + 1)); | ||||||||||||||||||||||||||||
}} | ||||||||||||||||||||||||||||
disabled={currentPage === totalPages || isLoading} | ||||||||||||||||||||||||||||
className="p-2 hover:bg-[#FFFFFF1A] rounded-lg transition-colors disabled:opacity-50 disabled:cursor-not-allowed" | ||||||||||||||||||||||||||||
aria-label="Next page" | ||||||||||||||||||||||||||||
> | ||||||||||||||||||||||||||||
› | ||||||||||||||||||||||||||||
</button> | ||||||||||||||||||||||||||||
</div> | ||||||||||||||||||||||||||||
)} | ||||||||||||||||||||||||||||
</div> | ||||||||||||||||||||||||||||
</div> | ||||||||||||||||||||||||||||
<div className="mt-6 w-full justify-center md:hidden block"> | ||||||||||||||||||||||||||||
|
@@ -247,6 +325,8 @@ | |||||||||||||||||||||||||||
/> | ||||||||||||||||||||||||||||
</React.Fragment> | ||||||||||||||||||||||||||||
))} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
{/* Add pagination controls */} | ||||||||||||||||||||||||||||
</div> | ||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { useState, useEffect } from 'react'; | ||
|
||
const useIsMobile = (breakpoint: number = 1024) => { | ||
const [isMobile, setIsMobile] = useState(false); | ||
|
||
useEffect(() => { | ||
// Only run on client side | ||
if (typeof window === 'undefined') { | ||
return; | ||
} | ||
|
||
const checkIsMobile = () => { | ||
setIsMobile(window.innerWidth <= breakpoint); | ||
}; | ||
|
||
// Initial check | ||
checkIsMobile(); | ||
|
||
// Add event listener | ||
window.addEventListener('resize', checkIsMobile); | ||
|
||
// Cleanup | ||
return () => window.removeEventListener('resize', checkIsMobile); | ||
}, [breakpoint]); | ||
|
||
return isMobile; | ||
}; | ||
|
||
export default useIsMobile; |
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
Add test coverage for important edge cases.
While the current test suite covers basic pagination functionality well, consider adding the following test cases: