Skip to content
Source
Sign inSign up
TanStack/cli
Open History to see the last commit

blog.index.tsx

102 lines3.2 KB

1import { Link, createFileRoute } from '@tanstack/react-router'
2import { allBlogs } from 'content-collections'
3import { SITE_DESCRIPTION, SITE_TITLE, SITE_URL } from '#/lib/site'
4​
5const canonical = `${SITE_URL}/blog`
6const pageTitle = `Blog | ${SITE_TITLE}`
7​
8export const Route = createFileRoute('/blog/')({
9 head: () => ({
10 links: [{ rel: 'canonical', href: canonical }],
11 meta: [
12 { title: pageTitle },
13 { name: 'description', content: SITE_DESCRIPTION },
14 { property: 'og:image', content: `${SITE_URL}/images/lagoon-1.svg` },
15 ],
16 }),
17 component: BlogIndex,
18})
19​
20function BlogIndex() {
21 const postsByDate = Array.from(
22 new Map(
23 [...allBlogs]
24 .sort(
25 (a, b) =>
26 new Date(b.pubDate).valueOf() - new Date(a.pubDate).valueOf(),
27 )
28 .map((post) => [post.slug, post]),
29 ).values(),
30 )
31​
32 const featured = postsByDate[0]
33 const posts = postsByDate.slice(1)
34 return (
35 <main className="page-wrap px-4 pb-8 pt-14">
36 <section className="mb-4">
37 <p className="island-kicker mb-2">Latest Dispatches</p>
38 <h1 className="display-title m-0 text-4xl font-bold tracking-tight text-[var(--sea-ink)] sm:text-5xl">
39 Blog
40 </h1>
41 </section>
42​
43 <section className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
44 <article className="island-shell rise-in rounded-2xl p-5 sm:p-6 lg:col-span-2">
45 {featured.heroImage ? (
46 <img
47 src={featured.heroImage}
48 alt=""
49 className="mb-4 h-44 w-full rounded-xl object-cover xl:h-60"
50 />
51 ) : null}
52 <h2 className="m-0 text-2xl font-semibold text-[var(--sea-ink)]">
53 <Link
54 to="/blog/$slug"
55 params={{ slug: featured.slug }}
56 className="no-underline"
57 >
58 {featured.title}
59 </Link>
60 </h2>
61 <p className="mb-2 mt-3 text-base text-[var(--sea-ink-soft)]">
62 {featured.description}
63 </p>
64 <p className="m-0 text-xs text-[var(--sea-ink-soft)]">
65 {new Date(featured.pubDate).toLocaleDateString()}
66 </p>
67 </article>
68​
69 {posts.map((post, index) => (
70 <article
71 key={post.slug}
72 className="island-shell rise-in rounded-2xl p-5 sm:last:col-span-2 lg:last:col-span-1"
73 style={{ animationDelay: `${index * 80 + 120}ms` }}
74 >
75 {post.heroImage ? (
76 <img
77 src={post.heroImage}
78 alt=""
79 className="mb-4 h-44 w-full rounded-xl object-cover"
80 />
81 ) : null}
82 <h2 className="m-0 text-2xl font-semibold text-[var(--sea-ink)]">
83 <Link
84 to="/blog/$slug"
85 params={{ slug: post.slug }}
86 className="no-underline"
87 >
88 {post.title}
89 </Link>
90 </h2>
91 <p className="mb-2 mt-2 text-sm text-[var(--sea-ink-soft)]">
92 {post.description}
93 </p>
94 <p className="m-0 text-xs text-[var(--sea-ink-soft)]">
95 {new Date(post.pubDate).toLocaleDateString()}
96 </p>
97 </article>
98 ))}
99 </section>
100 </main>
101 )