"use client"; import useSWRInfinite from "swr/infinite"; type Item = { id: string; title: string; createdAt: string; isPinned: boolean; status: string; stat?: { recommendCount: number; views: number; commentsCount: number } | null; }; type Resp = { total: number; page: number; pageSize: number; items: Item[]; sort: "recent" | "popular"; }; const fetcher = (url: string) => fetch(url).then((r) => r.json()); export function PostList({ boardId, sort = "recent" }: { boardId?: string; sort?: "recent" | "popular" }) { const pageSize = 10; const getKey = (index: number, prev: Resp | null) => { if (prev && prev.items.length === 0) return null; const page = index + 1; const sp = new URLSearchParams({ page: String(page), pageSize: String(pageSize), sort }); if (boardId) sp.set("boardId", boardId); return `/api/posts?${sp.toString()}`; }; const { data, size, setSize, isLoading } = useSWRInfinite(getKey, fetcher); const items = data?.flatMap((d) => d.items) ?? []; const canLoadMore = (data?.at(-1)?.items.length ?? 0) === pageSize; return (
정렬: 최신 인기
); }