forked from alan2207/bulletproof-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
discussion.tsx
73 lines (62 loc) · 2.17 KB
/
discussion.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
import { QueryClient } from '@tanstack/react-query';
import { ErrorBoundary } from 'react-error-boundary';
import { useParams, LoaderFunctionArgs } from 'react-router-dom';
import { ContentLayout } from '@/components/layouts';
import { Spinner } from '@/components/ui/spinner';
import { getInfiniteCommentsQueryOptions } from '@/features/comments/api/get-comments';
import { Comments } from '@/features/comments/components/comments';
import {
useDiscussion,
getDiscussionQueryOptions,
} from '@/features/discussions/api/get-discussion';
import { DiscussionView } from '@/features/discussions/components/discussion-view';
export const discussionLoader =
(queryClient: QueryClient) =>
async ({ params }: LoaderFunctionArgs) => {
const discussionId = params.discussionId as string;
const discussionQuery = getDiscussionQueryOptions(discussionId);
const commentsQuery = getInfiniteCommentsQueryOptions(discussionId);
const promises = [
queryClient.getQueryData(discussionQuery.queryKey) ??
(await queryClient.fetchQuery(discussionQuery)),
queryClient.getQueryData(commentsQuery.queryKey) ??
(await queryClient.fetchInfiniteQuery(commentsQuery)),
] as const;
const [discussion, comments] = await Promise.all(promises);
return {
discussion,
comments,
};
};
export const DiscussionRoute = () => {
const params = useParams();
const discussionId = params.discussionId as string;
const discussionQuery = useDiscussion({
discussionId,
});
if (discussionQuery.isLoading) {
return (
<div className="flex h-48 w-full items-center justify-center">
<Spinner size="lg" />
</div>
);
}
const discussion = discussionQuery.data?.data;
if (!discussion) return null;
return (
<>
<ContentLayout title={discussion.title}>
<DiscussionView discussionId={discussionId} />
<div className="mt-8">
<ErrorBoundary
fallback={
<div>Failed to load comments. Try to refresh the page.</div>
}
>
<Comments discussionId={discussionId} />
</ErrorBoundary>
</div>
</ContentLayout>
</>
);
};