otterscan/src/components/TokenLogo.tsx

34 lines
1.0 KiB
TypeScript
Raw Normal View History

2021-11-26 08:13:23 +00:00
import React, { useContext } from "react";
2021-07-01 18:21:40 +00:00
import { useImage } from "react-image";
2021-11-26 08:13:23 +00:00
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faCoins } from "@fortawesome/free-solid-svg-icons/faCoins";
2021-07-09 16:54:59 +00:00
import { tokenLogoURL } from "../url";
2021-07-09 17:22:25 +00:00
import { RuntimeContext } from "../useRuntime";
2021-11-26 08:13:23 +00:00
import { ChecksummedAddress } from "../types";
2021-07-01 18:21:40 +00:00
type TokenLogoProps = {
2021-11-26 08:13:23 +00:00
address: ChecksummedAddress;
2021-07-01 18:21:40 +00:00
name: string;
};
2021-11-26 08:13:23 +00:00
const TokenLogo: React.FC<TokenLogoProps> = ({ address, name }) => {
2021-07-09 17:22:25 +00:00
const { config } = useContext(RuntimeContext);
2021-07-09 16:54:59 +00:00
const srcList: string[] = [];
if (config) {
2021-07-09 17:13:31 +00:00
srcList.push(tokenLogoURL(config.assetsURLPrefix ?? "", address));
2021-07-09 16:54:59 +00:00
}
2021-11-26 08:13:23 +00:00
const { src, isLoading } = useImage({ srcList, useSuspense: false });
2021-07-01 18:21:40 +00:00
return (
2021-11-26 08:13:23 +00:00
<div className="flex items-center justify-center text-gray-400 w-5 h-5">
{src && (
<img className="max-w-full max-h-full" src={src} alt={`${name} logo`} />
)}
{!src && !isLoading && <FontAwesomeIcon icon={faCoins} size="1x" />}
2021-07-01 18:21:40 +00:00
</div>
);
};
export default React.memo(TokenLogo);