Commit 5f431a1e authored by cartcrom's avatar cartcrom Committed by GitHub

feat: show wrapped native asset at top of token selector (#4967)

* finished feature

* simplified logic from pr comments

* added checking for disableNonToken back in
parent bf13b4a9
......@@ -104,20 +104,17 @@ export function CurrencySearch({
const filteredSortedTokens = useSortTokensByQuery(debouncedQuery, sortedTokens)
const native = useNativeCurrency()
const wrapped = native.wrapped
const filteredSortedTokensWithETH: Currency[] = useMemo(() => {
// Use Celo ERC20 Implementation and exclude the native asset
if (!native) {
return filteredSortedTokens
}
const searchCurrencies: Currency[] = useMemo(() => {
const s = debouncedQuery.toLowerCase().trim()
if (native.symbol?.toLowerCase()?.indexOf(s) !== -1) {
// Always bump the native token to the top of the list.
return [native, ...filteredSortedTokens.filter((t) => !t.equals(native))]
}
return filteredSortedTokens
}, [debouncedQuery, native, filteredSortedTokens])
const tokens = filteredSortedTokens.filter((t) => !(t.equals(wrapped) || (disableNonToken && t.isNative)))
const natives = (disableNonToken || native.equals(wrapped) ? [wrapped] : [native, wrapped]).filter(
(n) => n.symbol?.toLowerCase()?.indexOf(s) !== -1 || n.name?.toLowerCase()?.indexOf(s) !== -1
)
return [...natives, ...tokens]
}, [debouncedQuery, filteredSortedTokens, wrapped, disableNonToken, native])
const handleCurrencySelect = useCallback(
(currency: Currency, hasWarning?: boolean) => {
......@@ -147,17 +144,17 @@ export function CurrencySearch({
const s = debouncedQuery.toLowerCase().trim()
if (s === native?.symbol?.toLowerCase()) {
handleCurrencySelect(native)
} else if (filteredSortedTokensWithETH.length > 0) {
} else if (searchCurrencies.length > 0) {
if (
filteredSortedTokensWithETH[0].symbol?.toLowerCase() === debouncedQuery.trim().toLowerCase() ||
filteredSortedTokensWithETH.length === 1
searchCurrencies[0].symbol?.toLowerCase() === debouncedQuery.trim().toLowerCase() ||
searchCurrencies.length === 1
) {
handleCurrencySelect(filteredSortedTokensWithETH[0])
handleCurrencySelect(searchCurrencies[0])
}
}
}
},
[debouncedQuery, native, filteredSortedTokensWithETH, handleCurrencySelect]
[debouncedQuery, native, searchCurrencies, handleCurrencySelect]
)
// menu ui
......@@ -229,13 +226,13 @@ export function CurrencySearch({
)}
/>
</Column>
) : filteredSortedTokens?.length > 0 || filteredInactiveTokens?.length > 0 || isLoading ? (
) : searchCurrencies?.length > 0 || filteredInactiveTokens?.length > 0 || isLoading ? (
<div style={{ flex: '1' }}>
<AutoSizer disableWidth>
{({ height }) => (
<CurrencyList
height={height}
currencies={disableNonToken ? filteredSortedTokens : filteredSortedTokensWithETH}
currencies={searchCurrencies}
otherListTokens={filteredInactiveTokens}
onCurrencySelect={handleCurrencySelect}
otherCurrency={otherSelectedCurrency}
......
......@@ -51,10 +51,12 @@ export function useSortTokensByQuery<T extends Token | TokenInfo>(query: string,
const rest: T[] = []
// sort tokens by exact match -> subtring on symbol match -> rest
const trimmedQuery = query.toLowerCase().trim()
tokens.map((token) => {
if (token.symbol?.toLowerCase() === matches[0]) {
const symbol = token.symbol?.toLowerCase()
if (symbol === matches[0]) {
return exactMatches.push(token)
} else if (token.symbol?.toLowerCase().startsWith(query.toLowerCase().trim())) {
} else if (symbol?.startsWith(trimmedQuery)) {
return symbolSubtrings.push(token)
} else {
return rest.push(token)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment