Skip to content

Commit

Permalink
Fix SVG icons (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrederikBolding authored Dec 20, 2023
1 parent 0395aa6 commit e01126d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
5 changes: 2 additions & 3 deletions src/components/Chain.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
Box,
Button,
Center,
Flex,
Expand All @@ -12,7 +11,7 @@ import {
import React, { useContext } from "react";
import { Web3Context } from "../context/Web3Context";
import { ChainData } from "../types/chain";
import { GatsbyImage } from "gatsby-plugin-image";
import { ChainIcon } from "./ChainIcon";

export const Chain = ({
name,
Expand Down Expand Up @@ -61,7 +60,7 @@ export const Chain = ({
</Flex>
{icon && (
<Flex>
<GatsbyImage objectFit="scale-down" image={icon} alt={name} />
<ChainIcon name={name} icon={icon} />
</Flex>
)}
</Flex>
Expand Down
11 changes: 11 additions & 0 deletions src/components/ChainIcon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from "react";
import { GatsbyImage } from "gatsby-plugin-image";
import { ChainData } from "../types/chain";
import { Image } from "@chakra-ui/react";

export const ChainIcon = ({ icon, name }: Pick<ChainData, "icon" | "name">) =>
typeof icon === "string" ? (
<Image src={icon} width="40px" />
) : (
<GatsbyImage objectFit="scale-down" image={icon} alt={name} />
);
15 changes: 14 additions & 1 deletion src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ const IndexPage = () => {
}
}
}
allFile(filter: { extension: { eq: "svg" } }) {
nodes {
id
name
extension
publicURL
}
}
}
`);

Expand All @@ -56,8 +64,13 @@ const IndexPage = () => {
return acc;
}, {});

const svgIcons = rawData.allFile.nodes.reduce((acc, node) => {
acc[node.name] = node.publicURL;
return acc;
}, {});

const chains = rawChains.reduce((acc, chain, idx) => {
acc[idx].icon = icons[chain.icon];
acc[idx].icon = icons[chain.icon] ?? svgIcons[chain.icon];
return acc;
}, rawChains);

Expand Down
4 changes: 3 additions & 1 deletion src/types/chain.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { IGatsbyImageData } from "gatsby-plugin-image";

export interface ChainData {
name: string;
chain: string;
icon: string;
icon: string | IGatsbyImageData;
rpc: string[];
chainId: number;
nativeCurrency: ChainCurrency;
Expand Down

0 comments on commit e01126d

Please sign in to comment.