-
-
Notifications
You must be signed in to change notification settings - Fork 371
Expand file tree
/
Copy pathRecentPostsWidget.tsx
More file actions
75 lines (70 loc) · 2.02 KB
/
Copy pathRecentPostsWidget.tsx
File metadata and controls
75 lines (70 loc) · 2.02 KB
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
import { Link } from '@tanstack/react-router'
import { useSuspenseQuery } from '@tanstack/react-query'
import { fetchRecentPosts } from '~/utils/blog.functions'
import { formatPublishedDate } from '~/utils/blog'
import { Suspense } from 'react'
function RecentPostsList() {
const { data: posts } = useSuspenseQuery({
queryKey: ['recentPosts'],
queryFn: () => fetchRecentPosts(),
staleTime: 1000 * 60 * 5,
})
return (
<div className="flex flex-col">
<div className="flex items-center justify-between border-b border-gray-500/20 px-3 py-2">
<Link
to="/blog"
className="font-medium opacity-60 hover:opacity-100 text-xs"
>
Latest Posts
</Link>
</div>
<div className="flex flex-col divide-y divide-gray-500/10">
{posts.map((post) => {
const content = (
<>
<span className="text-xs font-medium leading-snug line-clamp-2">
{post.title}
</span>
<span className="text-[10px] text-gray-500 dark:text-gray-500">
{formatPublishedDate(post.published)}
</span>
</>
)
const className = `flex flex-col gap-0.5 px-3 py-2.5
hover:bg-gray-500/5 transition-colors duration-150`
if (post.externalUrl) {
return (
<a
key={post.slug}
href={post.externalUrl}
target="_blank"
rel="noopener noreferrer"
className={className}
>
{content}
</a>
)
}
return (
<Link
key={post.slug}
to="/blog/$"
params={{ _splat: post.slug } as never}
className={className}
>
{content}
</Link>
)
})}
</div>
</div>
)
}
export function RecentPostsWidget() {
return (
<Suspense>
<RecentPostsList />
</Suspense>
)
}