-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
page.tsx
136 lines (122 loc) · 3.6 KB
/
page.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import type { Metadata } from 'next';
import { Suspense, cache } from 'react';
import { notFound } from 'next/navigation';
import { CustomMDX } from 'app/components/mdx';
import { getViewsCount } from 'app/db/queries';
import { getBlogPosts } from 'app/db/blog';
import ViewCounter from '../view-counter';
import { increment } from 'app/db/actions';
export async function generateMetadata({
params,
}): Promise<Metadata | undefined> {
let post = getBlogPosts().find((post) => post.slug === params.slug);
if (!post) {
return;
}
let {
title,
publishedAt: publishedTime,
summary: description,
image,
} = post.metadata;
let ogImage = image
? `https://leerob.io${image}`
: `https://leerob.io/og?title=${title}`;
return {
title,
description,
openGraph: {
title,
description,
type: 'article',
publishedTime,
url: `https://leerob.io/blog/${post.slug}`,
images: [
{
url: ogImage,
},
],
},
twitter: {
card: 'summary_large_image',
title,
description,
images: [ogImage],
},
};
}
function formatDate(date: string) {
let currentDate = new Date();
let targetDate = new Date(date);
let yearsAgo = currentDate.getFullYear() - targetDate.getFullYear();
let monthsAgo = currentDate.getMonth() - targetDate.getMonth();
let daysAgo = currentDate.getDate() - targetDate.getDate();
let formattedDate = '';
if (yearsAgo > 0) {
formattedDate = `${yearsAgo}y ago`;
} else if (monthsAgo > 0) {
formattedDate = `${monthsAgo}mo ago`;
} else if (daysAgo > 0) {
formattedDate = `${daysAgo}d ago`;
} else {
formattedDate = 'Today';
}
let fullDate = targetDate.toLocaleString('en-us', {
month: 'long',
day: 'numeric',
year: 'numeric',
});
return `${fullDate} (${formattedDate})`;
}
export default function Blog({ params }) {
let post = getBlogPosts().find((post) => post.slug === params.slug);
if (!post) {
notFound();
}
return (
<section>
<script
type="application/ld+json"
suppressHydrationWarning
dangerouslySetInnerHTML={{
__html: JSON.stringify({
'@context': 'https://schema.org',
'@type': 'BlogPosting',
headline: post.metadata.title,
datePublished: post.metadata.publishedAt,
dateModified: post.metadata.publishedAt,
description: post.metadata.summary,
image: post.metadata.image
? `https://leerob.io${post.metadata.image}`
: `https://leerob.io/og?title=${post.metadata.title}`,
url: `https://leerob.io/blog/${post.slug}`,
author: {
'@type': 'Person',
name: 'Lee Robinson',
},
}),
}}
/>
<h1 className="title font-medium text-2xl tracking-tighter max-w-[650px]">
{post.metadata.title}
</h1>
<div className="flex justify-between items-center mt-2 mb-8 text-sm max-w-[650px]">
<p className="text-sm text-neutral-600 dark:text-neutral-400">
{formatDate(post.metadata.publishedAt)}
</p>
<Suspense fallback={<p className="h-5" />}>
<Views slug={post.slug} />
</Suspense>
</div>
<article className="prose prose-quoteless prose-neutral dark:prose-invert">
<CustomMDX source={post.content} />
</article>
</section>
);
}
let incrementViews = cache(increment);
async function Views({ slug }: { slug: string }) {
let views = await getViewsCount();
incrementViews(slug);
return <ViewCounter allViews={views} slug={slug} />;
}