fix(web): Adjust current page in IconSearch on pagination changes

This commit is contained in:
Bjorn Lammers 2025-04-24 16:13:42 +02:00
parent e9fe6d3842
commit f32b62009b

View File

@ -227,6 +227,21 @@ export function IconSearch({ icons }: IconSearchProps) {
[pathname, router, initialSort], [pathname, router, initialSort],
) )
// Validate currentPage when iconsPerPage or filteredIcons change
useEffect(() => {
// Calculate new total pages
const totalPages = Math.ceil(filteredIcons.length / iconsPerPage)
// If current page is out of bounds, adjust it
if (currentPage > totalPages && totalPages > 0) {
// Update current page state
setCurrentPage(totalPages)
// Update URL to reflect the adjusted page
updateResults(searchQuery, selectedCategories, sortOption, totalPages)
}
}, [iconsPerPage, filteredIcons.length, currentPage, searchQuery, selectedCategories, sortOption, updateResults])
const handleSearch = useCallback( const handleSearch = useCallback(
(query: string) => { (query: string) => {
setSearchQuery(query) setSearchQuery(query)