diff --git a/apps/vaults/hooks/useVaultsQueryArgs.ts b/apps/vaults/hooks/useVaultsQueryArgs.ts index 2c619f4df..25f4030da 100644 --- a/apps/vaults/hooks/useVaultsQueryArgs.ts +++ b/apps/vaults/hooks/useVaultsQueryArgs.ts @@ -25,7 +25,7 @@ function useQueryArguments({defaultCategories}: {defaultCategories?: string[]}): const router = useRouter(); const [search, set_search] = useState(null); const [categories, set_categories] = useState(defaultCategories || []); - const [chains, set_chains] = useState(allChains); + const [chains, set_chains] = useState(allChains || []); const [sortDirection, set_sortDirection] = useState(null); const [sortBy, set_sortBy] = useState(null); @@ -46,6 +46,17 @@ function useQueryArguments({defaultCategories}: {defaultCategories?: string[]}): set_categories(defaultCategories || []); return; } + if (categoriesParamArray.length === defaultCategories?.length) { + const isEqual = categoriesParamArray.every((c): boolean => defaultCategories?.includes(c)); + if (isEqual) { + set_categories(defaultCategories); + return; + } + } + if (categoriesParamArray[0] === 'none') { + set_categories([]); + return; + } set_categories(categoriesParamArray); } else { set_categories(defaultCategories || []); @@ -58,6 +69,17 @@ function useQueryArguments({defaultCategories}: {defaultCategories?: string[]}): set_chains(allChains); return; } + if (chainsParamArray.length === allChains.length) { + const isEqual = chainsParamArray.every((c): boolean => allChains.includes(Number(c))); + if (isEqual) { + set_chains(allChains); + return; + } + } + if (chainsParamArray[0] === '0') { + set_chains([]); + return; + } set_chains(chainsParamArray.map((chain): number => Number(chain))); } else { set_chains(allChains); @@ -93,8 +115,8 @@ function useQueryArguments({defaultCategories}: {defaultCategories?: string[]}): return { search, - categories: (categories || defaultCategories || []) as string[], - chains: (chains || allChains) as number[], + categories: (categories || []) as string[], + chains: (chains || []) as number[], sortDirection: (sortDirection || 'desc') as TSortDirection, sortBy: (sortBy || 'featuringScore') as TPossibleSortBy, onSearch: (value): void => { @@ -118,10 +140,18 @@ function useQueryArguments({defaultCategories}: {defaultCategories?: string[]}): set_categories(value); const currentURL = new URL(window.location.href); if (value.length === 0) { - currentURL.searchParams.delete('categories'); + currentURL.searchParams.set('categories', 'none'); router.replace(currentURL, {search: currentURL.search}, {shallow: true}); return; } + if (value.length === defaultCategories?.length) { + const isEqual = value.every((category): boolean => defaultCategories?.includes(category)); + if (isEqual) { + currentURL.searchParams.delete('categories'); + router.replace(currentURL, {search: currentURL.search}, {shallow: true}); + return; + } + } currentURL.searchParams.set('categories', value.join('_')); router.replace(currentURL, {search: currentURL.search}, {shallow: true}); }, @@ -135,10 +165,18 @@ function useQueryArguments({defaultCategories}: {defaultCategories?: string[]}): set_chains(value); const currentURL = new URL(window.location.href); if (value.length === 0) { - currentURL.searchParams.delete('chains'); + currentURL.searchParams.set('chains', '0'); router.replace(currentURL, {search: currentURL.search}, {shallow: true}); return; } + if (value.length === allChains.length) { + const isEqual = value.every((chain): boolean => allChains.includes(chain)); + if (isEqual) { + currentURL.searchParams.delete('chains'); + router.replace(currentURL, {search: currentURL.search}, {shallow: true}); + return; + } + } currentURL.searchParams.set('chains', value.join('_')); router.replace(currentURL, {search: currentURL.search}, {shallow: true}); },