Skip to content

Commit

Permalink
Support only persisting when search query is undefined (the default l…
Browse files Browse the repository at this point in the history
…ists) (#6335)
  • Loading branch information
jinchung authored Dec 20, 2024
1 parent 7b644f3 commit b86910e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/__swaps__/screens/Swap/hooks/useSearchCurrencyLists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ export function useSearchCurrencyLists() {
const { data: verifiedAssets, isLoading: isLoadingVerifiedAssets } = useTokenSearch(
{
list: 'verifiedAssets',
chainId: isAddress(query) ? state.toChainId : undefined,
chainId: state.toChainId,
keys: isAddress(query) ? ['address'] : ['name', 'symbol'],
threshold: isAddress(query) ? 'CASE_SENSITIVE_EQUAL' : 'CONTAINS',
query: query.length > 0 ? query : undefined,
Expand Down Expand Up @@ -417,7 +417,7 @@ export function useSearchCurrencyLists() {
{
enabled: memoizedData.enableUnverifiedSearch,
select: (data: TokenSearchResult) => {
return getExactMatches(data, query).slice(0, MAX_UNVERIFIED_RESULTS);
return isAddress(query) ? getExactMatches(data, query).slice(0, MAX_UNVERIFIED_RESULTS) : data.slice(0, MAX_UNVERIFIED_RESULTS);
},
}
);
Expand Down
2 changes: 1 addition & 1 deletion src/__swaps__/screens/Swap/resources/search/discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useQuery } from '@tanstack/react-query';
import { parseTokenSearch } from './utils';

const tokenSearchHttp = new RainbowFetchClient({
baseURL: 'https://token-search.rainbow.me/v3/discovery',
baseURL: 'https://token-search.rainbow.me/v3/trending/swaps',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
Expand Down
19 changes: 14 additions & 5 deletions src/__swaps__/screens/Swap/resources/search/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { parseTokenSearch } from './utils';
const ALL_VERIFIED_TOKENS_PARAM = '/?list=verifiedAssets';

const tokenSearchHttp = new RainbowFetchClient({
baseURL: 'https://token-search.rainbow.me/v2',
baseURL: 'https://token-search.rainbow.me/v3/tokens',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
Expand All @@ -30,13 +30,19 @@ export type TokenSearchArgs = {
list: TokenSearchListId;
threshold?: TokenSearchThreshold;
query?: string;
shouldPersist?: boolean;
};

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

const tokenSearchQueryKey = ({ chainId, fromChainId, keys, list, threshold, query }: TokenSearchArgs) =>
createQueryKey('TokenSearch', { chainId, fromChainId, keys, list, threshold, query }, { persisterVersion: 2 });
const tokenSearchQueryKey = ({ chainId, fromChainId, keys, list, threshold, query, shouldPersist }: TokenSearchArgs) => {
return createQueryKey(
'TokenSearch',
{ chainId, fromChainId, keys, list, threshold, query },
{ persisterVersion: shouldPersist ? 3 : undefined }
);
};

type TokenSearchQueryKey = ReturnType<typeof tokenSearchQueryKey>;

Expand Down Expand Up @@ -77,6 +83,7 @@ async function tokenSearchQueryFunction({
return parseTokenSearch(tokenSearch.data.data, chainId);
}

// search for address on other chains
const allVerifiedTokens = await tokenSearchHttp.get<{ data: SearchAsset[] }>(ALL_VERIFIED_TOKENS_PARAM);

const addressQuery = query.trim().toLowerCase();
Expand Down Expand Up @@ -104,8 +111,9 @@ export async function fetchTokenSearch(
{ chainId, fromChainId, keys, list, threshold, query }: TokenSearchArgs,
config: QueryConfigWithSelect<TokenSearchResult, Error, TokenSearchResult, TokenSearchQueryKey> = {}
) {
const shouldPersist = query === undefined;
return await queryClient.fetchQuery(
tokenSearchQueryKey({ chainId, fromChainId, keys, list, threshold, query }),
tokenSearchQueryKey({ chainId, fromChainId, keys, list, threshold, query, shouldPersist }),
tokenSearchQueryFunction,
config
);
Expand All @@ -130,7 +138,8 @@ export function useTokenSearch(
{ chainId, fromChainId, keys, list, threshold, query }: TokenSearchArgs,
config: QueryConfigWithSelect<TokenSearchResult, Error, TokenSearchResult, TokenSearchQueryKey> = {}
) {
return useQuery(tokenSearchQueryKey({ chainId, fromChainId, keys, list, threshold, query }), tokenSearchQueryFunction, {
const shouldPersist = query === undefined;
return useQuery(tokenSearchQueryKey({ chainId, fromChainId, keys, list, threshold, query, shouldPersist }), tokenSearchQueryFunction, {
...config,
keepPreviousData: true,
});
Expand Down

0 comments on commit b86910e

Please sign in to comment.