11import { useInfiniteQuery } from '@tanstack/react-query' ;
22import { useInView } from 'react-intersection-observer' ;
33import SearchBar from '@/components/SearchBar' ;
4- import MainCard from '@/pages/home/components/MainCard' ;
54import { useEffect , useState } from 'react' ;
65import EmotionFilter from '@/components/EmotionFilter' ;
76import { useSheetStore } from '@/store/sheetStore' ;
8- import CardDetailModal from '@/components/modalSheet/CardDetailModal' ;
97import { getEmotionRecords } from '@/apis/emotionRecord' ;
10- import { formatDate } from '@/utils/formatDate' ;
8+
119import MusicSearchSheet from '@/components/modalSheet/MusicSearchSheet' ;
1210import { useMusicCardStore } from '@/store/MusicCardStore' ;
13- import InfoMessage from '@/components/InfoMessage' ;
11+
1412import LoadingMini from '@/components/loading/LoadingMini' ;
1513import Loading from '@/components/loading/Loading' ;
14+ import MainCardList from '@/pages/home/components/MainCardList' ;
15+ import { Outlet } from 'react-router' ;
1616
1717function Home ( ) {
1818 const { isMusicSheetOpen, openSheet, closeAllSheets } = useSheetStore ( ) ; // 모달 시트
1919 const { selectedPostMusic, clearPostMusic } = useMusicCardStore ( ) ; // 선택된 음악
2020
2121 const [ searchText , setSearchText ] = useState ( '' ) ; // 검색어
2222 const [ selectedEmotion , setSelectedEmotion ] = useState < string | null > ( null ) ; // 선택된 감정 필터
23- const [ selectedRecordId , setSelectedRecordId ] = useState < number | null > ( null ) ; // 선택한 타인 감정 게시글 id -> 게시글 상세 모달 열기
2423
2524 // 감정 필터링
2625 const onEmotionClick = ( emotion : string ) => {
2726 setSelectedEmotion ( ( prev ) => ( prev === emotion ? null : emotion ) ) ;
28- // // console.log(selectedEmotion);
29- } ;
30-
31- // 유저 상세 페이지 모달 열기
32- const handleOpenSheet = ( recordId : number ) => {
33- setSelectedRecordId ( recordId ) ;
34- openSheet ( 'isCardSheetOpen' ) ; // 모달 열기
3527 } ;
3628
3729 // 감정 기록 불러오기
@@ -44,8 +36,6 @@ function Home() {
4436 } = useInfiniteQuery ( {
4537 queryKey : [ 'emotionRecords' , selectedPostMusic ?. spotifyId , selectedEmotion ] ,
4638 queryFn : async ( { pageParam } ) => {
47- // // console.log('pageParam:', pageParam);
48-
4939 const { data } = await getEmotionRecords (
5040 pageParam ,
5141 10 ,
@@ -68,10 +58,7 @@ function Home() {
6858 initialPageParam : 1 , // 첫 페이지 번호 초기화!
6959 } ) ;
7060
71- useEffect ( ( ) => {
72- // 첫 페이지 로드
73- // // console.log('페이지 로드', isLoading);
74- } , [ isLoading ] ) ;
61+ const allRecords = emotionRecords ?. pages . flatMap ( ( page ) => page . records ) ?? [ ] ;
7562
7663 // 무한 스크롤 감지 요소 추가
7764 const { ref, inView } = useInView ( ) ;
@@ -119,41 +106,15 @@ function Home() {
119106 </ div >
120107
121108 { /* 메인카드 리스트 */ }
122- < div className = "flex-1" >
123- { emotionRecords ?. pages [ 0 ] . records . length > 0 ? (
124- < >
125- < div className = "flex flex-col items-center gap-2.5 pb-5 " >
126- { emotionRecords ?. pages . map ( ( page ) =>
127- page . records . map ( ( record : EmotionRecord ) => (
128- < div className = "w-full" key = { record . recordId } >
129- < div onClick = { ( ) => handleOpenSheet ( record . recordId ) } >
130- < MainCard
131- albumImage = { record . spotifyMusic . albumImage } // 앨범 이미지
132- nickname = { record . nickName } // 닉네임
133- emotion = { record . emotion } // 감정
134- title = { record . spotifyMusic . title } // 노래 제목
135- artist = { record . spotifyMusic . artist } // 가수
136- comment = { record . comment } // 글 내용
137- createdAt = { formatDate ( record . createdAt ) } // 날짜
138- />
139- </ div >
140- </ div >
141- ) ) ,
142- ) }
143- { hasNextPage && ! isFetchingNextPage && (
144- < div className = "m-auto" ref = { ref } >
145- < LoadingMini />
146- </ div >
147- ) }
148- </ div >
149- </ >
150- ) : (
151- < div className = "flex items-center justify-center w-full h-full" >
152- < InfoMessage text = "아직 작성된 글이 없어요" />
109+ < div className = "pb-5" >
110+ < MainCardList records = { allRecords } />
111+ { hasNextPage && ! isFetchingNextPage && (
112+ < div className = "m-auto" ref = { ref } >
113+ < LoadingMini />
153114 </ div >
154115 ) }
155116 </ div >
156- { selectedRecordId !== null && < CardDetailModal recordId = { selectedRecordId } /> }
117+ < Outlet />
157118 { isLoading && < Loading /> }
158119 { isMusicSheetOpen && < MusicSearchSheet /> }
159120 </ div >
0 commit comments