File size: 2,969 Bytes
95d29a5 |
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
import { FC, useMemo } from 'react';
import {
Table,
TableHeader,
TableColumn,
TableBody,
TableRow,
TableCell,
getKeyValue,
Button,
Spinner,
Link,
} from '@nextui-org/react';
import { trpc } from '@web/utils/trpc';
import dayjs from 'dayjs';
import { useParams } from 'react-router-dom';
const ArticleList: FC = () => {
const { id } = useParams();
const mpId = id || '';
const { data, fetchNextPage, isLoading, hasNextPage } =
trpc.article.list.useInfiniteQuery(
{
limit: 20,
mpId: mpId,
},
{
getNextPageParam: (lastPage) => lastPage.nextCursor,
},
);
const items = useMemo(() => {
const items = data
? data.pages.reduce((acc, page) => [...acc, ...page.items], [] as any[])
: [];
return items;
}, [data]);
return (
<div>
<Table
classNames={{
base: 'h-full',
table: 'min-h-[420px]',
}}
aria-label="文章列表"
bottomContent={
hasNextPage && !isLoading ? (
<div className="flex w-full justify-center">
<Button
isDisabled={isLoading}
variant="flat"
onPress={() => {
fetchNextPage();
}}
>
{isLoading && <Spinner color="white" size="sm" />}
加载更多
</Button>
</div>
) : null
}
>
<TableHeader>
<TableColumn key="title">标题</TableColumn>
<TableColumn width={180} key="publishTime">
发布时间
</TableColumn>
</TableHeader>
<TableBody
isLoading={isLoading}
emptyContent={'暂无数据'}
items={items || []}
loadingContent={<Spinner />}
>
{(item) => (
<TableRow key={item.id}>
{(columnKey) => {
let value = getKeyValue(item, columnKey);
if (columnKey === 'publishTime') {
value = dayjs(value * 1e3).format('YYYY-MM-DD HH:mm:ss');
return <TableCell>{value}</TableCell>;
}
if (columnKey === 'title') {
return (
<TableCell>
<Link
className="visited:text-neutral-400"
isBlock
showAnchorIcon
color="foreground"
target="_blank"
href={`https://mp.weixin.qq.com/s/${item.id}`}
>
{value}
</Link>
</TableCell>
);
}
return <TableCell>{value}</TableCell>;
}}
</TableRow>
)}
</TableBody>
</Table>
</div>
);
};
export default ArticleList;
|