|
1 | 1 | --- |
2 | 2 | import PostCard from './PostCard.astro'; |
3 | | -import ReadNextCard from './ReadNextCard.astro'; |
4 | 3 | import type { CollectionEntry } from 'astro:content'; |
5 | 4 |
|
6 | 5 | interface Props { |
7 | 6 | currentSlug: string; |
8 | | - tags: string[]; |
9 | 7 | relatedPosts: CollectionEntry<'posts'>[]; |
10 | 8 | prevPost?: CollectionEntry<'posts'> | null; |
11 | 9 | nextPost?: CollectionEntry<'posts'> | null; |
12 | 10 | } |
13 | 11 |
|
14 | | -const { currentSlug, tags, relatedPosts, prevPost, nextPost } = Astro.props; |
15 | | -const showRelatedPosts = relatedPosts.length > 1; |
16 | | -const hasContent = showRelatedPosts || prevPost || nextPost; |
| 12 | +const { currentSlug, relatedPosts, prevPost, nextPost } = Astro.props; |
| 13 | +
|
| 14 | +const MAX_CARDS = 3; |
| 15 | +const seen = new Set<string>([currentSlug]); |
| 16 | +const cards: CollectionEntry<'posts'>[] = []; |
| 17 | +
|
| 18 | +for (const post of relatedPosts) { |
| 19 | + if (cards.length >= MAX_CARDS) break; |
| 20 | + if (!seen.has(post.id)) { |
| 21 | + seen.add(post.id); |
| 22 | + cards.push(post); |
| 23 | + } |
| 24 | +} |
| 25 | +
|
| 26 | +for (const post of [prevPost, nextPost]) { |
| 27 | + if (cards.length >= MAX_CARDS) break; |
| 28 | + if (post && !seen.has(post.id)) { |
| 29 | + seen.add(post.id); |
| 30 | + cards.push(post); |
| 31 | + } |
| 32 | +} |
17 | 33 | --- |
18 | 34 |
|
19 | 35 | { |
20 | | - hasContent && ( |
| 36 | + cards.length > 0 && ( |
21 | 37 | <aside class="read-next"> |
22 | 38 | <div class="read-next-inner"> |
23 | 39 | <h3 class="read-next-heading">Read Next</h3> |
24 | 40 | <div class="read-next-feed"> |
25 | | - {showRelatedPosts && ( |
26 | | - <ReadNextCard currentSlug={currentSlug} tags={tags} relatedPosts={relatedPosts} /> |
27 | | - )} |
28 | | - {prevPost && <PostCard post={prevPost} />} |
29 | | - {nextPost && <PostCard post={nextPost} />} |
| 41 | + {cards.map(post => <PostCard post={post} />)} |
30 | 42 | </div> |
31 | 43 | </div> |
32 | 44 | </aside> |
|
0 commit comments