|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect, useState, useRef } from "react"; |
| 4 | +import { useIntersection } from "@/lib/hooks/useIntersection"; |
1 | 5 | import { DashboardList } from "@/lib/types"; |
2 | 6 | import { fetchDashboardList } from "@/lib/apis/dashboardsApi"; |
3 | 7 | import { TOKEN_1 } from "@/lib/constants/tokens"; |
4 | 8 | import SideMenuItem from "./SideMenuItem"; |
5 | 9 |
|
6 | | -export default async function SideMenuList() { |
7 | | - const { dashboards, totalCount, cursorId } = |
8 | | - await fetchDashboardList(TOKEN_1); |
9 | | - const items: DashboardList[] = dashboards; |
| 10 | +const PAGE_SIZE = 15; |
| 11 | + |
| 12 | +export default function SideMenuList() { |
| 13 | + const [items, setItems] = useState<DashboardList[]>([]); |
| 14 | + const [page, setPage] = useState(1); |
| 15 | + const [isLoading, setIsLoading] = useState(false); |
| 16 | + const [isLast, setIsLast] = useState(false); |
| 17 | + const observerRef = useRef<HTMLDivElement | null>(null); |
| 18 | + |
| 19 | + const handleLoad = async () => { |
| 20 | + if (isLoading || isLast) return; |
| 21 | + setIsLoading(true); |
| 22 | + |
| 23 | + try { |
| 24 | + const { dashboards: newDashboards } = await fetchDashboardList({ |
| 25 | + token: TOKEN_1, |
| 26 | + size: PAGE_SIZE, |
| 27 | + page, |
| 28 | + }); |
| 29 | + |
| 30 | + if (newDashboards.length === 0) { |
| 31 | + setIsLast(true); |
| 32 | + } else { |
| 33 | + setItems((prev) => [...prev, ...newDashboards]); |
| 34 | + setPage((prev) => prev + 1); |
| 35 | + } |
| 36 | + } finally { |
| 37 | + setIsLoading(false); |
| 38 | + } |
| 39 | + }; |
| 40 | + |
| 41 | + useEffect(() => { |
| 42 | + handleLoad(); |
| 43 | + }, []); |
10 | 44 |
|
11 | | - // 해당 값들 사용하게 되면 지울 테스트 코드들 |
12 | | - console.log(totalCount); |
13 | | - console.log(cursorId); |
| 45 | + useIntersection({ |
| 46 | + target: observerRef, |
| 47 | + onIntersect: handleLoad, |
| 48 | + disabled: isLast, |
| 49 | + }); |
14 | 50 |
|
15 | 51 | return ( |
16 | | - <div className="flex flex-col gap-[14px] tablet:gap-[2px] pc:gap-2"> |
17 | | - {items.map((item) => ( |
18 | | - <SideMenuItem key={item.id} {...item} /> |
| 52 | + <div className="flex flex-col gap-[14px] flex-grow min-h-0 overflow-y-auto whitespace-nowrap scrollbar-hide tablet:gap-[2px] pc:gap-2"> |
| 53 | + {items.map((item, index) => ( |
| 54 | + <div |
| 55 | + key={item.id} |
| 56 | + ref={index === items.length - 1 ? observerRef : null} |
| 57 | + > |
| 58 | + <SideMenuItem {...item} /> |
| 59 | + </div> |
19 | 60 | ))} |
20 | 61 | </div> |
21 | 62 | ); |
|
0 commit comments