From ad5ce609fc0c7c3674999d9e99d2eae35ab7b211 Mon Sep 17 00:00:00 2001 From: vikingowl Date: Mon, 14 Apr 2025 21:58:54 +0200 Subject: [PATCH] Improve URL parameter handling and add error handling in filters Refactored the `initFromUrl` function to handle edge cases, including invalid or malformed URL parameters, and added a fallback to default filter options in case of errors. Improved validation for `page`, `status`, and `repo` parameters, ensuring robust behavior and logging errors for debugging. --- .../composables/Packages/usePackageFilters.ts | 77 +++++++++++++++---- 1 file changed, 60 insertions(+), 17 deletions(-) diff --git a/frontend/src/composables/Packages/usePackageFilters.ts b/frontend/src/composables/Packages/usePackageFilters.ts index 063a6eb..d8cb7d1 100644 --- a/frontend/src/composables/Packages/usePackageFilters.ts +++ b/frontend/src/composables/Packages/usePackageFilters.ts @@ -87,25 +87,68 @@ export function usePackageFilters() { } const initFromUrl = () => { - const urlParams = new URLSearchParams(window.location.search) + try { + const urlParams = new URLSearchParams(window.location.search) - filterOptions.value = { - pkgbaseSearch: urlParams.get('pkgbase') || '', - page: urlParams.has('page') ? parseInt(urlParams.get('page') || '1') : 1, - exactSearch: urlParams.has('exact'), - statuses: urlParams.getAll('status').map((status) => ({ - title: status.toUpperCase(), - value: status - })), - repo: undefined + const defaultOptions: FilterOptions = { + pkgbaseSearch: '', + page: 1, + exactSearch: false, + statuses: [], + repo: undefined + } + + let page = 1 + if (urlParams.has('page')) { + const pageParam = urlParams.get('page') || '1' + const parsedPage = parseInt(pageParam, 10) + page = !isNaN(parsedPage) && parsedPage > 0 ? parsedPage : 1 + } + + const statuses = urlParams + .getAll('status') + .filter((status) => typeof status === 'string' && status.trim() !== '') + .map((status) => ({ + title: status.toUpperCase(), + value: status + })) + + filterOptions.value = { + ...defaultOptions, + pkgbaseSearch: urlParams.get('pkgbase') || '', + page, + exactSearch: urlParams.has('exact'), + statuses + } + + const repoValue = urlParams.get('repo') + if (repoValue && typeof repoValue === 'string' && repoValue.trim() !== '') { + filterOptions.value.repo = { title: repoValue, value: repoValue } + } + + applyFilters() + + // Check if page exceeds total pages and adjust if necessary + if ( + filterOptions.value.page > filterResult.value.totalPages && + filterResult.value.totalPages > 0 + ) { + filterOptions.value.page = filterResult.value.totalPages + applyFilters() + updateUrlParams() + } + } catch (error) { + console.error('Error parsing URL parameters:', error) + // Reset to default state + filterOptions.value = { + pkgbaseSearch: '', + page: 1, + exactSearch: false, + statuses: [], + repo: undefined + } + applyFilters() } - - const repoValue = urlParams.get('repo') - if (repoValue) { - filterOptions.value.repo = { title: repoValue, value: repoValue } - } - - applyFilters() } const updateUrlParams = () => {