Skip to content

Commit

Permalink
fix: best effort global search unrug (#8641)
Browse files Browse the repository at this point in the history
  • Loading branch information
NeOMakinG authored Jan 21, 2025
1 parent 567df17 commit 178e55f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 17 deletions.
15 changes: 1 addition & 14 deletions src/state/slices/assetsSlice/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import { createSelector } from '@reduxjs/toolkit'
import type { AssetId, ChainId } from '@shapeshiftoss/caip'
import type { Asset } from '@shapeshiftoss/types'
import { isSome } from '@shapeshiftoss/utils'
import { matchSorter } from 'match-sorter'
import createCachedSelector from 're-reselect'
import { getChainAdapterManager } from 'context/PluginProvider/chainAdapterSingleton'
import type { ReduxState } from 'state/reducer'
import { createDeepEqualOutputSelector } from 'state/selector-utils'
import { selectAssetIdParamFromFilter, selectSearchQueryFromFilter } from 'state/selectors'
import { selectAssetIdParamFromFilter } from 'state/selectors'

import { getFeeAssetByAssetId, getFeeAssetByChainId } from './utils'

Expand Down Expand Up @@ -67,15 +66,3 @@ export const selectFeeAssetById = createCachedSelector(
state: ReduxState,
assetId: AssetId,
) => ReturnType<typeof getFeeAssetByAssetId>

export const selectAssetsBySearchQuery = createCachedSelector(
selectAssetsSortedByMarketCap,
selectSearchQueryFromFilter,
(sortedAssets: Asset[], searchQuery?: string): Asset[] => {
if (!searchQuery) return sortedAssets
return matchSorter(sortedAssets, searchQuery ?? '', {
keys: ['name', 'symbol', 'assetId'],
threshold: matchSorter.rankings.CONTAINS,
})
},
)((_state: ReduxState, filter) => filter?.searchQuery ?? 'assetsBySearchQuery')
54 changes: 52 additions & 2 deletions src/state/slices/common-selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@ import { isEvmChainId } from '@shapeshiftoss/chain-adapters'
import type { Asset, PartialRecord } from '@shapeshiftoss/types'
import orderBy from 'lodash/orderBy'
import pickBy from 'lodash/pickBy'
import { matchSorter } from 'match-sorter'
import createCachedSelector from 're-reselect'
import { createSelector } from 'reselect'
import { bn, bnOrZero } from 'lib/bignumber/bignumber'
import { fromBaseUnit } from 'lib/math'
import { isSome } from 'lib/utils'
import type { ReduxState } from 'state/reducer'
import { createDeepEqualOutputSelector } from 'state/selector-utils'
import { selectAccountIdParamFromFilter, selectAssetIdParamFromFilter } from 'state/selectors'
import {
selectAccountIdParamFromFilter,
selectAssetIdParamFromFilter,
selectSearchQueryFromFilter,
} from 'state/selectors'

import { selectAssets } from './assetsSlice/selectors'
import { selectAssets, selectAssetsSortedByMarketCap } from './assetsSlice/selectors'
import { getFeeAssetByChainId } from './assetsSlice/utils'
import {
selectMarketDataByAssetIdUserCurrency,
Expand Down Expand Up @@ -281,3 +286,48 @@ export const selectIsAssetWithoutMarketData = createSelector(
return !marketData || marketData.price === '0'
},
)

export const selectAssetsBySearchQuery = createCachedSelector(
selectAssetsSortedByMarketCap,
selectPortfolioAssetBalancesBaseUnit,
selectPortfolioUserCurrencyBalances,
selectMarketDataUsd,
selectSearchQueryFromFilter,
(
sortedAssets: Asset[],
portfolioBalancesCryptoBaseUnit,
portfolioBalancesUserCurrency,
marketDataUsd,
searchQuery?: string,
): Asset[] => {
if (!searchQuery) return sortedAssets

const matchedAssets = matchSorter(sortedAssets, searchQuery ?? '', {
keys: ['name', 'symbol', 'assetId'],
threshold: matchSorter.rankings.CONTAINS,
})

const getAssetBalanceCryptoPrecision = (asset: Asset) =>
fromBaseUnit(bnOrZero(portfolioBalancesCryptoBaseUnit[asset.assetId]), asset.precision)

const getAssetUserCurrencyBalance = (asset: Asset) =>
bnOrZero(portfolioBalancesUserCurrency[asset.assetId]).toNumber()

// This looks weird but isn't - looks like we could use the sorted selectAssetsByMarketCap instead of selectAssets
// but we actually can't - this would rug the quadruple-sorting
const getAssetMarketCap = (asset: Asset) =>
bnOrZero(marketDataUsd[asset.assetId]?.marketCap).toNumber()
const getAssetName = (asset: Asset) => asset.name

return orderBy(
Object.values(matchedAssets).filter(isSome),
[
getAssetUserCurrencyBalance,
getAssetMarketCap,
getAssetBalanceCryptoPrecision,
getAssetName,
],
['desc', 'desc', 'desc', 'asc'],
)
},
)((_state: ReduxState, filter) => filter?.searchQuery ?? 'assetsBySearchQuery')
3 changes: 2 additions & 1 deletion src/state/slices/search-selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import type { Asset } from '@shapeshiftoss/types'
import { createDeepEqualOutputSelector } from 'state/selector-utils'

import { selectSearchQueryFromFilter } from '../selectors'
import { selectAssetsBySearchQuery, selectTxsByQuery } from './selectors'
import { selectAssetsBySearchQuery } from './common-selectors'
import { selectTxsByQuery } from './txHistorySlice/selectors'
import type { TxId } from './txHistorySlice/txHistorySlice'

export enum GlobalSearchResultType {
Expand Down

0 comments on commit 178e55f

Please sign in to comment.