-
-
Notifications
You must be signed in to change notification settings - Fork 4
Sanity #155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sanity #155
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import groq from 'groq'; | ||
| import { notFound } from 'next/navigation'; | ||
| import { getTranslations } from 'next-intl/server'; | ||
| import { client } from '@/client'; | ||
| import { BlogCategoryGrid } from '@/components/blog/BlogCategoryGrid'; | ||
|
|
||
| export const revalidate = 0; | ||
|
|
||
| type Author = { | ||
| name?: string; | ||
| image?: string; | ||
| }; | ||
|
|
||
| type Post = { | ||
| _id: string; | ||
| title: string; | ||
| slug: { current: string }; | ||
| publishedAt?: string; | ||
| categories?: string[]; | ||
| mainImage?: string; | ||
| body?: any[]; | ||
| author?: Author; | ||
| }; | ||
|
|
||
| type Category = { | ||
| _id: string; | ||
| title: string; | ||
| }; | ||
|
|
||
| const categoriesQuery = groq` | ||
| *[_type == "category"] | order(orderRank asc) { | ||
| _id, | ||
| title | ||
| } | ||
| `; | ||
|
|
||
| export default async function BlogCategoryPage({ | ||
| params, | ||
| }: { | ||
| params: Promise<{ locale: string; category: string }>; | ||
| }) { | ||
| const { locale, category } = await params; | ||
| const t = await getTranslations({ locale, namespace: 'blog' }); | ||
| const categoryKey = String(category || '').toLowerCase(); | ||
| const categories: Category[] = await client | ||
| .withConfig({ useCdn: false }) | ||
| .fetch(categoriesQuery); | ||
| const matchedCategory = categories.find( | ||
| item => slugify(item.title) === categoryKey | ||
| ); | ||
|
|
||
| if (!matchedCategory) return notFound(); | ||
| const categoryTitle = matchedCategory.title; | ||
| const displayTitle = | ||
| categoryTitle === 'Growth' ? 'Career' : categoryTitle; | ||
|
|
||
| const posts: Post[] = await client.withConfig({ useCdn: false }).fetch( | ||
| groq` | ||
| *[_type == "post" && defined(slug.current) && $category in categories[]->title] | ||
| | order(publishedAt desc) { | ||
| _id, | ||
| "title": coalesce(title[$locale], title[lower($locale)], title.uk, title.en, title.pl, title), | ||
| slug, | ||
| publishedAt, | ||
| "categories": categories[]->title, | ||
| "body": coalesce(body[$locale], body[lower($locale)], body.uk, body.en, body.pl, body)[]{ | ||
| ..., | ||
| children[]{ text } | ||
| }, | ||
| "mainImage": mainImage.asset->url, | ||
| "author": author->{ | ||
| "name": coalesce(name[$locale], name[lower($locale)], name.uk, name.en, name.pl, name), | ||
| "image": image.asset->url | ||
| } | ||
| } | ||
| `, | ||
| { locale, category: categoryTitle } | ||
| ); | ||
|
|
||
| return ( | ||
| <main className="max-w-6xl mx-auto px-6 py-12"> | ||
| <h1 className="text-4xl font-bold mb-4 text-center"> | ||
| {displayTitle} | ||
| </h1> | ||
| <div className="mt-12"> | ||
| <BlogCategoryGrid posts={posts} /> | ||
| </div> | ||
| {!posts.length && ( | ||
| <p className="text-center text-gray-500 mt-10">{t('noPosts')}</p> | ||
| )} | ||
| </main> | ||
| ); | ||
| } | ||
|
|
||
| function slugify(value: string) { | ||
| return value | ||
| .toLowerCase() | ||
| .trim() | ||
| .replace(/[^a-z0-9\s-]/g, '') | ||
| .replace(/\s+/g, '-'); | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,10 @@ | ||||||||||||||||
| 'use client'; | ||||||||||||||||
|
|
||||||||||||||||
| import BlogGrid from '@/components/blog/BlogGrid'; | ||||||||||||||||
| import type { Post } from '@/components/blog/BlogFilters'; | ||||||||||||||||
|
|
||||||||||||||||
| export function BlogCategoryGrid({ posts }: { posts: Post[] }) { | ||||||||||||||||
| if (!posts.length) return null; | ||||||||||||||||
|
|
||||||||||||||||
| return <BlogGrid posts={posts} onAuthorSelect={() => {}} />; | ||||||||||||||||
|
Comment on lines
+6
to
+9
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inconsistent empty-state handling compared to
Consider delegating to 💡 Suggested fix export function BlogCategoryGrid({ posts }: { posts: Post[] }) {
- if (!posts.length) return null;
-
return <BlogGrid posts={posts} onAuthorSelect={() => {}} />;
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||
| } | ||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| 'use client'; | ||
|
|
||
| import { Link, usePathname } from '@/i18n/routing'; | ||
| import { cn } from '@/lib/utils'; | ||
|
|
||
| type Category = { | ||
| _id: string; | ||
| title: string; | ||
| }; | ||
|
|
||
| type BlogCategoryLinksProps = { | ||
| categories: Category[]; | ||
| className?: string; | ||
| linkClassName?: string; | ||
| onNavigate?: () => void; | ||
| }; | ||
|
|
||
| export function BlogCategoryLinks({ | ||
| categories, | ||
| className, | ||
| linkClassName, | ||
| onNavigate, | ||
| }: BlogCategoryLinksProps) { | ||
| const pathname = usePathname(); | ||
| const baseLink = | ||
| linkClassName || | ||
| 'rounded-md px-3 py-2 text-sm font-medium transition-colors ' + | ||
| 'hover:bg-secondary hover:text-foreground ' + | ||
| 'focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring ' + | ||
| 'focus-visible:ring-offset-2 focus-visible:ring-offset-background'; | ||
|
|
||
| const items = categories | ||
| .map(category => ({ | ||
| ...category, | ||
| slug: slugify(category.title || ''), | ||
| displayTitle: category.title === 'Growth' ? 'Career' : category.title, | ||
| })) | ||
| .filter(category => category.slug); | ||
|
|
||
| return ( | ||
| <nav className={cn('flex items-center gap-1', className)} aria-label="Blog categories"> | ||
| {items.map(category => { | ||
| const href = `/blog/category/${category.slug}`; | ||
| const isActive = pathname === href; | ||
| return ( | ||
| <Link | ||
| key={category._id} | ||
| href={href} | ||
| onClick={onNavigate} | ||
| aria-current={isActive ? 'page' : undefined} | ||
| className={cn( | ||
| baseLink, | ||
| isActive ? 'bg-muted text-foreground' : 'text-muted-foreground' | ||
| )} | ||
| > | ||
| {category.displayTitle} | ||
| </Link> | ||
| ); | ||
| })} | ||
| <Link | ||
| href="/" | ||
| onClick={onNavigate} | ||
| aria-current={pathname === '/' ? 'page' : undefined} | ||
| className={cn( | ||
| baseLink, | ||
| pathname === '/' ? 'bg-muted text-foreground' : 'text-muted-foreground' | ||
| )} | ||
| > | ||
| Home | ||
| </Link> | ||
| </nav> | ||
| ); | ||
| } | ||
|
|
||
| function slugify(value: string) { | ||
| return value | ||
| .toLowerCase() | ||
| .trim() | ||
| .replace(/[^a-z0-9\s-]/g, '') | ||
| .replace(/\s+/g, '-'); | ||
| } | ||
|
Comment on lines
+75
to
+81
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Duplicate There's already a more robust
This local implementation is simpler but may produce inconsistent slugs for edge cases (e.g., accented characters). ♻️ Suggested fix 'use client';
import { Link, usePathname } from '@/i18n/routing';
import { cn } from '@/lib/utils';
+import { slugify } from '@/lib/shop/slug';
// ... rest of component ...
-function slugify(value: string) {
- return value
- .toLowerCase()
- .trim()
- .replace(/[^a-z0-9\s-]/g, '')
- .replace(/\s+/g, '-');
-}🤖 Prompt for AI Agents |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Category fetch lacks error handling.
If the Sanity client fetch fails, this will throw and crash the layout. Consider wrapping in try-catch with a fallback to an empty array:
This ensures the layout remains functional even if the CMS is temporarily unavailable.
🤖 Prompt for AI Agents