1- import { useMutation , useQuery } from '@tanstack/react-query' ;
2- import { ChangeEvent , useState } from 'react' ;
1+ import { useMutation , useInfiniteQuery } from '@tanstack/react-query' ;
2+ import { ChangeEvent , useEffect , useState } from 'react' ;
3+ import { useInView } from 'react-intersection-observer' ;
34import { useLocation , useNavigate } from 'react-router' ;
45
56import { getMailboxDetail , postMailboxDisconnect } from '@/apis/mailBox' ;
@@ -10,8 +11,6 @@ import PageTitle from '@/components/PageTitle';
1011
1112import InformationTooltip from './components/InformationTooltip' ;
1213import LetterPreview from './components/LetterPreview' ;
13-
14- // import useAuthStore from '@/stores/authStore';
1514interface MailBoxDetailProps {
1615 letterId : number ;
1716 title : string ;
@@ -23,7 +22,6 @@ interface MailBoxDetailProps {
2322const LetterBoxDetailPage = ( ) => {
2423 const location = useLocation ( ) ;
2524 const userInfo = { ...location . state } ;
26- // const {userId} = useAuthStore.getState();
2725
2826 const [ isShareMode , setShareMode ] = useState ( false ) ;
2927 const [ isOpenDisConnectModal , setIsOpenDisConnectModal ] = useState ( false ) ;
@@ -33,45 +31,33 @@ const LetterBoxDetailPage = () => {
3331
3432 const navigate = useNavigate ( ) ;
3533
36- // const {
37- // data: mailLists = [],
38- // isLoading,
39- // isError,
40- // fetchNextPage,
41- // hasNextPage,
42- // isFetchingNextPage,
43- // } = useInfiniteQuery({
44- // queryKey: ['mailBoxDetail', userInfo.id],
45- // queryFn: async ({ pageParam }) => {
46- // const response = await getMailboxDetail(userInfo.id, pageParam);
47- // console.log(response.data);
48- // return response.data as MailBoxDetailProps[];
49- // },
50- // enabled: !!userInfo.id,
51- // initialPageParam: 0,
52- // getNextPageParam: (lastPage, allPages) => {
53- // return lastPage?.length == 0 || !lastPage || lastPage?.length < 20
54- // ? undefined
55- // : allPages.length + 1;
56- // },
57- // staleTime: 1000 * 60 * 5,
58- // gcTime: 1000 * 60 * 10,
59- // });
60-
61- const {
62- data : mailLists = [ ] ,
63- isLoading,
64- isError,
65- } = useQuery ( {
66- queryKey : [ 'mailBoxDetail' , userInfo . id ] ,
67- queryFn : async ( ) => {
68- const response = await getMailboxDetail ( userInfo . id ) ;
69- console . log ( response . data ) ;
70- return response . data as MailBoxDetailProps [ ] ;
71- } ,
72- staleTime : 1000 * 60 * 5 ,
73- gcTime : 1000 * 60 * 10 ,
74- } ) ;
34+ const { data, isLoading, isError, fetchNextPage, hasNextPage, isFetchingNextPage } =
35+ useInfiniteQuery ( {
36+ queryKey : [ 'mailBoxDetail' , userInfo . id ] ,
37+ queryFn : async ( { pageParam } ) => {
38+ console . log ( `Fetching page: ${ pageParam } ` ) ; // 디버깅용
39+ const response = await getMailboxDetail ( userInfo . id , pageParam ) ;
40+ console . log ( response . data ) ;
41+ return response . data ;
42+ } ,
43+ enabled : ! ! userInfo . id ,
44+ initialPageParam : 0 ,
45+ getNextPageParam : ( lastPage , allPages ) => {
46+ return lastPage . currentPage >= lastPage . totalPages ? undefined : allPages . length + 1 ;
47+ } ,
48+ staleTime : 1000 * 60 * 5 ,
49+ gcTime : 1000 * 60 * 10 ,
50+ } ) ;
51+
52+ const mailLists : MailBoxDetailProps [ ] = data ?. pages . flatMap ( ( page ) => page . content ) || [ ] ;
53+
54+ const { ref, inView } = useInView ( ) ;
55+
56+ useEffect ( ( ) => {
57+ if ( inView && hasNextPage && ! isFetchingNextPage ) {
58+ fetchNextPage ( ) ;
59+ }
60+ } , [ inView , hasNextPage , isFetchingNextPage , fetchNextPage ] ) ;
7561
7662 const disconnectMutation = useMutation ( {
7763 mutationFn : async ( ) => await postMailboxDisconnect ( userInfo . id ) ,
@@ -119,7 +105,7 @@ const LetterBoxDetailPage = () => {
119105 } ;
120106
121107 if ( isError ) {
122- navigate ( '/NotFound ' ) ;
108+ navigate ( '/notFound ' ) ;
123109 }
124110
125111 return (
@@ -179,7 +165,7 @@ const LetterBoxDetailPage = () => {
179165 //TODO: skeleton
180166 < div > Loading</ div >
181167 ) : (
182- mailLists . map ( ( letter ) => (
168+ mailLists . map ( ( letter , index ) => (
183169 < LetterPreview
184170 key = { letter . letterId }
185171 id = { letter . letterId }
@@ -191,6 +177,7 @@ const LetterBoxDetailPage = () => {
191177 isClosed = { userInfo . isClosed }
192178 onToggle = { ( ) => toggleSelected ( letter . letterId ) }
193179 zipCode = { userInfo . zipCode }
180+ ref = { index === mailLists . length - 1 ? ref : null }
194181 />
195182 ) )
196183 ) }
0 commit comments