Skip to content

Commit

Permalink
[Issue-3920] Show well-known tokens on top
Browse files Browse the repository at this point in the history
  • Loading branch information
PDTnhah committed Dec 19, 2024
1 parent 5c8e40f commit 7a273ef
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/extension-base/src/background/KoniTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2297,7 +2297,7 @@ export interface KoniRequestSignatures {
/* Ledger */

/* Popular tokens */
'pri(popular.tokens)': [null, string[], string[]];
'pri(popular.tokens)': [null, Record<string, number>, Record<string, number>];
/* Popular tokens */
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3935,7 +3935,7 @@ export default class KoniExtension {

/* Popular tokens */

private subscribePopularTokens (id: string, port: chrome.runtime.Port): string[] {
private subscribePopularTokens (id: string, port: chrome.runtime.Port): Record<string, number> {
const cb = createSubscription<'pri(popular.tokens)'>(id, port);

const subscription = this.#koniState.chainService.observablePopularTokens.popularTokens.subscribe(cb);
Expand Down
6 changes: 3 additions & 3 deletions packages/extension-base/src/services/chain-service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class ChainService {
private assetLogoMapSubject = new BehaviorSubject<Record<string, string>>(AssetLogoMap);
private chainLogoMapSubject = new BehaviorSubject<Record<string, string>>(ChainLogoMap);
private ledgerGenericAllowChainsSubject = new BehaviorSubject<string[]>([]);
private popularTokensSubject = new BehaviorSubject<string[]>([]);
private popularTokensSubject = new BehaviorSubject<Record<string, number>>({});

// Todo: Update to new store indexed DB
private store: AssetSettingStore = new AssetSettingStore();
Expand Down Expand Up @@ -788,7 +788,7 @@ export class ChainService {
this.logger.log('Finished updating latest ledger generic allow chains');
}

handleLatestPopularTokens (latestPopularTokens: string[]) {
handleLatestPopularTokens (latestPopularTokens: Record<string, number>) {
this.popularTokensSubject.next(latestPopularTokens);
this.logger.log('Finished updating latest popular tokens');
}
Expand Down Expand Up @@ -1121,7 +1121,7 @@ export class ChainService {
}

private async fetchLatestPopularTokens () {
return await fetchStaticData<string[]>('chain-assets/popular-tokens') || [];
return await fetchStaticData<Record<string, number>>('chain-assets/popular-tokens') || [];
}

private async initChains () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const initialState: ChainStore = {
chainStateMap: {},
chainStatusMap: {},
ledgerGenericAllowNetworks: [],
popularTokens: [],
popularTokens: {},
reduxStatus: ReduxStatus.INIT
};

Expand Down Expand Up @@ -55,7 +55,7 @@ const chainStoreSlice = createSlice({
reduxStatus: ReduxStatus.READY
};
},
updatePopularTokens (state, action: PayloadAction<string[]>) {
updatePopularTokens (state, action: PayloadAction<Record<string, number>>) {
const { payload } = action;

return {
Expand Down
2 changes: 1 addition & 1 deletion packages/extension-koni-ui/src/stores/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export interface ChainStore extends BaseReduxStore {
chainStateMap: Record<string, _ChainState>
chainStatusMap: Record<string, _ChainApiStatus>
ledgerGenericAllowNetworks: string[];
popularTokens: string[];
popularTokens: Record<string, number>;
}

export interface BalanceStore extends BaseReduxStore {
Expand Down
2 changes: 1 addition & 1 deletion packages/extension-koni-ui/src/stores/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ export const subscribeUnreadNotificationCount = lazySubscribeMessage('pri(inappN
/* Notification service */

/* Popular tokens */
export const updatePopularTokens = (data: string[]) => {
export const updatePopularTokens = (data: Record<string, number>) => {
store.dispatch({ type: 'chainStore/updatePopularTokens', payload: data });
};

Expand Down
14 changes: 10 additions & 4 deletions packages/extension-koni-ui/src/utils/sort/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ export const sortTokenByValue = (a: TokenBalanceItemType, b: TokenBalanceItemTyp
}
};

export function sortToken (tokenGroupSlug: TokenBalanceItemType[], popularTokens: string[]) {
export function sortToken (tokenGroupSlug: TokenBalanceItemType[], popularTokens: Record<string, number>) {
return tokenGroupSlug.sort((a, b) => {
const aIsPiorityToken = popularTokens.includes(a.slug);
const bIsPiorityToken = popularTokens.includes(b.slug);
const aIsPiorityToken = Object.keys(popularTokens).includes(a.slug);
const bIsPiorityToken = Object.keys(popularTokens).includes(b.slug);
const aPiority = popularTokens[a.slug];
const bPiority = popularTokens[b.slug];

if (aIsPiorityToken && !bIsPiorityToken) {
return -1;
Expand All @@ -25,7 +27,11 @@ export function sortToken (tokenGroupSlug: TokenBalanceItemType[], popularTokens
} else if (!aIsPiorityToken && !bIsPiorityToken) {
return sortTokenByValue(a, b);
} else {
return 0;
if (aPiority < bPiority) {
return -1;
} else {
return 1;
}
}
});
}

0 comments on commit 7a273ef

Please sign in to comment.