Skip to content

Commit

Permalink
sort tokens to buy
Browse files Browse the repository at this point in the history
  • Loading branch information
SorinC6 committed Oct 25, 2023
1 parent bbd5783 commit fe70ec9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
26 changes: 26 additions & 0 deletions apps/wallet-mobile/src/features/Swap/common/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {createTypeGuardFromSchema, parseSafe} from '@yoroi/common'
import {Balance} from '@yoroi/types'
import {SwapApi} from '@yoroi/types/src/swap/api'
import {useMutation, UseMutationOptions} from 'react-query'
import {z} from 'zod'
Expand Down Expand Up @@ -65,3 +66,28 @@ export const parseOrderTxMetadata = (metadataJson: string): OrderTxMetadata | nu
const parsedMetadata = parseSafe(metadataJson)
return isOrderTxMetadata(parsedMetadata) ? parsedMetadata : null
}

function containsOnlyValidChars(str?: string): boolean {
const validCharsRegex = /^[a-zA-Z0]*$/
return typeof str === 'string' && validCharsRegex.test(str)
}

export const sortTokensByName = (a: Balance.Token, b: Balance.Token) => {
const isValidNameA = containsOnlyValidChars(a.info.name) && containsOnlyValidChars(a.info.ticker)
const isValidNameB = containsOnlyValidChars(b.info.name) && containsOnlyValidChars(b.info.ticker)
const nameA = isValidNameA ? a.info.name.toLowerCase() : (a.info.ticker ?? '').toLowerCase()
const nameB = isValidNameB ? b.info.name.toLowerCase() : (b.info.ticker ?? '').toLowerCase()

// Move invalid names to the end.
if (!isValidNameA) {
return 1
}

if (nameA < nameB) {
return -1
}
if (nameA > nameB) {
return 1
}
return 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {Amounts, asQuantity, Quantities} from '../../../../../../../yoroi-wallet
import {NoAssetFoundImage} from '../../../../../../Send/common/NoAssetFoundImage'
import {Counter} from '../../../../../common/Counter/Counter'
import {filterBySearch} from '../../../../../common/filterBySearch'
import {sortTokensByName} from '../../../../../common/helpers'
import {useNavigateTo} from '../../../../../common/navigation'
import {useStrings} from '../../../../../common/strings'
import {useSwapForm} from '../../../../../common/SwapFormProvider'
Expand Down Expand Up @@ -71,7 +72,7 @@ const TokenList = () => {

const filteredTokenList = React.useMemo(() => {
const filter = filterBySearch(assetSearchTerm)
return tokens.filter((token) => filter(token.info))
return tokens.filter((token) => filter(token.info)).sort(sortTokensByName)
}, [tokens, assetSearchTerm])

return (
Expand Down

0 comments on commit fe70ec9

Please sign in to comment.