index.tsx 1.9 KB

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