troylusty.com/src/components/RelatedArticles.astro

34 lines
1,019 B
Text
Raw Normal View History

---
import Button from "./Button.astro";
import { type CollectionEntry, getCollection } from "astro:content";
type Props = {
entry: CollectionEntry<"posts"> | CollectionEntry<"projects">;
};
const { entry } = Astro.props;
const { collection } = entry;
const items = (await getCollection(collection))
.filter((post) => !post.data.draft)
.sort((a, b) => b.data.date.getTime() - a.data.date.getTime());
const index = items.findIndex((x) => x.slug === entry.slug);
const prev = items[(index - 1 + items.length) % items.length];
const next = items[(index + 1) % items.length];
---
{
items.length > 1 ? (
<div class="animate-reveal mx-auto flex w-full flex-col-reverse items-center justify-between gap-4 opacity-0 [animation-delay:0.1s] md:flex-row">
<Button
href={`/${prev.collection}/${prev.slug}`}
link={`Previous: ${prev.data.title}`}
/>
<Button
href={`/${next.collection}/${next.slug}`}
link={`Next: ${next.data.title}`}
/>
</div>
) : null
}