-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add manage tokens, closes #5643
- Loading branch information
1 parent
775adf8
commit 7d3ac6f
Showing
43 changed files
with
2,133 additions
and
1,051 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { useConfigTokensEnabledByDefault } from '@leather.io/query'; | ||
|
||
import { useCurrentAccountIndex } from '@app/store/accounts/account'; | ||
import { useUserAllTokens } from '@app/store/manage-tokens/manage-tokens.slice'; | ||
|
||
interface IsTokenEnabledArgs { | ||
tokenId: string; | ||
preEnabledTokensIds: string[]; | ||
} | ||
export type AssetFilter = 'all' | 'enabled' | 'disabled'; | ||
interface FilterTokensArgs<T> { | ||
tokens: T[]; | ||
filter: AssetFilter; | ||
getTokenId(token: T): string; | ||
preEnabledTokensIds: string[]; | ||
} | ||
|
||
export function useManageTokens() { | ||
const configEnabledTokens = useConfigTokensEnabledByDefault(); | ||
|
||
const accountIndex = useCurrentAccountIndex(); | ||
const userTokensList = useUserAllTokens(); | ||
|
||
function isTokenEnabled({ tokenId, preEnabledTokensIds }: IsTokenEnabledArgs) { | ||
const token = userTokensList.find(t => t.accountIndex === accountIndex && t.id === tokenId); | ||
const isEnabledByDefault = | ||
configEnabledTokens.includes(tokenId) || preEnabledTokensIds?.includes(tokenId); | ||
|
||
return token?.enabled ?? isEnabledByDefault; | ||
} | ||
|
||
function filterTokens<T>({ | ||
tokens, | ||
filter = 'all', | ||
getTokenId, | ||
preEnabledTokensIds, | ||
}: FilterTokensArgs<T>): T[] { | ||
if (filter === 'all') return tokens; | ||
|
||
return tokens.filter(t => { | ||
const tokenId = getTokenId(t); | ||
const tokenEnabled = isTokenEnabled({ tokenId, preEnabledTokensIds }); | ||
|
||
return filter === 'enabled' ? tokenEnabled : !tokenEnabled; | ||
}); | ||
} | ||
|
||
return { isTokenEnabled, filterTokens }; | ||
} |
60 changes: 60 additions & 0 deletions
60
src/app/components/crypto-asset-item/crypto-asset-item-toggle.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { useState } from 'react'; | ||
import { useDispatch } from 'react-redux'; | ||
|
||
import { sanitize } from 'dompurify'; | ||
import { Box, VStack } from 'leather-styles/jsx'; | ||
|
||
import { ItemLayout, Pressable, Switch } from '@leather.io/ui'; | ||
import { spamFilter } from '@leather.io/utils'; | ||
|
||
import { useCurrentAccountIndex } from '@app/store/accounts/account'; | ||
import { manageTokensSlice } from '@app/store/manage-tokens/manage-tokens.slice'; | ||
|
||
export interface CryptoAssetItemToggleProps { | ||
captionLeft: string; | ||
icon: React.ReactNode; | ||
titleLeft: string; | ||
assetId: string; | ||
isCheckedByDefault: boolean; | ||
} | ||
|
||
export function CryptoAssetItemToggle({ | ||
captionLeft, | ||
icon, | ||
titleLeft, | ||
assetId, | ||
isCheckedByDefault = false, | ||
}: CryptoAssetItemToggleProps) { | ||
const accountIndex = useCurrentAccountIndex(); | ||
const dispatch = useDispatch(); | ||
|
||
const [isChecked, setIsChecked] = useState(isCheckedByDefault); | ||
|
||
function handleSelection(enabled: boolean) { | ||
setIsChecked(enabled); | ||
dispatch( | ||
manageTokensSlice.actions.userTogglesTokenVisibility({ id: assetId, enabled, accountIndex }) | ||
); | ||
} | ||
|
||
const toggle = ( | ||
<VStack h="100%" justifyContent="center"> | ||
<Switch.Root onCheckedChange={handleSelection} checked={isChecked} id={assetId}> | ||
<Switch.Thumb /> | ||
</Switch.Root> | ||
</VStack> | ||
); | ||
|
||
return ( | ||
<Box my="space.02"> | ||
<Pressable onClick={() => handleSelection(!isChecked)} data-testid={sanitize(assetId)}> | ||
<ItemLayout | ||
img={icon} | ||
titleLeft={spamFilter(titleLeft)} | ||
captionLeft={spamFilter(captionLeft)} | ||
titleRight={toggle} | ||
/> | ||
</Pressable> | ||
</Box> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 0 additions & 7 deletions
7
src/app/components/crypto-asset-item/crypto-asset-item.layout.utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,14 @@ | ||
import { CryptoAssetSelectors } from '@tests/selectors/crypto-asset.selectors'; | ||
|
||
import type { Money } from '@leather.io/models'; | ||
import { formatMoneyWithoutSymbol } from '@leather.io/utils'; | ||
|
||
import { formatBalance } from '@app/common/format-balance'; | ||
|
||
export function parseCryptoAssetBalance(availableBalance: Money) { | ||
const availableBalanceString = formatMoneyWithoutSymbol(availableBalance); | ||
const dataTestId = CryptoAssetSelectors.CryptoAssetListItem.replace( | ||
'{symbol}', | ||
availableBalance.symbol.toLowerCase() | ||
); | ||
const formattedBalance = formatBalance(availableBalanceString); | ||
|
||
return { | ||
availableBalanceString, | ||
dataTestId, | ||
formattedBalance, | ||
}; | ||
} |
20 changes: 20 additions & 0 deletions
20
src/app/components/crypto-asset-item/crypto-asset-item.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { CryptoAssetItemToggle, type CryptoAssetItemToggleProps } from './crypto-asset-item-toggle'; | ||
import { CryptoAssetItemLayout, type CryptoAssetItemLayoutProps } from './crypto-asset-item.layout'; | ||
|
||
interface CryptoAssetItemProps { | ||
isToggleMode: boolean; | ||
toggleProps?: CryptoAssetItemToggleProps; | ||
itemProps?: CryptoAssetItemLayoutProps; | ||
} | ||
|
||
export function CryptoAssetItem({ isToggleMode, toggleProps, itemProps }: CryptoAssetItemProps) { | ||
if (isToggleMode && toggleProps) { | ||
return <CryptoAssetItemToggle {...toggleProps} />; | ||
} | ||
|
||
if (itemProps) { | ||
return <CryptoAssetItemLayout {...itemProps} />; | ||
} | ||
|
||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,44 @@ | ||
import type { Brc20CryptoAssetInfo, CryptoAssetBalance, MarketData } from '@leather.io/models'; | ||
|
||
import { type AssetFilter, useManageTokens } from '@app/common/hooks/use-manage-tokens'; | ||
import { useBrc20Tokens } from '@app/query/bitcoin/ordinals/brc20/brc20-tokens.hooks'; | ||
|
||
interface Brc20TokenItem { | ||
balance: CryptoAssetBalance; | ||
info: Brc20CryptoAssetInfo; | ||
holderAddress: string; | ||
marketData: MarketData; | ||
} | ||
|
||
interface Brc20TokensLoaderProps { | ||
children( | ||
tokens: { | ||
balance: CryptoAssetBalance; | ||
info: Brc20CryptoAssetInfo; | ||
holderAddress: string; | ||
marketData: MarketData; | ||
}[] | ||
): React.ReactNode; | ||
filter?: AssetFilter; | ||
children({ | ||
tokens, | ||
preEnabledTokensIds, | ||
}: { | ||
tokens: Brc20TokenItem[]; | ||
preEnabledTokensIds: string[]; | ||
}): React.ReactNode; | ||
} | ||
|
||
function getTokenId(token: Brc20TokenItem) { | ||
return token.info.symbol; | ||
} | ||
export function Brc20TokensLoader({ children }: Brc20TokensLoaderProps) { | ||
export function Brc20TokensLoader({ children, filter = 'all' }: Brc20TokensLoaderProps) { | ||
const tokens = useBrc20Tokens(); | ||
return children(tokens); | ||
|
||
const { filterTokens } = useManageTokens(); | ||
|
||
const preEnabledTokensIds = tokens | ||
.filter(t => t.marketData.price.amount.isGreaterThan(0)) | ||
.map(t => t.info.symbol); | ||
|
||
const filteredTokens = filterTokens({ | ||
tokens, | ||
filter, | ||
getTokenId, | ||
preEnabledTokensIds, | ||
}); | ||
|
||
return children({ tokens: filteredTokens, preEnabledTokensIds }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,42 @@ | ||
import type { CryptoAssetBalance, MarketData, RuneCryptoAssetInfo } from '@leather.io/models'; | ||
import { useRuneTokens } from '@leather.io/query'; | ||
|
||
import { type AssetFilter, useManageTokens } from '@app/common/hooks/use-manage-tokens'; | ||
|
||
interface RunesLoaderProps { | ||
addresses: string[]; | ||
children( | ||
runes: { balance: CryptoAssetBalance; info: RuneCryptoAssetInfo; marketData: MarketData }[] | ||
): React.ReactNode; | ||
children({ | ||
tokens, | ||
preEnabledTokensIds, | ||
}: { | ||
tokens: RuneTokenItem[]; | ||
preEnabledTokensIds: string[]; | ||
}): React.ReactNode; | ||
filter?: AssetFilter; | ||
} | ||
|
||
interface RuneTokenItem { | ||
balance: CryptoAssetBalance; | ||
info: RuneCryptoAssetInfo; | ||
marketData: MarketData; | ||
} | ||
|
||
function getTokenId(token: RuneTokenItem) { | ||
return token.info.runeName; | ||
} | ||
export function RunesLoader({ addresses, children }: RunesLoaderProps) { | ||
|
||
export function RunesLoader({ addresses, children, filter = 'all' }: RunesLoaderProps) { | ||
const { runes = [] } = useRuneTokens(addresses); | ||
return children(runes); | ||
|
||
const { filterTokens } = useManageTokens(); | ||
|
||
const preEnabledTokensIds: string[] = []; | ||
const filteredTokens = filterTokens({ | ||
tokens: runes, | ||
filter, | ||
getTokenId, | ||
preEnabledTokensIds, | ||
}); | ||
|
||
return children({ tokens: filteredTokens, preEnabledTokensIds }); | ||
} |
Oops, something went wrong.