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(widget): Showing history for selected blockchain if a blockchain has selected from main list #447

Merged
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
@@ -1 +1 @@
export const MOST_USED_BLOCKChAINS = ['ETH', 'COSMOS', 'OSMOSIS'];
export const MOST_USED_BLOCKCHAINS = ['ETH', 'COSMOS', 'OSMOSIS'];
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import type { PrepareListOptions } from './usePrepareBlockchainList.types';
import type {
PrepareListOptions,
PrepareOutput,
} from './usePrepareBlockchainList.types';
import type { BlockchainMeta } from 'rango-sdk';

import { MOST_USED_BLOCKChAINS } from './usePrepareBlockchainList.constants';
import { MOST_USED_BLOCKCHAINS } from './usePrepareBlockchainList.constants';

export function prepare(
blockchains: BlockchainMeta[],
preferredBlockchains: string[],
options?: PrepareListOptions
) {
): PrepareOutput {
/*
* TODO:
* We copying the `blockchains` which can causes performance penalties but we should because we are mutating the original object.
Expand All @@ -25,17 +28,37 @@ export function prepare(
let preferredBlockchainsWithoutMainList = preferredBlockchains;

if (preferredBlockchains.length <= options.limit) {
/*
* For first X (limit) numbers, we need to remove them from preferred if exists.
/**
*
* if we want to show 10 items (aka `options.limit`)
* and have these items as our preferred list: [x,y,z]
* and these as our main list: [A, B, C, D, E, F, G, H, I, J]
* the final list will be like this: [...preferred, ...mainList].
*
* Since we have a limit (e.g. 10) on our list (preferred + mainList),
* we only should check those items from main list that will be in final list.
*
* finalListWithoutLimit = [x,y,z, A, B, C, D, E, F, G, H, I, J]
* finalListWithLimit = [x,y,z, A, B, C, D, E, F, G]
*
* As you can see, [H, I, J] will not be in our list.
*
* The following code only check preffred blockchain
* shouldn't be in first 7 of 10 items in main list. (Since, the last 3 items will not be in the final list and we should check those.)
*/
for (let i = 0; i < options.limit; i++) {
const howManyItemsShouldBeCheckedFromMainList =
options.limit - preferredBlockchains.length;

for (let i = 0; i <= howManyItemsShouldBeCheckedFromMainList; i++) {
const blockchain = list[i];

preferredBlockchainsWithoutMainList =
preferredBlockchainsWithoutMainList.filter((preferredBlockchain) => {
return blockchain.name !== preferredBlockchain;
});
}
}

list.sort(
generateSortByPreferredBlockchainsFor(preferredBlockchainsWithoutMainList)
);
Expand All @@ -57,7 +80,7 @@ export function sortByMostUsedBlockchains(
a: BlockchainMeta,
b: BlockchainMeta
) {
const mostUsed = MOST_USED_BLOCKChAINS;
const mostUsed = MOST_USED_BLOCKCHAINS;
const aIndexInMostUsed = mostUsed.findIndex((token) => token === a.name);
const bIndexInMostUsed = mostUsed.findIndex((token) => token === b.name);
const aIsMostUsed = aIndexInMostUsed > -1;
Expand Down Expand Up @@ -106,3 +129,10 @@ export function generateSortByPreferredBlockchainsFor(
return 0;
};
}

export function isInVisibleList(
blockchain: string,
prepareOutput: PrepareOutput
): boolean {
return !!prepareOutput.list.find((item) => item.name === blockchain);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,66 @@ import type { BlockchainMeta } from 'rango-sdk';

import { TransactionType } from 'rango-sdk';

export const sampleDefaultBlockchainNames = [
'ETH',
'COSMOS',
'OSMOSIS',
'BSC',
'ARBITRUM',
'POLYGON',
'ZKSYNC',
'STARKNET',
'OPTIMISM',
'AVAX_CCHAIN',
'POLYGONZK',
'LINEA',
'TRON',
'BTC',
'NEUTRON',
'NOBLE',
'SOLANA',
'CRONOS',
'BNB',
'AURORA',
'MAYA',
'THOR',
'BOBA',
'MOONBEAM',
'MOONRIVER',
'OKC',
'BOBA_AVALANCHE',
'LTC',
'BCH',
'HECO',
'STARGAZE',
'CRYPTO_ORG',
'CHIHUAHUA',
'BANDCHAIN',
'COMDEX',
'REGEN',
'IRIS',
'EMONEY',
'JUNO',
'STRIDE',
'MARS',
'TERRA',
'BITSONG',
'AKASH',
'KI',
'PERSISTENCE',
'MEDIBLOC',
'KUJIRA',
'SENTINEL',
'INJECTIVE',
'SECRET',
'KONSTELLATION',
'STARNAME',
'BITCANNA',
'UMEE',
'DESMOS',
'LUMNETWORK',
];

export const sampleBlockchains: BlockchainMeta[] = [
{
name: 'ETH',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { BlockchainMeta } from 'rango-sdk';

import { beforeEach, describe, expect, it } from 'vitest';

import { MOST_USED_BLOCKChAINS } from './usePrepareBlockchainList.constants';
import { MOST_USED_BLOCKCHAINS } from './usePrepareBlockchainList.constants';
import {
generateSortByPreferredBlockchainsFor,
prepare,
Expand All @@ -20,8 +20,8 @@ describe('usePrepareBlockchainList', () => {
it('should sort blockchains by most used ones.', () => {
const sortedBlockchainsList = sample.sort(sortByMostUsedBlockchains);

const mostUsedCount = MOST_USED_BLOCKChAINS.length;
const expected = MOST_USED_BLOCKChAINS;
const mostUsedCount = MOST_USED_BLOCKCHAINS.length;
const expected = MOST_USED_BLOCKCHAINS;
const received = sortedBlockchainsList
.map((blockchain) => blockchain.name)
.slice(0, mostUsedCount);
Expand Down Expand Up @@ -270,4 +270,79 @@ describe('usePrepareBlockchainList', () => {
...received.more.map((blockchain) => blockchain.name),
]);
});

it.only('Last item of the main list should be moved to front if it selected again.', () => {
const listLimit = 10;
const preferredBlockchains = ['AVAX_CCHAIN', 'BTC'];
const expected = [
'AVAX_CCHAIN',
'BTC',
'ETH',
'COSMOS',
'OSMOSIS',
'BSC',
'ARBITRUM',
'POLYGON',
'ZKSYNC',
'STARKNET',
'OPTIMISM',
'POLYGONZK',
'LINEA',
'TRON',
'NEUTRON',
'NOBLE',
'SOLANA',
'CRONOS',
'BNB',
'AURORA',
'MAYA',
'THOR',
'BOBA',
'MOONBEAM',
'MOONRIVER',
'OKC',
'BOBA_AVALANCHE',
'LTC',
'BCH',
'HECO',
'STARGAZE',
'CRYPTO_ORG',
'CHIHUAHUA',
'BANDCHAIN',
'COMDEX',
'REGEN',
'IRIS',
'EMONEY',
'JUNO',
'STRIDE',
'MARS',
'TERRA',
'BITSONG',
'AKASH',
'KI',
'PERSISTENCE',
'MEDIBLOC',
'KUJIRA',
'SENTINEL',
'INJECTIVE',
'SECRET',
'KONSTELLATION',
'STARNAME',
'BITCANNA',
'UMEE',
'DESMOS',
'LUMNETWORK',
];

const output = prepare(sample, preferredBlockchains, {
limit: listLimit,
});

const received = [
...output.list.map((blockchain) => blockchain.name),
...output.more.map((blockchain) => blockchain.name),
];

expect(expected).toEqual(received);
});
});
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type {
PrepareListOptions,
PrepareListOutput,
UsePrepareList,
} from './usePrepareBlockchainList.types';
import type { BlockchainMeta } from 'rango-sdk';

import { useEffect } from 'react';

import { useSettingsStore } from '../../store/settings';

import { prepare } from './usePrepareBlockchainList.helpers';
import { isInVisibleList, prepare } from './usePrepareBlockchainList.helpers';

/**
*
Expand All @@ -19,12 +19,21 @@ import { prepare } from './usePrepareBlockchainList.helpers';
export function usePrepareBlockchainList(
blockchains: BlockchainMeta[],
options?: PrepareListOptions
): PrepareListOutput {
): UsePrepareList {
const { preferredBlockchains, addPreferredBlockchain } = useSettingsStore();

useEffect(() => {
if (options?.selected) {
addPreferredBlockchain(options?.selected);
const output = prepare(blockchains, preferredBlockchains, options);

/**
* We only add a new blockchain to preferred when it's not in the main list
* In this way we can guarantee we will be able to regenerate the same list always.
* And also it helps us to avoid jumping for the last item of the main list (e.g. tenth item of our list).
*/
if (!isInVisibleList(options.selected, output)) {
addPreferredBlockchain(options?.selected);
}
}
}, [options?.selected]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ export interface PrepareListOptions {
selected?: string;
}

export interface PrepareListOutput {
export interface PrepareOutput {
list: BlockchainMeta[];
more: BlockchainMeta[];
}

export interface UsePrepareList {
Ikari-Shinji-re marked this conversation as resolved.
Show resolved Hide resolved
list: PrepareOutput['list'];
more: PrepareOutput['more'];
history: string[];
}