Skip to content

Commit

Permalink
Merge pull request #172 from bnb-chain/wenty/search1206
Browse files Browse the repository at this point in the history
feat: Fix token list sort issue
  • Loading branch information
wenty22 authored Dec 6, 2024
2 parents d5eac0c + 34d21ec commit 54cf211
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function IconImage(props: IconImageProps) {
className="default-icon"
size={boxSize}
{...restProps}
bg={fallbackBgColor ?? theme.colors[colorMode].support.primary[1]}
bg={fallbackBgColor ?? theme.colors[colorMode].support.primary[4]}
/>
) : (
<Image
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,18 @@ export function ChooseTokenModal(props: ChooseTokenModalProps) {
toChainId: toChain?.id,
});

const { isNoResult, result, onSearch } = useSearch({
const { isNoResult, result, keyword, onSearch } = useSearch({
data: tokens,
filter: (item, keyword) => {
return (
item.displaySymbol?.toLowerCase().includes(keyword) ||
item.name?.toLowerCase().includes(keyword) ||
item.displaySymbol?.toLowerCase().includes(keyword?.toLowerCase()) ||
item.name?.toLowerCase().includes(keyword?.toLowerCase()) ||
isSameAddress(item.address, keyword)
);
},
});

const { isLoading, data } = useTokenList(result);
const { isLoading, data } = useTokenList(result, keyword);

const evmAccount = useAccount();
const tronAccount = useTronAccount();
Expand Down Expand Up @@ -109,7 +109,7 @@ export function ChooseTokenModal(props: ChooseTokenModalProps) {
iconUrl={item.icon}
isActive={isActive}
isDisabled={isDisabled}
data-address={item.address?.toLowerCase()}
data-address={item.address}
incompatibleTooltip={formatMessage({
id: 'select-modal.token.incompatible.tooltip',
})}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function useSearch<T>(props: UseSearchProps<T>) {
};

const result = useMemo(() => {
return data.filter((item) => filter(item, keyword.toLowerCase())).sort(sorter);
return data.filter((item) => filter(item, keyword)).sort(sorter);
}, [data, filter, keyword, sorter]);

const isNoResult = keyword.length > 0 && !result.length;
Expand All @@ -24,5 +24,6 @@ export function useSearch<T>(props: UseSearchProps<T>) {
onSearch,
isNoResult,
result,
keyword,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { sortTokens } from '@/modules/aggregator/shared/sortTokens';
import { useAggregator } from '@/modules/aggregator/components/AggregatorProvider';
import { isChainOrTokenCompatible } from '@/modules/aggregator/shared/isChainOrTokenCompatible';

export function useTokenList(tokens: IBridgeToken[] = []) {
export function useTokenList(tokens: IBridgeToken[] = [], keyword?: string) {
const selectedToken = useAppSelector((state) => state.transfer.selectedToken);
const isLoadingTokenBalances = useAppSelector((state) => state.aggregator.isLoadingTokenBalances);
const isLoadingTokenPrices = useAppSelector((state) => state.aggregator.isLoadingTokenPrices);
Expand Down Expand Up @@ -38,19 +38,37 @@ export function useTokenList(tokens: IBridgeToken[] = []) {
const sortedTokens = sortTokens({
tokens: tmpTokens,
orders: transferConfig.order?.tokens,
}).sort((a) => {
return isSameAddress(a.address, selectedToken?.address) && isChainOrTokenCompatible(a)
? -1
: 0;
});
})
.sort((a, b) => {
if (keyword) {
const isA = isChainOrTokenCompatible(a);
const isB = isChainOrTokenCompatible(b);
const findA = a.displaySymbol?.toLowerCase()?.includes(keyword?.toLowerCase());
const findB = b.displaySymbol?.toLowerCase()?.includes(keyword?.toLowerCase());

if (findA && isA) {
return -1;
}
if (findB && isB) {
return 1;
}
}
return 0;
})
.sort((a) => {
return isSameAddress(a.address, selectedToken?.address) && isChainOrTokenCompatible(a)
? -1
: 0;
});

return sortedTokens;
}, [
tokens,
transferConfig.order?.tokens,
getTokenBalance,
getTokenPrice,
selectedToken?.address,
tokens,
keyword,
]);

return { data: sortedTokens, isLoading: isLoadingTokenBalances || isLoadingTokenPrices };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const RouteName = React.memo(
w={['16px', '16px', '16px', '20px']}
h={['16px', '16px', '16px', '20px']}
borderRadius={'100%'}
mb={['4px', '4px', '4px', '0']}
mb={['0', '0', '4px', '0']}
/>
) : bridgeType === 'stargate' ? (
<StarGateLogo w={['16px', '16px', '16px', '20px']} h={['16px', '16px', '16px', '20px']} />
Expand All @@ -63,7 +63,7 @@ export const RouteName = React.memo(
w={['16px', '16px', '16px', '20px']}
h={['16px', '16px', '16px', '20px']}
borderRadius={'100%'}
mb={['4px', '4px', '4px', '0']}
mb={['0', '0', '4px', '0']}
/>
) : null}

Expand Down

0 comments on commit 54cf211

Please sign in to comment.