-
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
0717b12
commit 83e092f
Showing
33 changed files
with
1,679 additions
and
997 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import get from 'lodash.get'; | ||
|
||
import { useFilteredSip10Tokens, useRemoteConfig } from '@leather.io/query'; | ||
|
||
import { useCurrentStacksAccount } from '@app/store/accounts/blockchain/stacks/stacks-account.hooks'; | ||
import type { TokenUserSetting } from '@app/store/manage-tokens/manage-tokens.slice'; | ||
|
||
interface IsTokenEnabledArgs { | ||
userTokensList: TokenUserSetting[]; | ||
accountIndex: number; | ||
tokenId: string; | ||
} | ||
export type AssetFilter = 'all' | 'enabled' | 'disabled'; | ||
interface FilterTokensArgs<T> { | ||
tokens: T[]; | ||
accountIndex: number; | ||
userTokensList: TokenUserSetting[]; | ||
filter: AssetFilter; | ||
getTokenId(token: T): string; | ||
} | ||
|
||
export function useDefaultEnabledTokens() { | ||
const config = useRemoteConfig(); | ||
const configEnabledTokens: string[] = get(config, 'defaultEnabledTokens', []); | ||
const stxAddress = useCurrentStacksAccount()?.address ?? ''; | ||
const sip10Tokens = useFilteredSip10Tokens({ address: stxAddress, filter: 'supported' }); | ||
|
||
function isTokenEnabled({ userTokensList, accountIndex, tokenId }: IsTokenEnabledArgs) { | ||
const token = userTokensList.find(t => t.accountIndex === accountIndex && t.id === tokenId); | ||
const isEnabledByDefault = | ||
configEnabledTokens.includes(tokenId) || | ||
sip10Tokens.tokens.some(t => t.info.contractId === tokenId); | ||
return token?.enabled ?? isEnabledByDefault; | ||
} | ||
|
||
function filterTokens<T>({ | ||
tokens, | ||
accountIndex, | ||
userTokensList, | ||
filter = 'all', | ||
getTokenId, | ||
}: FilterTokensArgs<T>): T[] { | ||
if (filter === 'all') return tokens; | ||
return tokens.filter(t => { | ||
const tokenId = getTokenId(t); | ||
|
||
switch (filter) { | ||
case 'enabled': | ||
return isTokenEnabled({ tokenId, userTokensList, accountIndex }); | ||
case 'disabled': | ||
return !isTokenEnabled({ tokenId, userTokensList, accountIndex }); | ||
default: | ||
return true; | ||
} | ||
}); | ||
} | ||
|
||
return { isTokenEnabled, filterTokens }; | ||
} |
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
61 changes: 61 additions & 0 deletions
61
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,61 @@ | ||
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 { useDefaultEnabledTokens } from '@app/common/hooks/use-default-enabled-tokens'; | ||
import { useCurrentAccountIndex } from '@app/store/accounts/account'; | ||
import { manageTokensSlice, useUserAllTokens } from '@app/store/manage-tokens/manage-tokens.slice'; | ||
|
||
interface CryptoAssetItemToggleLayoutProps { | ||
captionLeft: string; | ||
icon: React.ReactNode; | ||
titleLeft: string; | ||
assetId?: string; | ||
} | ||
|
||
export function CryptoAssetItemToggleLayout({ | ||
captionLeft, | ||
icon, | ||
titleLeft, | ||
assetId, | ||
}: CryptoAssetItemToggleLayoutProps) { | ||
const userTokensList = useUserAllTokens(); | ||
const accountIndex = useCurrentAccountIndex(); | ||
const dispatch = useDispatch(); | ||
const { isTokenEnabled } = useDefaultEnabledTokens(); | ||
|
||
const tokenId = assetId ?? titleLeft; | ||
const [isChecked, setIsChecked] = useState( | ||
isTokenEnabled({ accountIndex, userTokensList, tokenId }) | ||
); | ||
|
||
function handleSelection(enabled: boolean) { | ||
setIsChecked(enabled); | ||
dispatch(manageTokensSlice.actions.setToken({ id: tokenId, enabled, accountIndex })); | ||
} | ||
|
||
const toggle = ( | ||
<VStack h="100%" justifyContent="center"> | ||
<Switch.Root onCheckedChange={handleSelection} checked={isChecked} id={tokenId}> | ||
<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
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,42 @@ | ||
import type { Brc20CryptoAssetInfo, CryptoAssetBalance, MarketData } from '@leather.io/models'; | ||
|
||
import { | ||
type AssetFilter, | ||
useDefaultEnabledTokens, | ||
} from '@app/common/hooks/use-default-enabled-tokens'; | ||
import { useBrc20Tokens } from '@app/query/bitcoin/ordinals/brc20/brc20-tokens.hooks'; | ||
import { useCurrentAccountIndex } from '@app/store/accounts/account'; | ||
import { useUserAllTokens } from '@app/store/manage-tokens/manage-tokens.slice'; | ||
|
||
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: Brc20TokenItem[]): 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 accountIndex = useCurrentAccountIndex(); | ||
const userTokensList = useUserAllTokens(); | ||
|
||
const { filterTokens } = useDefaultEnabledTokens(); | ||
|
||
const filteredTokens = filterTokens({ | ||
tokens, | ||
accountIndex, | ||
userTokensList, | ||
filter, | ||
getTokenId, | ||
}); | ||
|
||
return children(filteredTokens); | ||
} |
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,45 @@ | ||
import type { CryptoAssetBalance, MarketData, RuneCryptoAssetInfo } from '@leather.io/models'; | ||
import { useRuneTokens } from '@leather.io/query'; | ||
|
||
import { | ||
type AssetFilter, | ||
useDefaultEnabledTokens, | ||
} from '@app/common/hooks/use-default-enabled-tokens'; | ||
import { useCurrentAccountIndex } from '@app/store/accounts/account'; | ||
import { useUserAllTokens } from '@app/store/manage-tokens/manage-tokens.slice'; | ||
|
||
interface RunesLoaderProps { | ||
addresses: string[]; | ||
children( | ||
runes: { balance: CryptoAssetBalance; info: RuneCryptoAssetInfo; marketData: MarketData }[] | ||
): React.ReactNode; | ||
filter?: AssetFilter; | ||
} | ||
|
||
interface RuneTokenItem { | ||
balance: CryptoAssetBalance; | ||
info: RuneCryptoAssetInfo; | ||
marketData: MarketData; | ||
} | ||
|
||
function getTokenId(token: RuneTokenItem) { | ||
return token.info.spacedRuneName ?? 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 accountIndex = useCurrentAccountIndex(); | ||
const userTokensList = useUserAllTokens(); | ||
const { filterTokens } = useDefaultEnabledTokens(); | ||
|
||
const filteredTokens = filterTokens({ | ||
tokens: runes, | ||
accountIndex, | ||
userTokensList, | ||
filter, | ||
getTokenId, | ||
}); | ||
|
||
return children(filteredTokens); | ||
} |
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,43 @@ | ||
import { type Sip10CryptoAssetFilter, type Sip10TokenAssetDetails } from '@leather.io/query'; | ||
|
||
import { | ||
type AssetFilter, | ||
useDefaultEnabledTokens, | ||
} from '@app/common/hooks/use-default-enabled-tokens'; | ||
import { useCombinedFilteredSip10Tokens } from '@app/common/hooks/use-filtered-sip10-tokens'; | ||
import { useCurrentAccountIndex } from '@app/store/accounts/account'; | ||
import { useUserAllTokens } from '@app/store/manage-tokens/manage-tokens.slice'; | ||
|
||
interface Sip10TokensLoaderProps { | ||
address: string; | ||
assetFilter?: AssetFilter; | ||
filter: Sip10CryptoAssetFilter; | ||
children(isLoading: boolean, tokens: Sip10TokenAssetDetails[]): React.ReactNode; | ||
} | ||
export function Sip10TokensLoader({ address, filter, children }: Sip10TokensLoaderProps) { | ||
|
||
function getTokenId(token: Sip10TokenAssetDetails) { | ||
return token.info.contractId; | ||
} | ||
|
||
export function Sip10TokensLoader({ | ||
address, | ||
filter, | ||
assetFilter = 'all', | ||
children, | ||
}: Sip10TokensLoaderProps) { | ||
const { isLoading, tokens = [] } = useCombinedFilteredSip10Tokens({ address, filter }); | ||
return children(isLoading, tokens); | ||
|
||
const accountIndex = useCurrentAccountIndex(); | ||
const userTokensList = useUserAllTokens(); | ||
const { filterTokens } = useDefaultEnabledTokens(); | ||
|
||
const filteredTokens = filterTokens({ | ||
tokens, | ||
accountIndex, | ||
userTokensList, | ||
filter: assetFilter, | ||
getTokenId, | ||
}); | ||
|
||
return children(isLoading, filteredTokens); | ||
} |
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,16 +1,40 @@ | ||
import type { CryptoAssetBalance, Src20CryptoAssetInfo } from '@leather.io/models'; | ||
import { useSrc20TokensByAddress } from '@leather.io/query'; | ||
|
||
import { | ||
type AssetFilter, | ||
useDefaultEnabledTokens, | ||
} from '@app/common/hooks/use-default-enabled-tokens'; | ||
import { useCurrentAccountIndex } from '@app/store/accounts/account'; | ||
import { useUserAllTokens } from '@app/store/manage-tokens/manage-tokens.slice'; | ||
|
||
export interface Src20TokenAssetDetails { | ||
balance: CryptoAssetBalance; | ||
info: Src20CryptoAssetInfo; | ||
} | ||
|
||
interface Src20TokensLoaderProps { | ||
address: string; | ||
filter?: AssetFilter; | ||
children(tokens: Src20TokenAssetDetails[]): React.ReactNode; | ||
} | ||
export function Src20TokensLoader({ address, children }: Src20TokensLoaderProps) { | ||
|
||
function getTokenId(token: Src20TokenAssetDetails) { | ||
return token.info.symbol; | ||
} | ||
|
||
export function Src20TokensLoader({ address, children, filter = 'all' }: Src20TokensLoaderProps) { | ||
const { data: tokens = [] } = useSrc20TokensByAddress(address); | ||
return children(tokens); | ||
const accountIndex = useCurrentAccountIndex(); | ||
const { filterTokens } = useDefaultEnabledTokens(); | ||
const userTokensList = useUserAllTokens(); | ||
|
||
const filteredTokens = filterTokens({ | ||
tokens, | ||
accountIndex, | ||
userTokensList, | ||
filter, | ||
getTokenId, | ||
}); | ||
return children(filteredTokens); | ||
} |
Oops, something went wrong.