| 1 | import type { |
| 2 | InfiniteData, |
| 3 | UseInfiniteQueryResult, |
| 4 | } from '@tanstack/react-query' |
| 5 | import type { ReactNode } from 'react' |
| 6 | import { More } from '../../components/ui/More' |
| 7 | import { Failed, Skeletons } from '../../components/ui/Notice' |
| 8 | import type { Page } from '../../lib/api' |
| 9 | import { PostItem } from './PostItem' |
| 10 | |
| 11 | type Props = { |
| 12 | query: UseInfiniteQueryResult<InfiniteData<Page>> |
| 13 | empty: ReactNode |
| 14 | } |
| 15 | |
| 16 | export function PostList({ query, empty }: Props) { |
| 17 | if (query.isPending) return <Skeletons /> |
| 18 | if (query.isError) { |
| 19 | return <Failed error={query.error} retry={query.refetch} /> |
| 20 | } |
| 21 | const posts = query.data.pages.flatMap((page) => page.posts) |
| 22 | if (!posts.length) return empty |
| 23 | return ( |
| 24 | <> |
| 25 | <ol className="divide-y divide-line border-b border-line"> |
| 26 | {posts.map((post) => ( |
| 27 | <li key={post.id}> |
| 28 | <PostItem post={post} /> |
| 29 | </li> |
| 30 | ))} |
| 31 | </ol> |
| 32 | <More pages={query} /> |
| 33 | </> |
| 34 | ) |
| 35 | } |