|
1 | 1 | import Generic from "cryptocurrency-icons/svg/color/generic.svg";
|
2 |
| -import type { ComponentProps } from "react"; |
| 2 | +import type { ComponentProps, ComponentType } from "react"; |
3 | 3 |
|
| 4 | +import Commodities from "./commodities.svg"; |
| 5 | +import CryptoIndex from "./crypto-index.svg"; |
| 6 | +import CryptoRedemptionRate from "./crypto-redemption-rate.svg"; |
| 7 | +import Crypto from "./crypto.svg"; |
| 8 | +import Equity from "./equity.svg"; |
| 9 | +import Fx from "./fx.svg"; |
4 | 10 | import { icons } from "./icons";
|
| 11 | +import styles from "./index.module.scss"; |
| 12 | +import Metal from "./metal.svg"; |
| 13 | +import Rates from "./rates.svg"; |
5 | 14 |
|
6 | 15 | type OwnProps = {
|
7 | 16 | assetClass: string;
|
8 | 17 | symbol: string;
|
9 | 18 | };
|
10 |
| -type Props = Omit< |
11 |
| - ComponentProps<typeof Generic>, |
12 |
| - keyof OwnProps | "width" | "height" | "viewBox" |
13 |
| -> & |
| 19 | +type Props = Omit<SVGProps, keyof OwnProps | "width" | "height" | "viewBox"> & |
14 | 20 | OwnProps;
|
15 | 21 |
|
16 | 22 | export const PriceFeedIcon = ({ assetClass, symbol, ...props }: Props) => {
|
17 |
| - const Icon = getIcon(assetClass, symbol); |
18 |
| - return <Icon width="100%" height="100%" viewBox="0 0 32 32" {...props} />; |
19 |
| -}; |
20 |
| - |
21 |
| -const getIcon = (assetClass: string, symbol: string) => { |
22 | 23 | if (assetClass === "Crypto") {
|
23 | 24 | const firstPart = symbol.split("/")[0];
|
24 |
| - return firstPart && firstPart in icons |
25 |
| - ? icons[firstPart as keyof typeof icons] |
26 |
| - : Generic; |
| 25 | + const Icon = firstPart ? (icons as SVGRecord)[firstPart] : undefined; |
| 26 | + return Icon ? ( |
| 27 | + <Icon width="100%" height="100%" viewBox="0 0 32 32" {...props} /> |
| 28 | + ) : ( |
| 29 | + <GenericIcon assetClass={assetClass} {...props} /> |
| 30 | + ); |
27 | 31 | } else {
|
28 |
| - return Generic; |
| 32 | + return <GenericIcon assetClass={assetClass} {...props} />; |
29 | 33 | }
|
30 | 34 | };
|
| 35 | + |
| 36 | +type GenericProps = ComponentProps<"svg"> & { assetClass: string }; |
| 37 | + |
| 38 | +const GenericIcon = ({ assetClass, ...props }: GenericProps) => { |
| 39 | + const Icon = ASSET_CLASS_TO_ICON[assetClass] ?? Generic; |
| 40 | + return ( |
| 41 | + <Icon |
| 42 | + width="100%" |
| 43 | + height="100%" |
| 44 | + className={styles.generic} |
| 45 | + data-asset-class={assetClass} |
| 46 | + {...(!(assetClass in ASSET_CLASS_TO_ICON) && { |
| 47 | + viewBox: "0 0 32 32", |
| 48 | + })} |
| 49 | + {...props} |
| 50 | + /> |
| 51 | + ); |
| 52 | +}; |
| 53 | + |
| 54 | +type SVGProps = ComponentProps<"svg">; |
| 55 | +type SVGComponent = ComponentType<SVGProps>; |
| 56 | +type SVGRecord = Record<string, SVGComponent>; |
| 57 | + |
| 58 | +const ASSET_CLASS_TO_ICON: Record<string, SVGComponent> = { |
| 59 | + Commodities, |
| 60 | + "Crypto Index": CryptoIndex, |
| 61 | + "Crypto Redemption Rate": CryptoRedemptionRate, |
| 62 | + Crypto, |
| 63 | + Equity, |
| 64 | + FX: Fx, |
| 65 | + Metal, |
| 66 | + Rates, |
| 67 | +}; |
0 commit comments