|
| 1 | +import { type UseQueryOptions, useQuery } from "@tanstack/react-query"; |
| 2 | +import type { JSX } from "react"; |
| 3 | +import { getChainMetadata } from "../../../../../chains/utils.js"; |
| 4 | +import type { ThirdwebClient } from "../../../../../client/client.js"; |
| 5 | +import { resolveScheme } from "../../../../../utils/ipfs.js"; |
| 6 | +import { useChainContext } from "./provider.js"; |
| 7 | + |
| 8 | +/** |
| 9 | + * Props for the ChainIcon component |
| 10 | + * @chain |
| 11 | + * @component |
| 12 | + */ |
| 13 | +export interface ChainIconProps |
| 14 | + extends Omit<React.ImgHTMLAttributes<HTMLImageElement>, "src"> { |
| 15 | + /** |
| 16 | + * You need a ThirdwebClient to resolve the icon which is hosted on IPFS. |
| 17 | + * (since most chain icons are hosted on IPFS, loading them via thirdweb gateway will ensure better performance) |
| 18 | + */ |
| 19 | + client: ThirdwebClient; |
| 20 | + /** |
| 21 | + * This prop can be a string or a (async) function that resolves to a string, representing the icon url of the chain |
| 22 | + * This is particularly useful if you already have a way to fetch the chain icon. |
| 23 | + */ |
| 24 | + iconResolver?: string | (() => string) | (() => Promise<string>); |
| 25 | + /** |
| 26 | + * This component will be shown while the avatar of the icon is being fetched |
| 27 | + * If not passed, the component will return `null`. |
| 28 | + * |
| 29 | + * You can pass a loading sign or spinner to this prop. |
| 30 | + * @example |
| 31 | + * ```tsx |
| 32 | + * <ChainIcon loadingComponent={<Spinner />} /> |
| 33 | + * ``` |
| 34 | + */ |
| 35 | + loadingComponent?: JSX.Element; |
| 36 | + /** |
| 37 | + * This component will be shown if the request for fetching the avatar is done |
| 38 | + * but could not retreive any result. |
| 39 | + * You can pass a dummy avatar/image to this prop. |
| 40 | + * |
| 41 | + * If not passed, the component will return `null` |
| 42 | + * |
| 43 | + * @example |
| 44 | + * ```tsx |
| 45 | + * <ChainIcon fallbackComponent={<DummyImage />} /> |
| 46 | + * ``` |
| 47 | + */ |
| 48 | + fallbackComponent?: JSX.Element; |
| 49 | + |
| 50 | + /** |
| 51 | + * Optional query options for `useQuery` |
| 52 | + */ |
| 53 | + queryOptions?: Omit<UseQueryOptions<string>, "queryFn" | "queryKey">; |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * This component tries to resolve the icon of a given chain, then return an image. |
| 58 | + * @returns an <img /> with the src of the chain icon |
| 59 | + * |
| 60 | + * @example |
| 61 | + * ### Basic usage |
| 62 | + * ```tsx |
| 63 | + * import { ChainProvider, ChainIcon } from "thirdweb/react"; |
| 64 | + * |
| 65 | + * <ChainProvider chain={chain}> |
| 66 | + * <ChainIcon /> |
| 67 | + * </ChainProvider> |
| 68 | + * ``` |
| 69 | + * |
| 70 | + * Result: An <img /> component with the src of the icon |
| 71 | + * ```html |
| 72 | + * <img src="chain-icon.png" /> |
| 73 | + * ``` |
| 74 | + * |
| 75 | + * ### Override the icon with the `iconResolver` prop |
| 76 | + * If you already have the icon url, you can skip the network requests and pass it directly to the ChainIcon |
| 77 | + * ```tsx |
| 78 | + * <ChainIcon iconResolver="/ethereum-icon.png" /> |
| 79 | + * ``` |
| 80 | + * |
| 81 | + * You can also pass in your own custom (async) function that retrieves the icon url |
| 82 | + * ```tsx |
| 83 | + * const getIcon = async () => { |
| 84 | + * const icon = getIconFromCoinMarketCap(chainId, etc); |
| 85 | + * return icon; |
| 86 | + * }; |
| 87 | + * |
| 88 | + * <ChainIcon iconResolver={getIcon} /> |
| 89 | + * ``` |
| 90 | + * |
| 91 | + * ### Show a loading sign while the icon is being loaded |
| 92 | + * ```tsx |
| 93 | + * <ChainIcon loadingComponent={<Spinner />} /> |
| 94 | + * ``` |
| 95 | + * |
| 96 | + * ### Fallback to a dummy image if the chain icon fails to resolve |
| 97 | + * ```tsx |
| 98 | + * <ChainIcon fallbackComponent={<img src="blank-image.png" />} /> |
| 99 | + * ``` |
| 100 | + * |
| 101 | + * ### Usage with queryOptions |
| 102 | + * ChainIcon uses useQuery() from tanstack query internally. |
| 103 | + * It allows you to pass a custom queryOptions of your choice for more control of the internal fetching logic |
| 104 | + * ```tsx |
| 105 | + * <ChainIcon queryOptions={{ enabled: someLogic, retry: 3, }} /> |
| 106 | + * ``` |
| 107 | + * |
| 108 | + * @component |
| 109 | + * @chain |
| 110 | + * @beta |
| 111 | + */ |
| 112 | +export function ChainIcon({ |
| 113 | + iconResolver, |
| 114 | + loadingComponent, |
| 115 | + fallbackComponent, |
| 116 | + queryOptions, |
| 117 | + client, |
| 118 | + ...restProps |
| 119 | +}: ChainIconProps) { |
| 120 | + const { chain } = useChainContext(); |
| 121 | + const iconQuery = useQuery({ |
| 122 | + queryKey: ["_internal_chain_icon_", chain.id] as const, |
| 123 | + queryFn: async () => { |
| 124 | + if (typeof iconResolver === "string") { |
| 125 | + return iconResolver; |
| 126 | + } |
| 127 | + if (typeof iconResolver === "function") { |
| 128 | + return iconResolver(); |
| 129 | + } |
| 130 | + // Check if the chain object already has "icon" |
| 131 | + if (chain.icon?.url) { |
| 132 | + return chain.icon.url; |
| 133 | + } |
| 134 | + const possibleUrl = await getChainMetadata(chain).then( |
| 135 | + (data) => data.icon?.url, |
| 136 | + ); |
| 137 | + if (!possibleUrl) { |
| 138 | + throw new Error("Failed to resolve icon for chain"); |
| 139 | + } |
| 140 | + return resolveScheme({ uri: possibleUrl, client }); |
| 141 | + }, |
| 142 | + ...queryOptions, |
| 143 | + }); |
| 144 | + |
| 145 | + if (iconQuery.isLoading) { |
| 146 | + return loadingComponent || null; |
| 147 | + } |
| 148 | + |
| 149 | + if (!iconQuery.data) { |
| 150 | + return fallbackComponent || null; |
| 151 | + } |
| 152 | + |
| 153 | + return <img src={iconQuery.data} {...restProps} alt={restProps.alt} />; |
| 154 | +} |
0 commit comments