Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(token-search): fix token search results #3607

Merged
merged 4 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,15 @@ export function TokenSearchResults({
return searchCount === 0
}, [isLoading, searchCount])

const [matchedToken, activeList] = useMemo(() => {
let matched: TokenWithLogo | undefined = undefined
const [matchedTokens, activeList] = useMemo(() => {
const matched: TokenWithLogo[] = []
const remaining: TokenWithLogo[] = []

for (const t of activeListsResult) {
if (doesTokenMatchSymbolOrAddress(t, searchInput)) {
matched = t
// There should ever be only 1 token with a given address
// There can be multiple with the same symbol
matched.push(t)
Comment on lines -64 to +66
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix for missing entries.
Now all matches are collected.

} else {
remaining.push(t)
}
Expand All @@ -70,16 +72,14 @@ export function TokenSearchResults({
return [matched, remaining]
}, [activeListsResult, searchInput])

const matchedTokenAddress = matchedToken?.address.toLowerCase()

// On press Enter, select first token if only one token is found or it's fully matches to the search input
// On press Enter, select first token if only one token is found or it fully matches to the search input
const onInputPressEnter = useCallback(() => {
if (!searchInput || !activeListsResult) return

if (activeListsResult.length === 1 || matchedToken) {
onSelectToken(matchedToken || activeListsResult[0])
if (activeListsResult.length === 1 || matchedTokens.length === 1) {
onSelectToken(matchedTokens[0] || activeListsResult[0])
}
}, [searchInput, activeListsResult, matchedToken, onSelectToken])
}, [searchInput, activeListsResult, matchedTokens, onSelectToken])

useEffect(() => {
updateSelectTokenWidget({
Expand All @@ -101,34 +101,22 @@ export function TokenSearchResults({

return (
<>
{/*Exact match*/}
{matchedToken && matchedTokenAddress && (
<TokenListItem
token={matchedToken}
balance={balances ? balances[matchedTokenAddress] : undefined}
onSelectToken={onSelectToken}
selectedToken={selectedToken}
isUnsupported={!!unsupportedTokens[matchedTokenAddress]}
isPermitCompatible={permitCompatibleTokens[matchedTokenAddress]}
/>
)}
{/*Tokens from active lists*/}
{activeList &&
activeList.map((token) => {
const addressLowerCase = token.address.toLowerCase()

return (
<TokenListItem
key={token.address}
isUnsupported={!!unsupportedTokens[addressLowerCase]}
isPermitCompatible={permitCompatibleTokens[addressLowerCase]}
selectedToken={selectedToken}
token={token}
balance={balances ? balances[token.address.toLowerCase()] : undefined}
onSelectToken={onSelectToken}
/>
)
})}
{/*Matched tokens first, followed by tokens from active lists*/}
{matchedTokens.concat(activeList).map((token) => {
const addressLowerCase = token.address.toLowerCase()

return (
<TokenListItem
key={token.address}
isUnsupported={!!unsupportedTokens[addressLowerCase]}
isPermitCompatible={permitCompatibleTokens[addressLowerCase]}
selectedToken={selectedToken}
token={token}
balance={balances ? balances[token.address.toLowerCase()] : undefined}
onSelectToken={onSelectToken}
/>
)
})}

{/*Tokens from blockchain*/}
{blockchainResult?.length ? (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export function FavouriteTokensList(props: FavouriteTokensListProps) {
return (
<div>
<styledEl.Header>
<h4>Favourite tokens</h4>
<InfoIcon iconType="help" content="Your favourite saved tokens. Edit this list in your account page." />
<h4>Favorite tokens</h4>
<InfoIcon iconType="help" content="Your favorite saved tokens. Edit this list in your account page." />
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional request by Alex to update the spelling.

</styledEl.Header>
<styledEl.List>
{tokens.map((token) => {
Expand Down
2 changes: 1 addition & 1 deletion libs/tokens/src/utils/tokenMapToListWithLogo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import { TokensMap } from '../types'
*/
export function tokenMapToListWithLogo(tokenMap: TokensMap): TokenWithLogo[] {
return Object.values(tokenMap)
.sort((a, b) => (a.symbol > b.symbol ? 1 : -1))
.sort((a, b) => a.symbol.localeCompare(b.symbol))
Copy link
Contributor

@anxolin anxolin Jan 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

funny, i just came to suggest this (use localeCompare), and I saw it was done moments ago!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.map((token) => TokenWithLogo.fromToken(token, token.logoURI))
}
Loading