1
0
Fork 0
blog/src/components/templates/Photos.tsx

99 lines
2.4 KiB
TypeScript
Raw Normal View History

2020-07-19 13:31:27 +02:00
import React, { ReactElement } from 'react'
2021-03-01 00:36:51 +01:00
import { graphql, Link, PageProps } from 'gatsby'
import { PageContext } from '../../@types/Post'
2020-07-19 13:31:27 +02:00
import { Image } from '../atoms/Image'
import Pagination from '../molecules/Pagination'
2021-03-14 01:34:04 +01:00
import Page from './Page'
import * as styles from './Photos.module.css'
2020-07-19 13:31:27 +02:00
export const PhotoThumb = ({
photo
}: {
photo: Queries.PhotosTemplateQuery['allMarkdownRemark']['edges'][0]['node']
}): ReactElement => {
2020-07-19 13:31:27 +02:00
const { title, image } = photo.frontmatter
const { slug } = photo.fields
2021-03-13 04:11:44 +01:00
const { gatsbyImageData } = (image as any).childImageSharp
2020-07-19 13:31:27 +02:00
return (
<article className={styles.photo}>
2020-07-19 13:31:27 +02:00
{image && (
<Link to={slug}>
2021-03-13 04:11:44 +01:00
<Image title={title} image={gatsbyImageData} alt={title} />
2020-07-19 13:31:27 +02:00
</Link>
)}
</article>
)
}
2021-03-01 00:36:51 +01:00
interface PhotosPageProps extends PageProps {
data: Queries.PhotosTemplateQuery
2020-07-19 13:31:27 +02:00
pageContext: PageContext
2021-03-01 00:36:51 +01:00
}
2021-03-01 01:06:23 +01:00
function getMetadata(currentPageNumber: number, numPages: number) {
2020-07-19 13:31:27 +02:00
const paginationTitle =
numPages > 1 && currentPageNumber > 1
? `Page ${currentPageNumber} / ${numPages}`
: ''
2021-03-01 01:06:23 +01:00
return {
2020-07-19 13:31:27 +02:00
frontmatter: {
title: `Photos ${paginationTitle}`,
description:
'Personal photos of designer & developer Matthias Kretschmann.'
}
}
2021-03-01 01:06:23 +01:00
}
export default function Photos(props: PhotosPageProps): ReactElement {
const photos = props.data.allMarkdownRemark.edges
const { currentPageNumber, numPages } = props.pageContext
const page = getMetadata(currentPageNumber, numPages)
2020-07-19 13:31:27 +02:00
return (
2021-03-01 00:36:51 +01:00
<Page
title={page.frontmatter.title}
post={page}
pathname={props.location.pathname}
>
<section className={styles.photos}>
{photos.map(({ node }) => (
2020-07-19 13:31:27 +02:00
<PhotoThumb key={node.id} photo={node} />
))}
</section>
2021-03-01 00:36:51 +01:00
{numPages > 1 && <Pagination pageContext={props.pageContext} />}
2020-07-19 13:31:27 +02:00
</Page>
)
}
export const photosQuery = graphql`
query PhotosTemplate($skip: Int, $limit: Int) {
2020-07-19 13:31:27 +02:00
allMarkdownRemark(
2021-02-28 04:50:05 +01:00
filter: { fields: { type: { eq: "photo" } } }
sort: { fields: { date: DESC } }
2020-07-19 13:31:27 +02:00
skip: $skip
limit: $limit
) {
edges {
node {
id
frontmatter {
title
image {
childImageSharp {
...PhotoFluidThumb
}
}
}
fields {
slug
2021-03-01 00:36:51 +01:00
type
2020-07-19 13:31:27 +02:00
}
}
}
}
}
`