1- import React , { useState , useEffect } from 'react' ;
1+ import React , { useEffect , useState } from 'react' ;
2+ import { useSelector } from 'react-redux' ;
3+ import { RootState } from '@/redux/store' ;
4+ import { Dashboard } from '@/type/dashboard' ;
25import styles from '@/components/common/sidebar/Sidebar.module.css' ;
3- import getDashboards from '@/lib/mydashboard/getDashboard ' ;
6+ import useSidebarDashboards from '@/hooks/useSidebar ' ;
47import Logo from 'public/images/img_logo.svg' ;
58import TextLogo from 'public/images/img_textlogo.svg' ;
69import PlusBtn from 'public/ic/ic_plus.svg' ;
710import CrownIcon from 'public/ic/ic_crown.svg' ;
811import CDSButton from '../button/CDSButton' ;
912
1013export default function Sidebar ( ) {
11- const [ menu , setMenu ] = useState ( [ ] ) ;
1214 const [ currentPage , setCurrentPage ] = useState ( 1 ) ;
13- const [ totalPages , setTotalPages ] = useState ( 1 ) ;
14- const [ cursorId , setCursorId ] = useState < number | null > ( 0 ) ;
15- const [ isLoading , setIsLoading ] = useState ( false ) ;
15+ const [ dashbaords , setDashboards ] = useState < Dashboard [ ] > ( [ ] ) ;
1616
17- const pageSize = 10 ;
17+ const { isLoading, totalPages, fetchSidebarDashboards } =
18+ useSidebarDashboards ( ) ;
1819
19- useEffect ( ( ) => {
20- const fetchDashboards = async ( ) => {
21- try {
22- setIsLoading ( true ) ;
23- const response = await getDashboards ( {
24- page : currentPage ,
25- size : pageSize ,
26- cursorId : cursorId || 0 ,
27- navigationMethod : 'pagination' ,
28- } ) ;
29-
30- const { dashboards, totalCount, cursorId : newCursorId } = response ;
31-
32- const formattedDashboards = dashboards . map (
33- ( { id, title, color, createdByMe } ) => ( {
34- id,
35- title,
36- color,
37- createdByMe,
38- } ) ,
39- ) ;
40-
41- setMenu ( formattedDashboards ) ;
42- setCursorId ( newCursorId ) ;
43- setTotalPages ( Math . ceil ( totalCount / pageSize ) ) ;
44- } catch ( error ) {
45- console . error ( '대시보드 데이터를 가져오는데 실패했습니다:' , error ) ;
46- } finally {
47- setIsLoading ( false ) ;
48- }
49- } ;
50-
51- fetchDashboards ( ) ;
52- } , [ currentPage , cursorId ] ) ;
20+ const sidebarDashboards = useSelector (
21+ ( state : RootState ) => state . dashboard . sidebarDashboards ,
22+ ) ;
5323
5424 const handlePageChange = ( direction : 'prev' | 'next' ) => {
5525 if ( direction === 'prev' && currentPage > 1 ) {
@@ -59,6 +29,14 @@ export default function Sidebar() {
5929 }
6030 } ;
6131
32+ useEffect ( ( ) => {
33+ fetchSidebarDashboards ( currentPage ) ;
34+ } , [ currentPage ] ) ;
35+
36+ useEffect ( ( ) => {
37+ setDashboards ( sidebarDashboards ) ;
38+ } , [ sidebarDashboards ] ) ;
39+
6240 return (
6341 < div className = { styles . sidebar } >
6442 < div className = { styles . logo } >
@@ -80,26 +58,33 @@ export default function Sidebar() {
8058 </ div >
8159
8260 { /* 동적으로 렌더링되는 메뉴 */ }
83- < ul className = { styles [ 'menu-list' ] } >
84- { menu . map ( ( item ) => (
85- < li key = { item . id } className = { styles [ 'menu-list-dashboard' ] } >
86- < div className = { styles [ 'dashboard-item' ] } >
87- < span
88- className = { styles [ 'color-circle ' ] }
89- style = { { backgroundColor : item . color } }
90- / >
91- < span className = { styles [ 'dashboard-title' ] } >
92- { item . title }
93- </ span >
94- { item . createdByMe && (
95- < span className = { styles [ 'crown-icon ' ] } >
96- < CrownIcon width = { 16 } height = { 16 } />
61+ { dashbaords && dashbaords . length > 0 ? (
62+ < ul className = { styles [ 'menu-list' ] } >
63+ { dashbaords . map ( ( item ) => (
64+ < li
65+ key = { `Sidebar_ ${ item . id } ` }
66+ className = { styles [ 'menu-list-dashboard ' ] }
67+ >
68+ < div className = { styles [ 'dashboard-item' ] } >
69+ < span
70+ className = { styles [ 'color-circle' ] }
71+ style = { { backgroundColor : item . color } }
72+ />
73+ < span className = { styles [ 'dashboard-title ' ] } >
74+ { item . title }
9775 </ span >
98- ) }
99- </ div >
100- </ li >
101- ) ) }
102- </ ul >
76+ { item . createdByMe && (
77+ < span className = { styles [ 'crown-icon' ] } >
78+ < CrownIcon width = { 16 } height = { 16 } />
79+ </ span >
80+ ) }
81+ </ div >
82+ </ li >
83+ ) ) }
84+ </ ul >
85+ ) : (
86+ < span > 대시보드가 없습니다.</ span >
87+ ) }
10388 </ div >
10489
10590 { /* 페이지네이션 컨트롤 */ }
@@ -109,7 +94,6 @@ export default function Sidebar() {
10994 onClick = { ( ) => handlePageChange ( 'prev' ) }
11095 disabled = { currentPage === 1 || isLoading }
11196 />
112-
11397 < CDSButton
11498 btnType = "pagination_next"
11599 onClick = { ( ) => handlePageChange ( 'next' ) }
0 commit comments