6.5 개인화 위젯(최근 본 글/알림 요약)

This commit is contained in:
koreacomp5
2025-10-09 16:40:53 +09:00
parent c16b9c1a51
commit 663cf0f19e
4 changed files with 74 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
import { NextResponse } from "next/server";
import prisma from "@/lib/prisma";
import { getUserIdFromRequest } from "@/lib/auth";
export async function GET(req: Request) {
const userId = getUserIdFromRequest(req);
if (!userId) return NextResponse.json({ items: [] });
const items = await prisma.adminNotification.findMany({
orderBy: { createdAt: "desc" },
take: 5,
select: { id: true, type: true, message: true, createdAt: true },
});
return NextResponse.json({ items });
}

View File

@@ -0,0 +1,18 @@
import { NextResponse } from "next/server";
import prisma from "@/lib/prisma";
import { getUserIdFromRequest } from "@/lib/auth";
export async function GET(req: Request) {
const userId = getUserIdFromRequest(req);
if (!userId) return NextResponse.json({ items: [] });
const logs = await prisma.postViewLog.findMany({
where: { userId },
orderBy: { createdAt: "desc" },
take: 10,
select: { postId: true, createdAt: true, post: { select: { title: true } } },
});
const items = logs.map((l) => ({ id: l.postId, title: l.post?.title ?? "", viewedAt: l.createdAt }));
return NextResponse.json({ items });
}

View File

@@ -0,0 +1,38 @@
"use client";
import useSWR from "swr";
type RecentItem = { id: string; title: string; viewedAt: string };
type NotiItem = { id: string; type: string; message: string; createdAt: string };
const fetcher = (url: string) => fetch(url).then((r) => r.json());
export function PersonalWidgets() {
const { data: recent } = useSWR<{ items: RecentItem[] }>("/api/me/recent", fetcher);
const { data: notis } = useSWR<{ items: NotiItem[] }>("/api/me/notifications", fetcher);
return (
<div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 16, marginTop: 16 }}>
<section>
<h3 style={{ marginTop: 0 }}> </h3>
<ul style={{ display: "flex", flexDirection: "column", gap: 6 }}>
{(recent?.items ?? []).map((i) => (
<li key={i.id}>
<a href={`/posts/${i.id}`}>{i.title}</a>
</li>
))}
{(!recent || recent.items.length === 0) && <li> .</li>}
</ul>
</section>
<section>
<h3 style={{ marginTop: 0 }}> </h3>
<ul style={{ display: "flex", flexDirection: "column", gap: 6 }}>
{(notis?.items ?? []).map((n) => (
<li key={n.id}>{n.message}</li>
))}
{(!notis || notis.items.length === 0) && <li> .</li>}
</ul>
</section>
</div>
);
}

View File

@@ -2,6 +2,7 @@ import Image from "next/image";
import { QuickActions } from "@/app/components/QuickActions"; import { QuickActions } from "@/app/components/QuickActions";
import { HeroBanner } from "@/app/components/HeroBanner"; import { HeroBanner } from "@/app/components/HeroBanner";
import { PostList } from "@/app/components/PostList"; import { PostList } from "@/app/components/PostList";
import { PersonalWidgets } from "@/app/components/PersonalWidgets";
export default function Home({ searchParams }: { searchParams?: { sort?: "recent" | "popular" } }) { export default function Home({ searchParams }: { searchParams?: { sort?: "recent" | "popular" } }) {
const sort = searchParams?.sort ?? "recent"; const sort = searchParams?.sort ?? "recent";
@@ -10,6 +11,7 @@ export default function Home({ searchParams }: { searchParams?: { sort?: "recent
<HeroBanner /> <HeroBanner />
<QuickActions /> <QuickActions />
<PostList sort={sort} /> <PostList sort={sort} />
<PersonalWidgets />
</div> </div>
); );
} }