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.
This commit is contained in:
2025-04-14 21:58:54 +02:00
parent c458b564ce
commit ad5ce609fc

View File

@@ -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 = () => {