1- import { useState } from "react" ;
1+ import { useEffect , useState } from "react" ;
22import { GetServerSidePropsContext } from "next" ;
33import { useRouter } from "next/router" ;
44import { parse } from "cookie" ;
55import { LinkData } from "@/types/linkTypes" ;
66import { FolderData } from "@/types/folderTypes" ;
77import { Modal } from "@/components/modal/modalManager/ModalManager" ;
88import { SearchInput } from "../../components/Search/SearchInput" ;
9+ import { useLinkCardStore } from "@/store/useLinkCardStore" ;
910import axiosInstance from "@/lib/api/axiosInstanceApi" ;
1011import useModalStore from "@/store/useModalStore" ;
1112import Pagination from "@/components/Pagination" ;
@@ -20,6 +21,7 @@ import LinkCard from "@/components/Link/LinkCard";
2021import RenderEmptyLinkMessage from "@/components/Link/RenderEmptyLinkMessage" ;
2122import useFetchLinks from "@/hooks/useFetchLinks" ;
2223import useViewport from "@/hooks/useViewport" ;
24+ import useFolderName from "@/hooks/useFolderName" ;
2325import LoadingSpinner from "@/components/LoadingSpinner" ;
2426
2527interface LinkPageProps {
@@ -36,6 +38,16 @@ export const getServerSideProps = async (
3638 const cookies = parse ( req . headers . cookie || "" ) ;
3739 const accessToken = cookies . accessToken ;
3840
41+ // accessToken이 없으면 클라이언트에서 실행될 때 /login 페이지로 이동시킴.
42+ if ( ! accessToken ) {
43+ return {
44+ redirect : {
45+ destination : "/login" ,
46+ permanent : false ,
47+ } ,
48+ } ;
49+ }
50+
3951 const fetchData = async ( endpoint : string ) => {
4052 const res = await axiosInstance . get ( endpoint , {
4153 headers : {
@@ -68,17 +80,18 @@ const LinkPage = ({
6880 const { search, folder } = router . query ;
6981 const { isOpen } = useModalStore ( ) ;
7082 const { isMobile } = useViewport ( ) ;
71- const [ linkCardList , setLinkCardList ] = useState ( initialLinkList ) ;
83+ const { totalCount, linkCardList, setLinkCardList } =
84+ useLinkCardStore . getState ( ) ;
85+ const [ isLoading , setIsLoading ] = useState ( false ) ;
86+ const [ folderName ] = useFolderName ( folder ) ;
7287 const [ folderList , setFolderList ] = useState ( initialFolderList ) ;
73- const [ totalCount , setTotalCount ] = useState ( initialTotalCount ) ;
88+
89+ useEffect ( ( ) => {
90+ setLinkCardList ( initialLinkList , initialTotalCount ) ;
91+ } , [ initialLinkList , initialTotalCount , setLinkCardList ] ) ;
7492
7593 // 링크페이지의 query가 바뀌면 새로운 리스트로 업데이트 해주는 훅
76- const loading = useFetchLinks (
77- setLinkCardList ,
78- setTotalCount ,
79- router . query ,
80- router . pathname
81- ) ;
94+ useFetchLinks ( setLinkCardList , setIsLoading ) ;
8295
8396 return (
8497 < >
@@ -94,29 +107,27 @@ const LinkPage = ({
94107 { ! isMobile && < AddFolderButton setFolderList = { setFolderList } /> }
95108 </ div >
96109 < div className = "flex justify-between items-center my-[24px]" >
97- < h1 className = "text-2xl " > 유용한 글</ h1 >
98110 { folder && (
99- < FolderActionsMenu
100- setFolderList = { setFolderList }
101- folderId = { folder }
102- linkCount = { totalCount }
103- />
111+ < >
112+ < h1 className = "text-2xl " > { folderName as string } </ h1 >
113+ < FolderActionsMenu
114+ setFolderList = { setFolderList }
115+ folderId = { folder }
116+ linkCount = { totalCount as number }
117+ />
118+ </ >
104119 ) }
105120 </ div >
106-
107- { /* 로딩 중일 때 */ }
108- { loading ? (
109- < div className = "text-center" >
110- < LoadingSpinner />
111- </ div >
112- ) : linkCardList . length > 0 ? (
121+ { isLoading ? (
122+ < LoadingSpinner /> // 로딩 상태일 때 로딩 스피너 표시
123+ ) : linkCardList . length !== 0 ? (
113124 < >
114125 < CardsLayout >
115126 { linkCardList . map ( ( link ) => (
116127 < LinkCard key = { link . id } info = { link } />
117128 ) ) }
118129 </ CardsLayout >
119- < Pagination totalCount = { totalCount } />
130+ < Pagination totalCount = { totalCount as number } />
120131 </ >
121132 ) : (
122133 < RenderEmptyLinkMessage />
0 commit comments