-
Notifications
You must be signed in to change notification settings - Fork 788
/
Copy pathposts.tsx
52 lines (49 loc) · 1.31 KB
/
posts.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
import { getSession } from "@/lib/auth";
import db from "@/lib/db";
import Image from "next/image";
import { redirect } from "next/navigation";
import PostCard from "./post-card";
export default async function Posts({
siteId,
limit,
}: {
siteId?: string;
limit?: number;
}) {
const session = await getSession();
if (!session?.user) {
redirect("/login");
}
const posts = await db.query.posts.findMany({
where: (posts, { and, eq }) =>
and(
eq(posts.userId, session.user.id),
siteId ? eq(posts.siteId, siteId) : undefined,
),
with: {
site: true,
},
orderBy: (posts, { desc }) => desc(posts.updatedAt),
...(limit ? { limit } : {}),
});
return posts.length > 0 ? (
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2 xl:grid-cols-4">
{posts.map((post) => (
<PostCard key={post.id} data={post} />
))}
</div>
) : (
<div className="flex flex-col items-center space-x-4">
<h1 className="font-cal text-4xl">No Posts Yet</h1>
<Image
alt="missing post"
src="https://illustrations.popsy.co/gray/graphic-design.svg"
width={400}
height={400}
/>
<p className="text-lg text-stone-500">
You do not have any posts yet. Create one to get started.
</p>
</div>
);
}