Skip to content

Commit

Permalink
feat: pass in custom filteredTokensGenerator function to asset-picker
Browse files Browse the repository at this point in the history
  • Loading branch information
micaelae committed Aug 21, 2024
1 parent 7d017e9 commit 13275a9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ type AssetPickerModalProps = {
*/
sendingAsset?: { image: string; symbol: string } | undefined;
onNetworkPickerClick?: () => void;
/**
* Generator function that returns a list of tokens filtered by a predicate and sorted
* by a custom order.
*/
customTokenListGenerator?: (
filterPredicate: (token: ERC20Asset | NativeAsset) => boolean,
) => Generator<
AssetWithDisplayData<NativeAsset> | AssetWithDisplayData<ERC20Asset>
>;
} & Pick<
React.ComponentProps<typeof AssetPickerModalTabs>,
'visibleTabs' | 'defaultActiveTabKey'
Expand All @@ -94,6 +103,7 @@ export function AssetPickerModal({
sendingAsset,
network,
onNetworkPickerClick,
customTokenListGenerator,
...tabProps
}: AssetPickerModalProps) {
const t = useI18nContext();
Expand Down Expand Up @@ -222,10 +232,31 @@ export function AssetPickerModal({
);

const filteredTokenList = useMemo(() => {
const filteredTokens: AssetWithDisplayData<ERC20Asset | NativeAsset>[] = [];
const filteredTokens: (
| AssetWithDisplayData<ERC20Asset>
| AssetWithDisplayData<NativeAsset>
)[] = [];
// undefined would be the native token address
const filteredTokensAddresses = new Set<string | undefined>();

// If filteredTokensGenerator is passed in, use it to generate the filtered tokens
if (customTokenListGenerator) {
for (const token of customTokenListGenerator(
({ symbol, address }) =>
symbol?.toLowerCase().includes(searchQuery.toLowerCase()) &&
!filteredTokensAddresses.has(address?.toLowerCase()),
)) {
filteredTokensAddresses.add(token.address?.toLowerCase());
filteredTokens.push(token);

if (filteredTokens.length > MAX_UNOWNED_TOKENS_RENDERED) {
break;
}
}
return filteredTokens;
}

// Otherwise use the default token list generator
for (const token of sortedTokenListGenerator()) {
if (
token.symbol?.toLowerCase().includes(searchQuery.toLowerCase()) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export type AssetPickerProps = {
>;
} & Pick<
React.ComponentProps<typeof AssetPickerModal>,
'visibleTabs' | 'header' | 'sendingAsset'
'visibleTabs' | 'header' | 'sendingAsset' | 'customTokenListGenerator'
>;

// A component that lets the user pick from a list of assets.
Expand All @@ -82,6 +82,7 @@ export function AssetPicker({
onClick,
isDisabled = false,
visibleTabs,
customTokenListGenerator,
}: AssetPickerProps) {
///: BEGIN:ONLY_INCLUDE_IF(build-main,build-beta,build-flask)
const t = useI18nContext();
Expand Down Expand Up @@ -156,6 +157,7 @@ export function AssetPicker({
defaultActiveTabKey={
asset?.type === AssetType.NFT ? TabName.NFTS : TabName.TOKENS
}
customTokenListGenerator={customTokenListGenerator}
/>

<Button
Expand Down

0 comments on commit 13275a9

Please sign in to comment.