-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
discussions-list.tsx
92 lines (82 loc) · 2.34 KB
/
discussions-list.tsx
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
import { useQueryClient } from '@tanstack/react-query';
import { useSearchParams } from 'react-router';
import { Link } from '@/components/ui/link';
import { Spinner } from '@/components/ui/spinner';
import { Table } from '@/components/ui/table';
import { paths } from '@/config/paths';
import { formatDate } from '@/utils/format';
import { getDiscussionQueryOptions } from '../api/get-discussion';
import { useDiscussions } from '../api/get-discussions';
import { DeleteDiscussion } from './delete-discussion';
export type DiscussionsListProps = {
onDiscussionPrefetch?: (id: string) => void;
};
export const DiscussionsList = ({
onDiscussionPrefetch,
}: DiscussionsListProps) => {
const [searchParams] = useSearchParams();
const discussionsQuery = useDiscussions({
page: +(searchParams.get('page') || 1),
});
const queryClient = useQueryClient();
if (discussionsQuery.isLoading) {
return (
<div className="flex h-48 w-full items-center justify-center">
<Spinner size="lg" />
</div>
);
}
const discussions = discussionsQuery.data?.data;
const meta = discussionsQuery.data?.meta;
if (!discussions) return null;
return (
<Table
data={discussions}
columns={[
{
title: 'Title',
field: 'title',
},
{
title: 'Created At',
field: 'createdAt',
Cell({ entry: { createdAt } }) {
return <span>{formatDate(createdAt)}</span>;
},
},
{
title: '',
field: 'id',
Cell({ entry: { id } }) {
return (
<Link
onMouseEnter={() => {
// Prefetch the discussion data when the user hovers over the link
queryClient.prefetchQuery(getDiscussionQueryOptions(id));
onDiscussionPrefetch?.(id);
}}
to={paths.app.discussion.getHref(id)}
>
View
</Link>
);
},
},
{
title: '',
field: 'id',
Cell({ entry: { id } }) {
return <DeleteDiscussion id={id} />;
},
},
]}
pagination={
meta && {
totalPages: meta.totalPages,
currentPage: meta.page,
rootUrl: '',
}
}
/>
);
};