1
0
Fork 0
blog/src/components/molecules/RelatedPosts.tsx

103 lines
2.4 KiB
TypeScript
Raw Normal View History

2021-03-01 00:36:51 +01:00
import React, { ReactElement, useState } from 'react'
2019-10-02 13:35:50 +02:00
import { graphql, useStaticQuery } from 'gatsby'
2019-11-24 14:29:25 +01:00
import PostTeaser from './PostTeaser'
import * as styles from './RelatedPosts.module.css'
2021-02-28 22:34:52 +01:00
import { PhotoThumb } from '../templates/Photos'
2019-10-02 13:35:50 +02:00
const query = graphql`
query RelatedPosts {
allMarkdownRemark(sort: { fields: { date: DESC } }) {
2019-10-02 13:35:50 +02:00
edges {
node {
2019-11-24 14:29:25 +01:00
...PostTeaser
2019-10-02 13:35:50 +02:00
}
}
}
}
`
2019-10-16 01:45:30 +02:00
function postsWithDataFilter(
posts: Queries.RelatedPostsQuery['allMarkdownRemark']['edges'],
key: keyof Queries.MarkdownRemarkFrontmatter,
2019-10-16 01:45:30 +02:00
valuesToFind: string[]
) {
2021-03-01 01:06:23 +01:00
const newArray = posts
.filter(({ node }) => {
2021-03-01 01:06:23 +01:00
const frontmatterKey = node.frontmatter[key] as []
if (
frontmatterKey !== null &&
frontmatterKey.some((r: string) => valuesToFind.includes(r))
) {
return node
}
})
.sort(() => 0.5 - Math.random())
.slice(0, 6)
2019-10-02 13:35:50 +02:00
return newArray
}
function photosWithDataFilter(
posts: Queries.RelatedPostsQuery['allMarkdownRemark']['edges']
) {
2021-03-01 01:06:23 +01:00
const newArray = posts
.filter((post) => {
2021-03-01 01:06:23 +01:00
const { fileAbsolutePath } = post.node
if (fileAbsolutePath.includes('content/photos')) {
return post
}
})
.sort(() => 0.5 - Math.random())
.slice(0, 6)
2019-10-05 14:07:15 +02:00
return newArray
}
export default function RelatedPosts({
tags,
2020-07-19 13:31:27 +02:00
isPhotos
2019-10-05 14:07:15 +02:00
}: {
tags: string[]
2020-07-19 13:31:27 +02:00
isPhotos?: boolean
2020-05-22 14:38:19 +02:00
}): ReactElement {
const data = useStaticQuery<Queries.RelatedPostsQuery>(query)
2019-10-02 13:35:50 +02:00
const posts = data.allMarkdownRemark.edges
2019-10-05 14:07:15 +02:00
function getPosts() {
2020-07-19 13:31:27 +02:00
return isPhotos
2019-10-05 14:07:15 +02:00
? photosWithDataFilter(posts)
2019-11-16 16:10:34 +01:00
: tags && postsWithDataFilter(posts, 'tags', tags)
2019-10-05 14:07:15 +02:00
}
2021-03-01 01:06:23 +01:00
const [filteredPosts, setFilteredPosts] = useState(getPosts())
2019-10-02 20:48:59 +02:00
function refreshPosts() {
2021-02-28 22:34:52 +01:00
const newPosts = getPosts()
2021-03-01 01:06:23 +01:00
setFilteredPosts(newPosts)
2019-10-02 20:48:59 +02:00
}
2019-10-02 13:35:50 +02:00
return (
<aside className={styles.relatedPosts}>
<h1 className={styles.title}>
2020-07-19 13:31:27 +02:00
Related {isPhotos ? 'Photos' : 'Posts'}{' '}
<button className={styles.button} onClick={() => refreshPosts()}>
2019-11-11 01:00:26 +01:00
Refresh
</button>
</h1>
2019-10-02 13:35:50 +02:00
<ul>
{filteredPosts?.map(({ node }) => (
2021-02-28 22:34:52 +01:00
<li key={node.id}>
{isPhotos ? (
<PhotoThumb photo={node} />
) : (
<PostTeaser post={node} hideDate />
)}
</li>
))}
2019-10-02 13:35:50 +02:00
</ul>
</aside>
)
}