Skip to content

Commit 10f561b

Browse files
committed
chore: break up token list grouping
1 parent ac25f16 commit 10f561b

4 files changed

Lines changed: 106 additions & 45 deletions

File tree

src/hooks/useTokenListGrouping.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { useMemo } from 'react';
2+
3+
import type { Token } from 'config/tokens';
4+
5+
import { amount as sdkAmount } from '@wormhole-foundation/sdk';
6+
import type { Balances } from 'utils/wallet/types';
7+
8+
export function useTokenListGrouping(
9+
sortedTokens: Token[],
10+
isWalletConnected: boolean,
11+
isGroupingEnabled: boolean,
12+
balances: Balances,
13+
): {
14+
listItems: Token[];
15+
ownedCount: number;
16+
} {
17+
const { listItems, ownedCount } = useMemo(() => {
18+
if (!isGroupingEnabled) {
19+
return { listItems: sortedTokens, ownedCount: 0 };
20+
}
21+
22+
const hasPositiveBalance = (token: Token) => {
23+
if (!isWalletConnected) {
24+
return false;
25+
}
26+
27+
const bal = balances?.[token.key]?.balance;
28+
return Boolean(bal && sdkAmount.units(bal) > 0n);
29+
};
30+
31+
const nativeToken = sortedTokens.find((token) => token.isNativeGasToken);
32+
const ownedTokens = sortedTokens.filter(hasPositiveBalance);
33+
const ownedSet = new Set(ownedTokens.map((t) => t.key));
34+
35+
const nativePart =
36+
nativeToken && !ownedSet.has(nativeToken.key) ? [nativeToken] : [];
37+
38+
const rest = sortedTokens.filter(
39+
(tok) =>
40+
!ownedSet.has(tok.key) && (!nativeToken || tok.key !== nativeToken.key),
41+
);
42+
43+
const listItems = [...ownedTokens, ...nativePart, ...rest];
44+
45+
return { listItems, ownedCount: ownedTokens.length };
46+
}, [isGroupingEnabled, isWalletConnected, balances, sortedTokens]);
47+
return { listItems, ownedCount };
48+
}

src/utils/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,13 @@ export const getUSDFormat = (price: number | undefined): string => {
204204
}
205205

206206
if (price > 0 && price < 0.01) {
207-
return '<$0.01';
207+
// return '<$0.01';
208+
return Intl.NumberFormat('en-EN', {
209+
style: 'currency',
210+
currency: 'USD',
211+
maximumFractionDigits: 2,
212+
minimumFractionDigits: 2,
213+
}).format(price);
208214
}
209215

210216
return Intl.NumberFormat('en-EN', {

src/views/v3/Bridge/AssetPicker/TokenList.tsx

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import { useTokens } from 'contexts/TokensContext';
1515
import type { Balances } from 'utils/wallet/types';
1616
import { useTokenListWithSearch } from 'hooks/useTokenListWithSearch';
1717
import { amount as sdkAmount } from '@wormhole-foundation/sdk';
18+
import TokenSectionHeader from './TokenSectionHeader';
19+
import { useTokenListGrouping } from 'hooks/useTokenListGrouping';
1820

1921
type Props = {
2022
tokenList: Array<Token>;
@@ -124,36 +126,12 @@ const TokenList = (props: Props) => {
124126
const isGroupingEnabled = props.isSource && !props.searchQuery;
125127
const isWalletConnected = Boolean(props.wallet?.address);
126128

127-
const { listItems, ownedCount } = useMemo(() => {
128-
if (!isGroupingEnabled) {
129-
return { listItems: sortedTokens, ownedCount: 0 };
130-
}
131-
132-
const hasPositiveBalance = (token: Token) => {
133-
if (!isWalletConnected) {
134-
return false;
135-
}
136-
137-
const bal = props.balances?.[token.key]?.balance;
138-
return Boolean(bal && sdkAmount.units(bal) > 0n);
139-
};
140-
141-
const nativeToken = sortedTokens.find((token) => token.isNativeGasToken);
142-
const ownedTokens = sortedTokens.filter(hasPositiveBalance);
143-
const ownedSet = new Set(ownedTokens.map((t) => t.key));
144-
145-
const nativePart =
146-
nativeToken && !ownedSet.has(nativeToken.key) ? [nativeToken] : [];
147-
148-
const rest = sortedTokens.filter(
149-
(tok) =>
150-
!ownedSet.has(tok.key) && (!nativeToken || tok.key !== nativeToken.key),
151-
);
152-
153-
const listItems = [...ownedTokens, ...nativePart, ...rest];
154-
155-
return { listItems, ownedCount: ownedTokens.length };
156-
}, [isGroupingEnabled, isWalletConnected, props.balances, sortedTokens]);
129+
const { listItems, ownedCount } = useTokenListGrouping(
130+
sortedTokens,
131+
isWalletConnected,
132+
isGroupingEnabled,
133+
props.balances,
134+
);
157135

158136
const searchList = (
159137
<SearchableList<Token>
@@ -190,20 +168,12 @@ const TokenList = (props: Props) => {
190168

191169
return (
192170
<Fragment key={token.key}>
193-
{isGroupingEnabled && index === 0 && ownedCount > 0 && (
194-
<Box sx={{ padding: '4px 16px' }}>
195-
<Typography fontSize={14} color={theme.palette.text.secondary}>
196-
Your tokens
197-
</Typography>
198-
</Box>
199-
)}
200-
{isGroupingEnabled && index === ownedCount && (
201-
<Box sx={{ padding: '4px 16px' }}>
202-
<Typography fontSize={14} color={theme.palette.text.secondary}>
203-
All tokens
204-
</Typography>
205-
</Box>
206-
)}
171+
<TokenSectionHeader
172+
index={index}
173+
ownedCount={ownedCount}
174+
isGroupingEnabled={isGroupingEnabled}
175+
theme={theme}
176+
/>
207177
<TokenItem
208178
key={token.key}
209179
token={token}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { Box, useTheme } from '@mui/material';
2+
3+
import { Typography } from '@mui/material';
4+
import React from 'react';
5+
6+
const TokenSectionHeader = ({
7+
index,
8+
ownedCount,
9+
isGroupingEnabled,
10+
}: {
11+
index: number;
12+
ownedCount: number;
13+
isGroupingEnabled: boolean;
14+
}) => {
15+
const theme = useTheme();
16+
17+
return (
18+
<>
19+
{isGroupingEnabled && index === 0 && ownedCount > 0 && (
20+
<Box sx={{ padding: '4px 16px' }}>
21+
<Typography fontSize={14} color={theme.palette.text.secondary}>
22+
Your tokens
23+
</Typography>
24+
</Box>
25+
)}
26+
{isGroupingEnabled && index === ownedCount && (
27+
<Box sx={{ padding: '4px 16px' }}>
28+
<Typography fontSize={14} color={theme.palette.text.secondary}>
29+
All tokens
30+
</Typography>
31+
</Box>
32+
)}
33+
</>
34+
);
35+
};
36+
37+
export default TokenSectionHeader;

0 commit comments

Comments
 (0)