Skip to content

Commit

Permalink
fix: search
Browse files Browse the repository at this point in the history
  • Loading branch information
Majorfi committed Nov 28, 2023
1 parent d38c4dd commit 6bef916
Showing 1 changed file with 43 additions and 5 deletions.
48 changes: 43 additions & 5 deletions apps/vaults/hooks/useVaultsQueryArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function useQueryArguments({defaultCategories}: {defaultCategories?: string[]}):
const router = useRouter();
const [search, set_search] = useState<string | null>(null);
const [categories, set_categories] = useState<string[] | null>(defaultCategories || []);
const [chains, set_chains] = useState<number[] | null>(allChains);
const [chains, set_chains] = useState<number[] | null>(allChains || []);
const [sortDirection, set_sortDirection] = useState<string | null>(null);
const [sortBy, set_sortBy] = useState<string | null>(null);

Expand All @@ -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 || []);
Expand All @@ -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);
Expand Down Expand Up @@ -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 => {
Expand All @@ -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});
},
Expand All @@ -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});
},
Expand Down

0 comments on commit 6bef916

Please sign in to comment.