---
import { getCollection } from 'astro:content'
import PostList from '../../components/PostList.astro'
import PageWithTitle from '../../layouts/PageWithTitle.astro'

export async function getStaticPaths() {
  const posts = await getCollection('blog')
  const tags = posts.reduce((set, cur) => {
    for (const tag of cur.data.tags) {
      set.add(tag)
    }
    return set
  }, new Set<string>())

  return [...tags].map((tag) => ({
    params: { tag },
    props: { posts: posts.filter((post) => post.data.tags.includes(tag)) },
  }))
}

const params = Astro.params
const { posts } = Astro.props
---

<PageWithTitle title={params.tag}>
  <PostList posts={posts} />
</PageWithTitle>
