Skip to content

Commit

Permalink
Feat: Portfolio search (#2880)
Browse files Browse the repository at this point in the history
  • Loading branch information
tuul-wq authored Dec 20, 2024
1 parent 25d96b1 commit 753fc3f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ const mockTokens: AssetByChains[] = [
},
];

// TODO input data is a bit complex and after refactoring of internal model, chains wallet and etc should be presented.
// For now it's simplier to turn off some of the test and think about simplifying external dependencies.
// TODO: input data is a bit complex and after refactoring of internal model, chains wallet and etc should be presented.
// For now it's simpler to turn off some of the test and think about simplifying external dependencies.

describe('Portfolio model', () => {
test('should handle activeViewChanged event', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { combine, createEvent, createStore, restore } from 'effector';
import { once } from 'patronum';

import { type Account, type AssetByChains } from '@/shared/core';
import { includes, nullable } from '@/shared/lib/utils';
import { includesMultiple, nullable } from '@/shared/lib/utils';
import { AssetsListView } from '@/entities/asset';
import { balanceModel } from '@/entities/balance';
import { networkModel, networkUtils } from '@/entities/network';
Expand Down Expand Up @@ -119,25 +119,43 @@ const $activeTokensWithBalance = combine(
);

const $filteredTokensWithBalance = combine(
{ activeTokensWithBalance: $activeTokensWithBalance, query: $query },
{
activeTokensWithBalance: $activeTokensWithBalance,
query: $query,
},
({ activeTokensWithBalance, query }) => {
const filteredTokens: AssetByChains[] = [];
let filteredTokens: AssetByChains[] = [];
const fullChainMatch: number[] = [];

for (const token of activeTokensWithBalance) {
const filteredChains = token.chains.filter((chain) => {
const hasSymbol = includes(chain.assetSymbol, query);
const hasAssetName = includes(token.name, query);
const hasChainName = includes(chain.name, query);
// Case 1: full match for token symbol -> get only that token across all chains
if (query.toLowerCase() === token.symbol.toLowerCase()) {
filteredTokens = [{ ...token, chains: token.chains }];
break;
}

return hasSymbol || hasAssetName || hasChainName;
});
let tokenChains = [];
for (const chain of token.chains) {
// Case 2: full match for chain name -> get all tokens for that chain
if (query.toLowerCase() === chain.name.toLowerCase()) {
fullChainMatch.push(filteredTokens.length);
tokenChains = [chain];
break;
}
// Case 3: partial match for chain name or asset symbol
if (includesMultiple([chain.name, chain.assetSymbol], query)) {
tokenChains.push(chain);
}
}

if (filteredChains.length === 0) continue;
if (tokenChains.length === 0) continue;

filteredTokens.push({ ...token, chains: filteredChains });
filteredTokens.push({ ...token, chains: tokenChains });
}

return filteredTokens;
if (fullChainMatch.length === 0) return filteredTokens;

return filteredTokens.filter((_, index) => fullChainMatch.includes(index));
},
);

Expand Down
3 changes: 2 additions & 1 deletion src/renderer/shared/lib/utils/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const emptyMeta = <M extends object>(): M => {
* Performs searching by query and sort using weight of each field
*
* @param records - List of objects
* @param meta - List of additional info, associated with given record by index
* @param getMeta - List of additional info, associated with given record by
* index
* @param query - Requested string
* @param queryMinLength - From this query length method starts to perform
* search
Expand Down

0 comments on commit 753fc3f

Please sign in to comment.