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

108 lines
2.6 KiB
TypeScript
Raw Normal View History

2020-07-19 13:31:27 +02:00
import React, { ReactElement } from 'react'
2023-01-29 22:58:19 +01:00
import { Link, PageProps, graphql } from 'gatsby'
import { PageContext } from '../../@types/Post'
2023-01-29 22:58:19 +01:00
import HeadMeta, { HeadMetaProps } from '../atoms/HeadMeta'
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}`
: ''
2022-11-19 16:09:13 +01:00
const meta: Partial<HeadMetaProps> = {
title: `Photos ${paginationTitle}`,
description: 'Personal photos of designer & developer Matthias Kretschmann.'
2020-07-19 13:31:27 +02:00
}
2022-11-19 16:09:13 +01:00
return meta
2021-03-01 01:06:23 +01:00
}
2022-11-19 16:09:13 +01:00
export default function Photos({
data,
pageContext
}: PhotosPageProps): ReactElement {
const photos = data.allMarkdownRemark.edges
const { currentPageNumber, numPages } = pageContext
const meta = getMetadata(currentPageNumber, numPages)
2020-07-19 13:31:27 +02:00
return (
2022-11-19 16:09:13 +01:00
<Page title={meta.title}>
<section className={styles.photos}>
{photos.map(({ node }) => (
2020-07-19 13:31:27 +02:00
<PhotoThumb key={node.id} photo={node} />
))}
</section>
2022-11-19 16:09:13 +01:00
{numPages > 1 && <Pagination pageContext={pageContext} />}
2020-07-19 13:31:27 +02:00
</Page>
)
}
2022-11-19 16:09:13 +01:00
export function Head({
pageContext
}: {
pageContext: PhotosPageProps['pageContext']
}) {
const { currentPageNumber, numPages } = pageContext
const meta = getMetadata(currentPageNumber, numPages)
return <HeadMeta {...meta} slug={pageContext.slug} />
}
2020-07-19 13:31:27 +02:00
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
}
}
}
}
}
`