import { commonCodeApi } from "@/lib/api/commonCode"; import { queryKeys } from "@/lib/queryKeys"; import { useInfiniteScroll } from "@/hooks/useInfiniteScroll"; import type { CategoryFilter } from "@/lib/schemas/commonCode"; import type { CodeCategory } from "@/types/commonCode"; /** * 카테고리 무한 스크롤 훅 */ export function useCategoriesInfinite(filters?: CategoryFilter) { return useInfiniteScroll({ queryKey: queryKeys.categories.infiniteList(filters), queryFn: async ({ pageParam, ...params }) => { // 첫 페이지는 20개, 이후는 10개씩 const pageSize = pageParam === 1 ? 20 : 10; const response = await commonCodeApi.categories.getList({ ...params, page: pageParam, size: pageSize, }); return { data: response.data || [], total: response.total, currentPage: pageParam, pageSize: pageSize, }; }, initialPageParam: 1, pageSize: 20, // 첫 페이지 기준 params: filters, staleTime: 5 * 60 * 1000, // 5분 캐싱 // 커스텀 getNextPageParam 제공 getNextPageParam: (lastPage, allPages, lastPageParam) => { // 마지막 페이지의 데이터 개수가 요청한 페이지 크기보다 작으면 더 이상 페이지 없음 const currentPageSize = lastPage.pageSize || (lastPageParam === 1 ? 20 : 10); if ((lastPage.data?.length || 0) < currentPageSize) { return undefined; } return lastPageParam + 1; }, }); }