handle numeric values in useQueryParams (closes #25)

This commit is contained in:
ajvpot
2025-09-19 17:34:12 +02:00
parent f34836763c
commit a1416bcc05
2 changed files with 8 additions and 6 deletions
+4 -4
View File
@@ -38,7 +38,7 @@ export interface SearchQuery {
const searchBatcher = create({
fetcher: async (queries: SearchQuery[]) => {
const normalizedQueries = queries.map(q => ({
query: q.query?.trim() || "",
query: (typeof q.query === 'string' ? q.query.trim() : String(q.query || '').trim()) || "",
region: q.region || undefined,
lastSeen: q.lastSeen !== null && q.lastSeen !== undefined ? q.lastSeen : undefined,
limit: q.limit || 50,
@@ -101,7 +101,7 @@ export function useMeshcoreSearch({
enabled = true
}: UseMeshcoreSearchParams) {
// Stabilize the search query object to prevent unnecessary re-renders
const trimmedQuery = query.trim();
const trimmedQuery = typeof query === 'string' ? query.trim() : String(query || '').trim();
const searchQuery: SearchQuery = useMemo(() => ({
query: trimmedQuery,
region,
@@ -134,7 +134,7 @@ export function useMeshcoreSearch({
signal?.removeEventListener('abort', handleAbort);
}
},
enabled: enabled && query.trim().length > 0,
enabled: enabled && (typeof query === 'string' ? query.trim() : String(query || '').trim()).length > 0,
staleTime: 1000, // Reduce stale time to be more responsive to typing
gcTime: 30 * 1000, // Reduce garbage collection time
retry: 1,
@@ -169,7 +169,7 @@ export function useMeshcoreSearches({ searches }: UseMeshcoreSearchesParams) {
enabled = true
} = searchParams;
const trimmedQuery = query.trim();
const trimmedQuery = typeof query === 'string' ? query.trim() : String(query || '').trim();
const searchQuery: SearchQuery = {
query: trimmedQuery,
region,
+4 -2
View File
@@ -13,7 +13,8 @@ export function useQueryParams<T extends Record<string, any>>(defaultValues: T =
if (typeof window !== 'undefined') {
const urlParams = new URLSearchParams(window.location.search);
urlParams.forEach((value, key) => {
if (!isNaN(Number(value)) && value !== '') {
// Don't auto-convert 'q' (query) parameter to number since it should always be a string
if (key !== 'q' && !isNaN(Number(value)) && value !== '') {
result[key as keyof T] = Number(value) as T[keyof T];
} else {
result[key as keyof T] = value as T[keyof T];
@@ -33,7 +34,8 @@ export function useQueryParams<T extends Record<string, any>>(defaultValues: T =
const newState = { ...defaultValues };
searchParams.forEach((value, key) => {
if (!isNaN(Number(value)) && value !== '') {
// Don't auto-convert 'q' (query) parameter to number since it should always be a string
if (key !== 'q' && !isNaN(Number(value)) && value !== '') {
newState[key as keyof T] = Number(value) as T[keyof T];
} else {
newState[key as keyof T] = value as T[keyof T];