From 35ab3960d943f82f13622f4eee911da7caabc6e0 Mon Sep 17 00:00:00 2001 From: jinchung Date: Mon, 30 Dec 2024 11:51:31 -0500 Subject: [PATCH] Add separate query key and function for all networks request to simplify for consolidation later --- .../screens/Swap/resources/search/search.ts | 47 +++++++++++++++++-- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/src/__swaps__/screens/Swap/resources/search/search.ts b/src/__swaps__/screens/Swap/resources/search/search.ts index e715bffe0bd..e41d96abed2 100644 --- a/src/__swaps__/screens/Swap/resources/search/search.ts +++ b/src/__swaps__/screens/Swap/resources/search/search.ts @@ -39,6 +39,11 @@ export type TokenSearchArgs = { shouldPersist?: boolean; }; +export type TokenSearchAllNetworksArgs = { + query?: string; + shouldPersist?: boolean; +}; + // /////////////////////////////////////////////// // Query Key @@ -50,8 +55,14 @@ const tokenSearchQueryKey = ({ chainId, fromChainId, keys, list, threshold, quer ); }; +const tokenSearchAllNetworksQueryKey = ({ query, shouldPersist }: TokenSearchAllNetworksArgs) => { + return createQueryKey('TokenSearch', { query }, { persisterVersion: shouldPersist ? 3 : undefined }); +}; + type TokenSearchQueryKey = ReturnType; +type TokenSearchAllNetworksQueryKey = ReturnType; + // /////////////////////////////////////////////// // Query Function const getImportedAsset = async (searchQuery: string, chainId: number = ChainId.mainnet): Promise => { @@ -149,6 +160,36 @@ async function tokenSearchQueryFunction({ } } +async function tokenSearchQueryFunctionAllNetworks({ queryKey: [{ query }] }: QueryFunctionArgs) { + const queryParams: { + query?: string; + } = { + query, + }; + + const isAddressSearch = query && isAddress(query); + + const url = `/?${qs.stringify(queryParams)}`; + + try { + if (isAddressSearch) { + const tokenSearch = await tokenSearchHttp.get<{ data: SearchAsset[] }>(url); + + if (tokenSearch && tokenSearch.data.data.length > 0) { + return parseTokenSearch(tokenSearch.data.data); + } + + return []; + } else { + const tokenSearch = await tokenSearchHttp.get<{ data: SearchAsset[] }>(url); + return parseTokenSearch(tokenSearch.data.data); + } + } catch (e) { + logger.error(new RainbowError('[tokenSearchQueryFunction]: Token search failed'), { url }); + return []; + } +} + export type TokenSearchResult = QueryFunctionResult; // /////////////////////////////////////////////// @@ -193,10 +234,10 @@ export function useTokenSearch( } export function useTokenSearchAllNetworks( - { query }: Omit, - config: QueryConfigWithSelect = {} + { query }: TokenSearchAllNetworksArgs, + config: QueryConfigWithSelect = {} ) { - return useQuery(tokenSearchQueryKey({ query }), tokenSearchQueryFunction, { + return useQuery(tokenSearchAllNetworksQueryKey({ query }), tokenSearchQueryFunctionAllNetworks, { ...config, keepPreviousData: true, });