Skip to content

Commit

Permalink
feat(Posts): add other blog posts section to post page
Browse files Browse the repository at this point in the history
  • Loading branch information
emiliosheinz committed Mar 9, 2024
1 parent 1801f93 commit 1b91d5d
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 16 deletions.
16 changes: 1 addition & 15 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,7 @@ import { currentExperience } from '~/content/experiences'
import { getLastFivePosts } from '~/content/posts'
import { Image } from '~/components/image'
import { CommandBarTriggerFull } from '~/components/command-bar'

type SectionProps = {
id: string
title: string
children: React.ReactNode
}

function Section({ id, title, children }: SectionProps) {
return (
<div className='flex flex-col space-y-6 sm:space-y-8' id={id}>
<h1 className='font-bold text-2xl sm:text-3xl'>{title}</h1>
{children}
</div>
)
}
import { Section } from '~/components/section'

export default function HomePage() {
return (
Expand Down
21 changes: 20 additions & 1 deletion src/app/posts/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { Metadata, ResolvingMetadata } from 'next'
import { Image } from '~/components/image'
import { MDXContent } from '~/components/mdx-content'
import { getPostBySlug, posts } from '~/content/posts'
import { PostCard } from '~/components/post-card'
import { Section } from '~/components/section'
import { Slider } from '~/components/slider'
import { getPostBySlug, getRandomPosts, posts } from '~/content/posts'

type PostPageProps = {
params: {
Expand Down Expand Up @@ -65,6 +68,22 @@ export default function PostPage({ params }: PostPageProps) {
/>
</div>
<MDXContent code={post.body.code} />
<Section title='Other Blog Posts' id='blog'>
<Slider.Root>
{getRandomPosts({ count: 3, excludeSlug: post.slug }).map(
({ title, url, image, description }) => (
<Slider.Item key={title}>
<PostCard
url={url}
image={image}
title={title}
description={description}
/>
</Slider.Item>
)
)}
</Slider.Root>
</Section>
</>
)
}
1 change: 1 addition & 0 deletions src/components/section/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Section } from './section.component'
10 changes: 10 additions & 0 deletions src/components/section/section.component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { SectionProps } from './section.typs'

export function Section({ id, title, children }: SectionProps) {
return (
<div className='flex flex-col space-y-6 sm:space-y-8' id={id}>
<h1 className='font-bold text-2xl sm:text-3xl'>{title}</h1>
{children}
</div>
)
}
5 changes: 5 additions & 0 deletions src/components/section/section.typs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type SectionProps = {
id: string
title: string
children: React.ReactNode
}
11 changes: 11 additions & 0 deletions src/content/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,14 @@ export function getLastFivePosts(): Post[] {
export function getPostBySlug(slug: string): Post | undefined {
return posts.find(post => post.slug === slug)
}

type GetRandomPostsParams = {
excludeSlug: string
count: number
}

export function getRandomPosts({ excludeSlug, count }: GetRandomPostsParams) {
const filteredPosts = posts.filter(post => post.slug !== excludeSlug)
const randomPosts = filteredPosts.sort(() => 0.5 - Math.random())
return randomPosts.slice(0, count)
}

0 comments on commit 1b91d5d

Please sign in to comment.