|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { type UseQueryOptions, useQuery } from "@tanstack/react-query"; |
| 4 | +import type React from "react"; |
| 5 | +import type { JSX } from "react"; |
| 6 | +import { getChainMetadata } from "../../../../../chains/utils.js"; |
| 7 | +import { NATIVE_TOKEN_ADDRESS } from "../../../../../constants/addresses.js"; |
| 8 | +import { getContract } from "../../../../../contract/contract.js"; |
| 9 | +import { name } from "../../../../../extensions/common/read/name.js"; |
| 10 | +import { useTokenContext } from "./provider.js"; |
| 11 | + |
| 12 | +/** |
| 13 | + * Props for the TokenName component |
| 14 | + * @component |
| 15 | + * @token |
| 16 | + */ |
| 17 | +export interface TokenNameProps |
| 18 | + extends Omit<React.HTMLAttributes<HTMLSpanElement>, "children"> { |
| 19 | + /** |
| 20 | + * This prob can be a string or a (async) function that resolves to a string, representing the name of the token |
| 21 | + * This is particularly useful if you already have a way to fetch the token name. |
| 22 | + */ |
| 23 | + nameResolver?: string | (() => string) | (() => Promise<string>); |
| 24 | + /** |
| 25 | + * A function to format the name's display value |
| 26 | + * Particularly useful to avoid overflowing-UI issues |
| 27 | + * |
| 28 | + * ```tsx |
| 29 | + * <TokenName formatFn={(str: string) => doSomething()} /> |
| 30 | + * ``` |
| 31 | + */ |
| 32 | + formatFn?: (num: number) => number; |
| 33 | + /** |
| 34 | + * This component will be shown while the name of the token is being fetched |
| 35 | + * If not passed, the component will return `null`. |
| 36 | + * |
| 37 | + * You can/should pass a loading sign or spinner to this prop. |
| 38 | + * @example |
| 39 | + * ```tsx |
| 40 | + * <TokenName loadingComponent={<Spinner />} /> |
| 41 | + * ``` |
| 42 | + */ |
| 43 | + loadingComponent?: JSX.Element; |
| 44 | + /** |
| 45 | + * This component will be shown if the name fails to be retreived |
| 46 | + * If not passed, the component will return `null`. |
| 47 | + * |
| 48 | + * You can/should pass a descriptive text/component to this prop, indicating that the |
| 49 | + * name was not fetched succesfully |
| 50 | + * @example |
| 51 | + * ```tsx |
| 52 | + * <TokenName fallbackComponent={"Failed to load"} |
| 53 | + * /> |
| 54 | + * ``` |
| 55 | + */ |
| 56 | + fallbackComponent?: JSX.Element; |
| 57 | + /** |
| 58 | + * Optional `useQuery` params |
| 59 | + */ |
| 60 | + queryOptions?: Omit<UseQueryOptions<string>, "queryFn" | "queryKey">; |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * This component fetches then shows the name of a token. For ERC20 tokens, it calls the `name` function in the ERC20 contract. |
| 65 | + * It inherits all the attributes of a HTML <span> component, hence you can style it just like how you would style a normal <span> |
| 66 | + * |
| 67 | + * |
| 68 | + * @example |
| 69 | + * ### Basic usage |
| 70 | + * ```tsx |
| 71 | + * import { TokenProvider, TokenName } from "thirdweb/react"; |
| 72 | + * import { ethereum } from "thirdweb/chains"; |
| 73 | + * |
| 74 | + * <TokenProvider {...props}> |
| 75 | + * <TokenName /> |
| 76 | + * </TokenProvider> |
| 77 | + * ``` |
| 78 | + * Result: |
| 79 | + * ```html |
| 80 | + * <span>Ether</span> |
| 81 | + * ``` |
| 82 | + * |
| 83 | + * |
| 84 | + * ### Format the name (capitalize, truncate, etc.) |
| 85 | + * The TokenName component accepts a `formatFn` which takes in a string and outputs a string |
| 86 | + * The function is used to modify the name of the token |
| 87 | + * |
| 88 | + * ```tsx |
| 89 | + * const concatStr = (str: string):string => str + "Token" |
| 90 | + * |
| 91 | + * <TokenName formatFn={concatStr} /> |
| 92 | + * ``` |
| 93 | + * |
| 94 | + * Result: |
| 95 | + * ```html |
| 96 | + * <span>Ether Token</span> |
| 97 | + * ``` |
| 98 | + * |
| 99 | + * ### Show a loading sign when the name is being fetched |
| 100 | + * ```tsx |
| 101 | + * import { TokenProvider, TokenName } from "thirdweb/react"; |
| 102 | + * |
| 103 | + * <TokenProvider address="0x..."> |
| 104 | + * <TokenName loadingComponent={<Spinner />} /> |
| 105 | + * </TokenProvider> |
| 106 | + * ``` |
| 107 | + * |
| 108 | + * ### Fallback to something when the name fails to resolve |
| 109 | + * ```tsx |
| 110 | + * <TokenProvider address="0x..."> |
| 111 | + * <TokenName fallbackComponent={"Failed to load"} /> |
| 112 | + * </TokenProvider> |
| 113 | + * ``` |
| 114 | + * |
| 115 | + * ### Custom query options for useQuery |
| 116 | + * This component uses `@tanstack-query`'s useQuery internally. |
| 117 | + * You can use the `queryOptions` prop for more fine-grained control |
| 118 | + * ```tsx |
| 119 | + * <TokenName queryOption={{ |
| 120 | + * enabled: isEnabled, |
| 121 | + * retry: 4, |
| 122 | + * }} |
| 123 | + * /> |
| 124 | + * ``` |
| 125 | + * |
| 126 | + * @component |
| 127 | + * @token |
| 128 | + */ |
| 129 | +export function TokenName({ |
| 130 | + nameResolver, |
| 131 | + formatFn, |
| 132 | + loadingComponent, |
| 133 | + fallbackComponent, |
| 134 | + queryOptions, |
| 135 | + ...restProps |
| 136 | +}: TokenNameProps) { |
| 137 | + const { address, client, chain } = useTokenContext(); |
| 138 | + const nameQuery = useQuery({ |
| 139 | + queryKey: ["_internal_token_name_", chain.id, address] as const, |
| 140 | + queryFn: async () => { |
| 141 | + if (typeof nameResolver === "string") { |
| 142 | + return nameResolver; |
| 143 | + } |
| 144 | + if (typeof nameResolver === "function") { |
| 145 | + return nameResolver(); |
| 146 | + } |
| 147 | + if (address.toLowerCase() === NATIVE_TOKEN_ADDRESS.toLowerCase()) { |
| 148 | + // Don't wanna use `getChainNativeCurrencyName` because it has some side effect (it catches error and defaults to "ETH") |
| 149 | + return getChainMetadata(chain).then((data) => data.nativeCurrency.name); |
| 150 | + } |
| 151 | + return name({ contract: getContract({ address, client, chain }) }); |
| 152 | + }, |
| 153 | + ...queryOptions, |
| 154 | + }); |
| 155 | + |
| 156 | + if (nameQuery.isLoading) { |
| 157 | + return loadingComponent || null; |
| 158 | + } |
| 159 | + |
| 160 | + if (!nameQuery.data) { |
| 161 | + return fallbackComponent || null; |
| 162 | + } |
| 163 | + |
| 164 | + const displayValue = formatFn |
| 165 | + ? formatFn(Number(nameQuery.data)) |
| 166 | + : nameQuery.data; |
| 167 | + |
| 168 | + return <span {...restProps}>{displayValue}</span>; |
| 169 | +} |
0 commit comments