|
| 1 | +import { Skeleton } from "@/components/ui/skeleton"; |
| 2 | +import { isProd } from "@/constants/env-utils"; |
| 3 | +import { useQuery } from "@tanstack/react-query"; |
| 4 | +import { XIcon } from "lucide-react"; |
| 5 | +import Link from "next/link"; |
| 6 | +import { type ThirdwebClient, defineChain, toTokens } from "thirdweb"; |
| 7 | +import { |
| 8 | + Blobbie, |
| 9 | + TokenIcon, |
| 10 | + TokenProvider, |
| 11 | + useActiveAccount, |
| 12 | + useActiveWalletChain, |
| 13 | +} from "thirdweb/react"; |
| 14 | +import { ChainIconClient } from "../../../../../components/icons/ChainIcon"; |
| 15 | +import { useAllChainsData } from "../../../../../hooks/chains/allChains"; |
| 16 | +import { nebulaAppThirdwebClient } from "../../utils/nebulaThirdwebClient"; |
| 17 | + |
| 18 | +export type AssetBalance = { |
| 19 | + chain_id: number; |
| 20 | + token_address: string; |
| 21 | + balance: string; |
| 22 | + name: string; |
| 23 | + symbol: string; |
| 24 | + decimals: number; |
| 25 | +}; |
| 26 | + |
| 27 | +export function AssetsSectionUI(props: { |
| 28 | + data: AssetBalance[]; |
| 29 | + isPending: boolean; |
| 30 | + client: ThirdwebClient; |
| 31 | +}) { |
| 32 | + if (props.data.length === 0 && !props.isPending) { |
| 33 | + return ( |
| 34 | + <div className="flex h-full flex-col items-center justify-center gap-3 px-2 py-1"> |
| 35 | + <div className="rounded-full border p-1"> |
| 36 | + <XIcon className="size-4" /> |
| 37 | + </div> |
| 38 | + <div className="text-muted-foreground text-sm">No Assets </div> |
| 39 | + </div> |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + return ( |
| 44 | + <div className="flex flex-col gap-1"> |
| 45 | + {!props.isPending && |
| 46 | + props.data.map((asset) => ( |
| 47 | + <AssetItem |
| 48 | + key={asset.token_address} |
| 49 | + asset={asset} |
| 50 | + client={props.client} |
| 51 | + /> |
| 52 | + ))} |
| 53 | + |
| 54 | + {props.isPending && |
| 55 | + new Array(10).fill(null).map((_, index) => ( |
| 56 | + // biome-ignore lint/suspicious/noArrayIndexKey: <explanation> |
| 57 | + <SkeletonAssetItem key={index} /> |
| 58 | + ))} |
| 59 | + </div> |
| 60 | + ); |
| 61 | +} |
| 62 | + |
| 63 | +function SkeletonAssetItem() { |
| 64 | + return ( |
| 65 | + <div className="flex h-[48px] items-center gap-2 px-2 py-1"> |
| 66 | + <Skeleton className="size-8 rounded-full" /> |
| 67 | + <div className="flex flex-col gap-1"> |
| 68 | + <Skeleton className="h-3 w-32 bg-muted" /> |
| 69 | + <Skeleton className="h-3 w-24 bg-muted" /> |
| 70 | + </div> |
| 71 | + </div> |
| 72 | + ); |
| 73 | +} |
| 74 | + |
| 75 | +function AssetItem(props: { |
| 76 | + asset: AssetBalance; |
| 77 | + client: ThirdwebClient; |
| 78 | +}) { |
| 79 | + const { idToChain } = useAllChainsData(); |
| 80 | + const chainMeta = idToChain.get(props.asset.chain_id); |
| 81 | + return ( |
| 82 | + <TokenProvider |
| 83 | + address={props.asset.token_address} |
| 84 | + client={props.client} |
| 85 | + // eslint-disable-next-line no-restricted-syntax |
| 86 | + chain={defineChain(props.asset.chain_id)} |
| 87 | + > |
| 88 | + <div className="relative flex h-[48px] items-center gap-2.5 rounded-lg px-2 py-1 hover:bg-accent"> |
| 89 | + <div className="relative"> |
| 90 | + <TokenIcon |
| 91 | + className="size-8 rounded-full" |
| 92 | + loadingComponent={ |
| 93 | + <Blobbie |
| 94 | + address={props.asset.token_address} |
| 95 | + className="size-8 rounded-full" |
| 96 | + /> |
| 97 | + } |
| 98 | + fallbackComponent={ |
| 99 | + <Blobbie |
| 100 | + address={props.asset.token_address} |
| 101 | + className="size-8 rounded-full" |
| 102 | + /> |
| 103 | + } |
| 104 | + /> |
| 105 | + <div className="-right-0.5 -bottom-0.5 absolute rounded-full border bg-background p-0.5"> |
| 106 | + <ChainIconClient |
| 107 | + client={props.client} |
| 108 | + className="size-3.5" |
| 109 | + src={chainMeta?.icon?.url || ""} |
| 110 | + /> |
| 111 | + </div> |
| 112 | + </div> |
| 113 | + |
| 114 | + <div className="flex min-w-0 flex-col text-sm"> |
| 115 | + <Link |
| 116 | + href={`https://thirdweb.com/${props.asset.chain_id}/${props.asset.token_address}`} |
| 117 | + target="_blank" |
| 118 | + className="truncate font-medium before:absolute before:inset-0" |
| 119 | + > |
| 120 | + {props.asset.name} |
| 121 | + </Link> |
| 122 | + |
| 123 | + <p className="text-muted-foreground text-sm"> |
| 124 | + {`${toTokens(BigInt(props.asset.balance), props.asset.decimals)} ${props.asset.symbol}`} |
| 125 | + </p> |
| 126 | + </div> |
| 127 | + </div> |
| 128 | + </TokenProvider> |
| 129 | + ); |
| 130 | +} |
| 131 | + |
| 132 | +export function AssetsSection(props: { |
| 133 | + client: ThirdwebClient; |
| 134 | +}) { |
| 135 | + const account = useActiveAccount(); |
| 136 | + const activeChain = useActiveWalletChain(); |
| 137 | + |
| 138 | + const assetsQuery = useQuery({ |
| 139 | + queryKey: ["v1/tokens/erc20", account?.address, activeChain?.id], |
| 140 | + queryFn: async () => { |
| 141 | + if (!account || !activeChain) { |
| 142 | + return []; |
| 143 | + } |
| 144 | + const chains = [...new Set([1, 8453, 10, 137, activeChain.id])]; |
| 145 | + const url = new URL( |
| 146 | + `https://insight.${isProd ? "thirdweb" : "thirdweb-dev"}.com/v1/tokens/erc20/${account?.address}`, |
| 147 | + ); |
| 148 | + url.searchParams.set("limit", "50"); |
| 149 | + url.searchParams.set("metadata", "true"); |
| 150 | + url.searchParams.set("include_spam", "false"); |
| 151 | + url.searchParams.set("clientId", nebulaAppThirdwebClient.clientId); |
| 152 | + for (const chain of chains) { |
| 153 | + url.searchParams.append("chain", chain.toString()); |
| 154 | + } |
| 155 | + |
| 156 | + const response = await fetch(url.toString()); |
| 157 | + const json = (await response.json()) as { |
| 158 | + data: AssetBalance[]; |
| 159 | + }; |
| 160 | + |
| 161 | + return json.data; |
| 162 | + }, |
| 163 | + enabled: !!account && !!activeChain, |
| 164 | + }); |
| 165 | + |
| 166 | + return ( |
| 167 | + <AssetsSectionUI |
| 168 | + data={assetsQuery.data ?? []} |
| 169 | + isPending={assetsQuery.isPending} |
| 170 | + client={props.client} |
| 171 | + /> |
| 172 | + ); |
| 173 | +} |
0 commit comments