1
- import { type QueryClient , queryOptions , useInfiniteQuery } from '@tanstack/react-query' ;
2
- import { Suspense , useEffect , useMemo } from 'react' ;
3
- import { Await , defer , type LoaderFunctionArgs , useLoaderData } from 'react-router-dom' ;
1
+ import {
2
+ // type QueryClient, queryOptions,
3
+ useInfiniteQuery ,
4
+ } from '@tanstack/react-query' ;
5
+ import {
6
+ // Suspense,
7
+ useEffect ,
8
+ useMemo ,
9
+ } from 'react' ;
4
10
11
+ // import { Await, defer, type LoaderFunctionArgs, useLoaderData } from 'react-router-dom';
5
12
import { WithNavBanner } from '@/components/blocks/authed' ;
6
- import { Loading } from '@/components/blocks/loading' ;
13
+ // import { Loading } from '@/components/blocks/loading';
7
14
import { ScrollArea } from '@/components/ui/scroll-area' ;
8
15
import { usePageTitle } from '@/lib/hooks' ;
9
16
import { useCrumbs } from '@/lib/hooks/use-crumbs' ;
10
17
import { ROUTES } from '@/lib/routes' ;
11
18
import { fetchQuestions , ROWS_PER_PAGE } from '@/services/question-service' ;
19
+ import { useAuthedRoute } from '@/stores/auth-store' ;
12
20
import type { IGetQuestionsResponse } from '@/types/question-types' ;
13
21
14
22
import { QuestionTable } from './question-table' ;
15
23
import { columns } from './table-columns' ;
16
24
17
- const getListQuestionsQueryConfig = ( pageNumber ?: number ) =>
18
- queryOptions ( {
19
- queryKey : [ 'qn' , 'list' , pageNumber ] ,
20
- queryFn : async ( { signal : _ } ) => fetchQuestions ( pageNumber ) ,
21
- } ) ;
22
-
23
- export const loader =
24
- ( queryClient : QueryClient ) =>
25
- async ( { params : _ } : LoaderFunctionArgs ) => {
26
- return defer ( {
27
- initialPage : queryClient . ensureQueryData ( getListQuestionsQueryConfig ( ) ) ,
28
- } ) ;
29
- } ;
30
-
31
- type IQuestionListServiceAPIResponse = Awaited < ReturnType < typeof fetchQuestions > > ;
32
- type IQuestionLoaderReturn = Awaited < ReturnType < ReturnType < typeof loader > > > [ 'data' ] ;
33
- type IQuestionLoaderData = { initialPage ?: IQuestionListServiceAPIResponse } ;
25
+ // type IQuestionListServiceAPIResponse = Awaited<ReturnType<typeof fetchQuestions>>;
26
+ // type IQuestionLoaderReturn = Awaited<ReturnType<ReturnType<typeof loader>>>['data'];
27
+ // type IQuestionLoaderData = { initialPage?: IQuestionListServiceAPIResponse };
28
+ // const getListQuestionsQueryConfig = (pageNumber?: number) =>
29
+ // queryOptions({
30
+ // queryKey: ['qn', 'list', pageNumber],
31
+ // queryFn: async ({ signal: _ }) => fetchQuestions(pageNumber),
32
+ // });
33
+ // export const loader =
34
+ // (queryClient: QueryClient) =>
35
+ // async ({ params: _ }: LoaderFunctionArgs) => {
36
+ // return defer({
37
+ // initialPage: queryClient.ensureQueryData(getListQuestionsQueryConfig()),
38
+ // });
39
+ // };
34
40
35
41
export function Questions ( ) {
42
+ const { userId } = useAuthedRoute ( ) ;
36
43
usePageTitle ( ROUTES . QUESTIONS ) ;
37
44
const { crumbs } = useCrumbs ( ) ;
38
45
39
- const initialData = useLoaderData ( ) as IQuestionLoaderReturn as IQuestionLoaderData ;
40
-
41
46
const { data, fetchNextPage, hasNextPage, isError, isFetchingNextPage } = useInfiniteQuery <
42
47
IGetQuestionsResponse ,
43
48
Error
44
49
> ( {
45
- queryKey : [ 'questions' ] ,
46
- queryFn : ( { pageParam } ) => fetchQuestions ( pageParam as number | undefined ) ,
50
+ queryKey : [ 'questions' , userId ] ,
51
+ queryFn : ( { pageParam } ) => fetchQuestions ( userId , pageParam as number | undefined ) ,
47
52
initialPageParam : 0 ,
48
53
getNextPageParam : ( lastPage , pages ) => {
49
54
const nextPage = pages . length ;
50
55
const totalPages = Math . ceil ( lastPage . totalQuestions / ROWS_PER_PAGE ) ;
51
56
return nextPage < totalPages ? nextPage : undefined ;
52
57
} ,
53
- initialData : {
54
- pages : initialData ?. initialPage ?. questions ? [ initialData . initialPage ] : [ ] ,
55
- pageParams : initialData ?. initialPage ?. questions ? [ 0 ] : [ ] ,
56
- } ,
57
58
} ) ;
58
59
59
60
useEffect ( ( ) => {
@@ -65,22 +66,16 @@ export function Questions() {
65
66
const questions = useMemo ( ( ) => {
66
67
if ( data ) {
67
68
return data . pages . flatMap ( ( page ) => page . questions ) ;
68
- } else if ( initialData ?. initialPage ?. questions ) {
69
- return initialData . initialPage . questions ;
70
69
}
71
70
72
71
return [ ] ;
73
- } , [ data , initialData ] ) ;
72
+ } , [ data ] ) ;
74
73
75
74
return (
76
75
< WithNavBanner crumbs = { crumbs } >
77
76
< div className = 'flex w-full flex-1 overflow-hidden py-3' >
78
77
< ScrollArea className = 'size-full px-6' >
79
- < Suspense fallback = { < Loading /> } >
80
- < Await resolve = { initialData . initialPage } >
81
- < QuestionTable columns = { columns } data = { questions } isError = { isError } />
82
- </ Await >
83
- </ Suspense >
78
+ < QuestionTable columns = { columns } data = { questions } isError = { isError } />
84
79
</ ScrollArea >
85
80
</ div >
86
81
</ WithNavBanner >
0 commit comments