mirror of
https://github.com/mastermindzh/rickvanlieshout.com
synced 2025-08-03 16:13:01 +02:00
refactor(starter): upgrade and move to typescript
This commit is contained in:
62
src/components/Image/Image.tsx
Normal file
62
src/components/Image/Image.tsx
Normal file
@@ -0,0 +1,62 @@
|
||||
import React, { FC } from "react";
|
||||
|
||||
import { graphql, StaticQuery } from "gatsby";
|
||||
import {
|
||||
GatsbyImage,
|
||||
GatsbyImageProps,
|
||||
IGatsbyImageData,
|
||||
} from "gatsby-plugin-image";
|
||||
import { FileSystemNode } from "gatsby-source-filesystem";
|
||||
|
||||
interface Props extends Omit<GatsbyImageProps, "image"> {
|
||||
path: string;
|
||||
}
|
||||
|
||||
interface Data {
|
||||
images: {
|
||||
edges: Array<{
|
||||
node: FileSystemNode & {
|
||||
childImageSharp: {
|
||||
gatsbyImageData: IGatsbyImageData;
|
||||
};
|
||||
};
|
||||
}>;
|
||||
};
|
||||
}
|
||||
|
||||
const Image: FC<Props> = ({ path, ...rest }: Props) => (
|
||||
<StaticQuery
|
||||
query={graphql`
|
||||
query {
|
||||
images: allFile(
|
||||
filter: { ext: { regex: "/png|jpg|jpeg|webp|tif|tiff/" } }
|
||||
) {
|
||||
edges {
|
||||
node {
|
||||
absolutePath
|
||||
childImageSharp {
|
||||
gatsbyImageData(formats: [AUTO, WEBP, AVIF])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`}
|
||||
render={(data: Data) => {
|
||||
const { images: { edges = [] } = {} } = data;
|
||||
const image = edges.find(({ node }) => node.absolutePath.includes(path));
|
||||
|
||||
if (!image) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const {
|
||||
node: { childImageSharp },
|
||||
} = image;
|
||||
|
||||
return <GatsbyImage {...rest} image={childImageSharp.gatsbyImageData} />;
|
||||
}}
|
||||
/>
|
||||
);
|
||||
|
||||
export default Image;
|
1
src/components/Image/index.ts
Normal file
1
src/components/Image/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { default as Image } from "./Image";
|
Reference in New Issue
Block a user