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

72 lines
1.8 KiB
TypeScript
Raw Normal View History

2020-05-22 14:38:19 +02:00
import React, { ReactElement } from 'react'
2020-07-19 13:31:27 +02:00
import { graphql } from 'gatsby'
import { PageContext } from '../../@types/Post'
2023-01-29 22:58:19 +01:00
import HeadMeta, { HeadMetaProps } from '../atoms/HeadMeta'
2020-03-04 22:21:12 +01:00
import Pagination from '../molecules/Pagination'
2020-07-19 13:31:27 +02:00
import PostTeaser from '../molecules/PostTeaser'
import * as styles from './Archive.module.css'
2023-01-29 22:58:19 +01:00
import Page from './Page'
2022-11-19 16:09:13 +01:00
function getMetadata(pageContext: PageContext) {
const { tag, currentPageNumber, numPages } = pageContext
const title = tag ? `#${tag}` : 'Archive'
const paginationTitle =
numPages > 1 && currentPageNumber > 1
? `Page ${currentPageNumber} / ${numPages}`
: ''
const meta: Partial<HeadMetaProps> = {
title: `${title} ${paginationTitle}`,
description: 'All the articles & links.'
}
return meta
}
2018-07-17 23:33:55 +02:00
2020-07-08 01:31:03 +02:00
export default function Archive({
2019-10-02 13:35:50 +02:00
data,
pageContext
}: {
data: Queries.ArchiveTemplateQuery
2020-07-19 13:31:27 +02:00
pageContext: PageContext
2020-05-22 14:38:19 +02:00
}): ReactElement {
2018-07-17 23:33:55 +02:00
const edges = data.allMarkdownRemark.edges
2022-11-19 16:09:13 +01:00
const meta = getMetadata(pageContext)
2018-07-22 04:27:37 +02:00
const PostsList = edges.map(({ node }) => (
2020-07-19 13:31:27 +02:00
<PostTeaser key={node.id} post={node} />
))
2018-07-22 04:27:37 +02:00
2018-09-06 20:27:18 +02:00
return (
2022-11-19 16:09:13 +01:00
<Page title={meta.title}>
<div className={styles.posts}>{PostsList}</div>
2022-11-19 16:09:13 +01:00
{pageContext.numPages > 1 && <Pagination pageContext={pageContext} />}
2020-07-19 13:31:27 +02:00
</Page>
2018-09-06 20:27:18 +02:00
)
2018-07-17 23:33:55 +02:00
}
2022-11-19 16:09:13 +01:00
export function Head({ pageContext }: { pageContext: PageContext }) {
const meta = getMetadata(pageContext)
return <HeadMeta {...meta} slug={pageContext.slug} />
}
2020-07-08 01:31:03 +02:00
export const archiveQuery = graphql`
query ArchiveTemplate($tag: String, $skip: Int, $limit: Int) {
2018-09-04 02:57:39 +02:00
allMarkdownRemark(
2021-02-28 04:50:05 +01:00
filter: {
2021-03-16 00:13:01 +01:00
fields: { type: { nin: "photo" } }
2021-02-28 04:50:05 +01:00
frontmatter: { tags: { eq: $tag } }
}
sort: { fields: { date: DESC } }
2018-09-04 02:57:39 +02:00
skip: $skip
limit: $limit
) {
2018-07-17 23:33:55 +02:00
edges {
node {
2020-07-19 13:31:27 +02:00
...PostTeaser
2018-07-17 23:33:55 +02:00
}
}
}
}
`