From f32b62009bae292ae56a4e8533965e088680fa97 Mon Sep 17 00:00:00 2001 From: Bjorn Lammers Date: Thu, 24 Apr 2025 16:13:42 +0200 Subject: [PATCH] fix(web): Adjust current page in IconSearch on pagination changes --- web/src/app/icons/components/icon-search.tsx | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/web/src/app/icons/components/icon-search.tsx b/web/src/app/icons/components/icon-search.tsx index 62f3b924..4f8bc209 100644 --- a/web/src/app/icons/components/icon-search.tsx +++ b/web/src/app/icons/components/icon-search.tsx @@ -227,6 +227,21 @@ export function IconSearch({ icons }: IconSearchProps) { [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( (query: string) => { setSearchQuery(query)