6.5 개인화 위젯(최근 본 글/알림 요약)
This commit is contained in:
16
src/app/api/me/notifications/route.ts
Normal file
16
src/app/api/me/notifications/route.ts
Normal 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 });
|
||||
}
|
||||
|
||||
|
||||
18
src/app/api/me/recent/route.ts
Normal file
18
src/app/api/me/recent/route.ts
Normal 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 });
|
||||
}
|
||||
|
||||
|
||||
38
src/app/components/PersonalWidgets.tsx
Normal file
38
src/app/components/PersonalWidgets.tsx
Normal 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>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ import Image from "next/image";
|
||||
import { QuickActions } from "@/app/components/QuickActions";
|
||||
import { HeroBanner } from "@/app/components/HeroBanner";
|
||||
import { PostList } from "@/app/components/PostList";
|
||||
import { PersonalWidgets } from "@/app/components/PersonalWidgets";
|
||||
|
||||
export default function Home({ searchParams }: { searchParams?: { sort?: "recent" | "popular" } }) {
|
||||
const sort = searchParams?.sort ?? "recent";
|
||||
@@ -10,6 +11,7 @@ export default function Home({ searchParams }: { searchParams?: { sort?: "recent
|
||||
<HeroBanner />
|
||||
<QuickActions />
|
||||
<PostList sort={sort} />
|
||||
<PersonalWidgets />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user