Skip to content

Commit

Permalink
Add separate query key and function for all networks request to simpl…
Browse files Browse the repository at this point in the history
…ify for consolidation later
  • Loading branch information
jinchung committed Dec 30, 2024
1 parent 5c8376e commit 35ab396
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 deletions src/__swaps__/screens/Swap/resources/search/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ export type TokenSearchArgs = {
shouldPersist?: boolean;
};

export type TokenSearchAllNetworksArgs = {
query?: string;
shouldPersist?: boolean;
};

// ///////////////////////////////////////////////
// Query Key

Expand All @@ -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<typeof tokenSearchQueryKey>;

type TokenSearchAllNetworksQueryKey = ReturnType<typeof tokenSearchAllNetworksQueryKey>;

// ///////////////////////////////////////////////
// Query Function
const getImportedAsset = async (searchQuery: string, chainId: number = ChainId.mainnet): Promise<RainbowToken[] | null> => {
Expand Down Expand Up @@ -149,6 +160,36 @@ async function tokenSearchQueryFunction({
}
}

async function tokenSearchQueryFunctionAllNetworks({ queryKey: [{ query }] }: QueryFunctionArgs<typeof tokenSearchAllNetworksQueryKey>) {
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<typeof tokenSearchQueryFunction>;

// ///////////////////////////////////////////////
Expand Down Expand Up @@ -193,10 +234,10 @@ export function useTokenSearch(
}

export function useTokenSearchAllNetworks(
{ query }: Omit<TokenSearchArgs, 'list' | 'chainId' | 'fromChainId'>,
config: QueryConfigWithSelect<TokenSearchResult, Error, TokenSearchResult, TokenSearchQueryKey> = {}
{ query }: TokenSearchAllNetworksArgs,
config: QueryConfigWithSelect<TokenSearchResult, Error, TokenSearchResult, TokenSearchAllNetworksQueryKey> = {}
) {
return useQuery(tokenSearchQueryKey({ query }), tokenSearchQueryFunction, {
return useQuery(tokenSearchAllNetworksQueryKey({ query }), tokenSearchQueryFunctionAllNetworks, {
...config,
keepPreviousData: true,
});
Expand Down

0 comments on commit 35ab396

Please sign in to comment.