UNPKG

@lifi/widget

Version:

LI.FI Widget for cross-chain bridging and swapping. It will drive your multi-chain strategy and attract new users from everywhere.

46 lines 2.17 kB
import { getToken, } from '@lifi/sdk'; import { useQuery, useQueryClient } from '@tanstack/react-query'; import { useWidgetConfig } from '../providers/WidgetProvider/WidgetProvider.js'; import { getConfigItemSets, isFormItemAllowed } from '../utils/item.js'; import { getQueryKey } from '../utils/queries.js'; export const useTokenSearch = (chainId, tokenQuery, enabled, formType) => { const queryClient = useQueryClient(); const { tokens: configTokens, keyPrefix } = useWidgetConfig(); const { data, isLoading } = useQuery({ queryKey: [getQueryKey('token-search', keyPrefix), chainId, tokenQuery], queryFn: async ({ queryKey: [, chainId, tokenQuery], signal }) => { const token = await getToken(chainId, tokenQuery, { signal, }); if (token) { // Filter config tokens by chain before checking if token is allowed const filteredConfigTokens = getConfigItemSets(configTokens, (tokens) => new Set(tokens .filter((t) => t.chainId === token.chainId) .map((t) => t.address)), formType); // Return undefined if the token is denied if (!isFormItemAllowed(token, filteredConfigTokens, formType, (t) => t.address)) { return undefined; } queryClient.setQueriesData({ queryKey: [getQueryKey('tokens', keyPrefix)] }, (data) => { if (data && !data.tokens[chainId]?.some((t) => t.address === token.address)) { const clonedData = { ...data, tokens: { ...data.tokens } }; clonedData.tokens[chainId] = [ ...(clonedData.tokens[chainId] ?? []), token, ]; return clonedData; } }); } return token; }, enabled: Boolean(chainId && tokenQuery && enabled), retry: false, }); return { token: data, isLoading, }; }; //# sourceMappingURL=useTokenSearch.js.map