import { useQuery } from "@tanstack/react-query";
import type { Chain } from "../../../../../chains/types.js";
import type { ThirdwebClient } from "../../../../../client/client.js";
import { getOwnedTokens } from "../../../../../insight/get-tokens.js";
import { fontSize } from "../../../../core/design-system/index.js";
import { useWalletBalance } from "../../../../core/hooks/others/useWalletBalance.js";
import { useActiveAccount } from "../../../../core/hooks/wallets/useActiveAccount.js";
import { useActiveWalletChain } from "../../../../core/hooks/wallets/useActiveWalletChain.js";
import {
defaultTokens,
type SupportedTokens,
} from "../../../../core/utils/defaultTokens.js";
import { Container, Line, ModalHeader } from "../../components/basic.js";
import { Skeleton } from "../../components/Skeleton.js";
import { Spacer } from "../../components/Spacer.js";
import { TokenIcon } from "../../components/TokenIcon.js";
import { Text } from "../../components/text.js";
import type { ConnectLocale } from "../locale/types.js";
import { formatTokenBalance } from "./formatTokenBalance.js";
import {
type ERC20OrNativeToken,
isNativeToken,
NATIVE_TOKEN,
} from "./nativeToken.js";
/**
* @internal
*/
export function ViewTokens(props: {
supportedTokens?: SupportedTokens;
onBack: () => void;
client: ThirdwebClient;
connectLocale: ConnectLocale;
}) {
return (
);
}
export function ViewTokensContent(props: {
supportedTokens?: SupportedTokens;
client: ThirdwebClient;
connectLocale: ConnectLocale;
}) {
const account = useActiveAccount();
const activeChain = useActiveWalletChain();
const supportedTokens = props.supportedTokens || defaultTokens;
const tokenList =
(activeChain?.id ? supportedTokens[activeChain.id] : undefined) || [];
const erc20TokensQuery = useQuery({
// only fetch tokens if no explicit supported tokens are provided
enabled:
!!activeChain &&
!!account &&
(!props.supportedTokens || !props.supportedTokens[activeChain.id]),
queryFn: async () => {
if (!activeChain) {
throw new Error("No active chain");
}
if (!account) {
throw new Error("No account");
}
const result = await getOwnedTokens({
chains: [activeChain],
client: props.client,
ownerAddress: account.address,
});
return result.filter(
(token) =>
!defaultTokens[activeChain.id]?.some(
(t) => t.address.toLowerCase() === token.tokenAddress.toLowerCase(),
),
);
},
queryKey: ["tokens", activeChain?.id, account?.address],
});
if (!activeChain || !account) {
return null;
}
return (
<>
{tokenList.map((token) => {
return (
);
})}
{erc20TokensQuery.isLoading && (
)}
{erc20TokensQuery.data?.map((token) => {
return (
);
})}
>
);
}
function TokenInfo(props: {
token: ERC20OrNativeToken;
chain: Chain;
client: ThirdwebClient;
balanceData?: {
symbol: string;
name: string;
decimals: number;
displayValue: string;
};
}) {
const account = useActiveAccount();
const tokenBalanceQuery = useWalletBalance(
{
address: account?.address,
chain: props.chain,
client: props.client,
tokenAddress: isNativeToken(props.token)
? undefined
: props.token.address,
},
{
enabled: props.balanceData === undefined,
},
);
const tokenName = isNativeToken(props.token)
? tokenBalanceQuery.data?.name
: props.token.name;
return (
{tokenName ? (
{tokenName}
) : (
)}
{props.balanceData ? (
{formatTokenBalance(props.balanceData)}
) : tokenBalanceQuery.data ? (
{formatTokenBalance(tokenBalanceQuery.data)}
) : (
)}
);
}