|
| 1 | +import groq from 'groq'; |
| 2 | +import { notFound } from 'next/navigation'; |
| 3 | +import { getTranslations } from 'next-intl/server'; |
| 4 | +import { client } from '@/client'; |
| 5 | +import { BlogCategoryGrid } from '@/components/blog/BlogCategoryGrid'; |
| 6 | + |
| 7 | +export const revalidate = 0; |
| 8 | + |
| 9 | +type Author = { |
| 10 | + name?: string; |
| 11 | + image?: string; |
| 12 | +}; |
| 13 | + |
| 14 | +type Post = { |
| 15 | + _id: string; |
| 16 | + title: string; |
| 17 | + slug: { current: string }; |
| 18 | + publishedAt?: string; |
| 19 | + categories?: string[]; |
| 20 | + mainImage?: string; |
| 21 | + body?: any[]; |
| 22 | + author?: Author; |
| 23 | +}; |
| 24 | + |
| 25 | +type Category = { |
| 26 | + _id: string; |
| 27 | + title: string; |
| 28 | +}; |
| 29 | + |
| 30 | +const categoriesQuery = groq` |
| 31 | + *[_type == "category"] | order(orderRank asc) { |
| 32 | + _id, |
| 33 | + title |
| 34 | + } |
| 35 | +`; |
| 36 | + |
| 37 | +export default async function BlogCategoryPage({ |
| 38 | + params, |
| 39 | +}: { |
| 40 | + params: Promise<{ locale: string; category: string }>; |
| 41 | +}) { |
| 42 | + const { locale, category } = await params; |
| 43 | + const t = await getTranslations({ locale, namespace: 'blog' }); |
| 44 | + const categoryKey = String(category || '').toLowerCase(); |
| 45 | + const categories: Category[] = await client |
| 46 | + .withConfig({ useCdn: false }) |
| 47 | + .fetch(categoriesQuery); |
| 48 | + const matchedCategory = categories.find( |
| 49 | + item => slugify(item.title) === categoryKey |
| 50 | + ); |
| 51 | + |
| 52 | + if (!matchedCategory) return notFound(); |
| 53 | + const categoryTitle = matchedCategory.title; |
| 54 | + const displayTitle = |
| 55 | + categoryTitle === 'Growth' ? 'Career' : categoryTitle; |
| 56 | + |
| 57 | + const posts: Post[] = await client.withConfig({ useCdn: false }).fetch( |
| 58 | + groq` |
| 59 | + *[_type == "post" && defined(slug.current) && $category in categories[]->title] |
| 60 | + | order(publishedAt desc) { |
| 61 | + _id, |
| 62 | + "title": coalesce(title[$locale], title[lower($locale)], title.uk, title.en, title.pl, title), |
| 63 | + slug, |
| 64 | + publishedAt, |
| 65 | + "categories": categories[]->title, |
| 66 | + "body": coalesce(body[$locale], body[lower($locale)], body.uk, body.en, body.pl, body)[]{ |
| 67 | + ..., |
| 68 | + children[]{ text } |
| 69 | + }, |
| 70 | + "mainImage": mainImage.asset->url, |
| 71 | + "author": author->{ |
| 72 | + "name": coalesce(name[$locale], name[lower($locale)], name.uk, name.en, name.pl, name), |
| 73 | + "image": image.asset->url |
| 74 | + } |
| 75 | + } |
| 76 | + `, |
| 77 | + { locale, category: categoryTitle } |
| 78 | + ); |
| 79 | + |
| 80 | + return ( |
| 81 | + <main className="max-w-6xl mx-auto px-6 py-12"> |
| 82 | + <h1 className="text-4xl font-bold mb-4 text-center"> |
| 83 | + {displayTitle} |
| 84 | + </h1> |
| 85 | + <div className="mt-12"> |
| 86 | + <BlogCategoryGrid posts={posts} /> |
| 87 | + </div> |
| 88 | + {!posts.length && ( |
| 89 | + <p className="text-center text-gray-500 mt-10">{t('noPosts')}</p> |
| 90 | + )} |
| 91 | + </main> |
| 92 | + ); |
| 93 | +} |
| 94 | + |
| 95 | +function slugify(value: string) { |
| 96 | + return value |
| 97 | + .toLowerCase() |
| 98 | + .trim() |
| 99 | + .replace(/[^a-z0-9\s-]/g, '') |
| 100 | + .replace(/\s+/g, '-'); |
| 101 | +} |
0 commit comments