-
Notifications
You must be signed in to change notification settings - Fork 146
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
749908e
commit 33d8d66
Showing
41 changed files
with
2,112 additions
and
1,041 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
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,56 @@ | ||
import get from 'lodash.get'; | ||
|
||
import { useRemoteConfig } 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 config = useRemoteConfig(); | ||
const configEnabledTokens: string[] = get(config, 'tokensEnabledByDefault', []); | ||
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); | ||
|
||
switch (filter) { | ||
case 'enabled': | ||
return isTokenEnabled({ tokenId, preEnabledTokensIds }); | ||
case 'disabled': | ||
return !isTokenEnabled({ tokenId, preEnabledTokensIds }); | ||
default: | ||
return true; | ||
} | ||
}); | ||
} | ||
|
||
return { isTokenEnabled, filterTokens }; | ||
} |
57 changes: 57 additions & 0 deletions
57
src/app/components/crypto-asset-item/crypto-asset-item-toggle.layout.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,57 @@ | ||
import { useState } from 'react'; | ||
import { useDispatch } from 'react-redux'; | ||
|
||
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'; | ||
|
||
interface CryptoAssetItemToggleLayoutProps { | ||
captionLeft: string; | ||
icon: React.ReactNode; | ||
titleLeft: string; | ||
assetId: string; | ||
isCheckedByDefault?: boolean; | ||
} | ||
|
||
export function CryptoAssetItemToggleLayout({ | ||
captionLeft, | ||
icon, | ||
titleLeft, | ||
assetId, | ||
isCheckedByDefault = false, | ||
}: CryptoAssetItemToggleLayoutProps) { | ||
const accountIndex = useCurrentAccountIndex(); | ||
const dispatch = useDispatch(); | ||
|
||
const [isChecked, setIsChecked] = useState(isCheckedByDefault); | ||
|
||
function handleSelection(enabled: boolean) { | ||
setIsChecked(enabled); | ||
dispatch(manageTokensSlice.actions.setToken({ id: assetId, enabled, accountIndex })); | ||
} | ||
|
||
const toggle = ( | ||
<VStack h="100%" justifyContent="center"> | ||
<Switch.Root onCheckedChange={handleSelection} checked={isChecked} id={assetId}> | ||
<Switch.Thumb /> | ||
</Switch.Root> | ||
</VStack> | ||
); | ||
|
||
const content = ( | ||
<Pressable onClick={() => handleSelection(!isChecked)} data-testid={assetId}> | ||
<ItemLayout | ||
img={icon} | ||
titleLeft={spamFilter(titleLeft)} | ||
captionLeft={spamFilter(captionLeft)} | ||
titleRight={toggle} | ||
/> | ||
</Pressable> | ||
); | ||
|
||
return <Box my="space.02">{content}</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, | ||
}; | ||
} |
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 }); | ||
} |
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,46 @@ | ||
import { type Sip10CryptoAssetFilter, type Sip10TokenAssetDetails } from '@leather.io/query'; | ||
import { type Sip10TokenAssetDetails } from '@leather.io/query'; | ||
|
||
import { useCombinedFilteredSip10Tokens } from '@app/common/hooks/use-filtered-sip10-tokens'; | ||
import { type AssetFilter, useManageTokens } from '@app/common/hooks/use-manage-tokens'; | ||
|
||
interface Sip10TokensLoaderProps { | ||
address: string; | ||
filter: Sip10CryptoAssetFilter; | ||
children(isLoading: boolean, tokens: Sip10TokenAssetDetails[]): React.ReactNode; | ||
assetFilter?: AssetFilter; | ||
|
||
children({ | ||
isLoading, | ||
tokens, | ||
preEnabledTokensIds, | ||
}: { | ||
isLoading: boolean; | ||
tokens: Sip10TokenAssetDetails[]; | ||
preEnabledTokensIds: string[]; | ||
}): React.ReactNode; | ||
} | ||
|
||
function getTokenId(token: Sip10TokenAssetDetails) { | ||
return token.info.contractId; | ||
} | ||
export function Sip10TokensLoader({ address, filter, children }: Sip10TokensLoaderProps) { | ||
const { isLoading, tokens = [] } = useCombinedFilteredSip10Tokens({ address, filter }); | ||
return children(isLoading, tokens); | ||
|
||
export function Sip10TokensLoader({ | ||
address, | ||
assetFilter = 'all', | ||
children, | ||
}: Sip10TokensLoaderProps) { | ||
const { | ||
isLoading, | ||
allTokens = [], | ||
supportedTokens, | ||
} = useCombinedFilteredSip10Tokens({ address, filter: 'all' }); | ||
|
||
const { filterTokens } = useManageTokens(); | ||
const preEnabledTokensIds = supportedTokens.map(t => t.info.contractId); | ||
const filteredTokens = filterTokens({ | ||
tokens: allTokens, | ||
filter: assetFilter, | ||
getTokenId, | ||
preEnabledTokensIds, | ||
}); | ||
|
||
return children({ isLoading, tokens: filteredTokens, preEnabledTokensIds }); | ||
} |
Oops, something went wrong.