Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions apps/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
"class-variance-authority": "^0.7.1",
"firebase": "^12.2.1",
"framer-motion": "^12.23.12",
"lottie-react": "^2.4.1",
"react": "^19.1.1",
"react-dom": "^19.1.1",
"react-error-boundary": "^6.0.0",
"react-router-dom": "^7.8.2",
"lottie-react": "^2.4.1"
"react-intersection-observer": "^10.0.3",
"react-router-dom": "^7.8.2"
},
"devDependencies": {
"@eslint/js": "^9.33.0",
Expand Down
9 changes: 5 additions & 4 deletions apps/client/src/pages/jobPins/JobPins.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const JobPins = () => {
const { scrollContainerRef, isBottomNoticeVisible, handleBottomWheel } =
useJobPinsBottomNotice();

const { data, isPending, fetchNextPage, hasNextPage } =
const { data, isPending, fetchNextPage, hasNextPage, isFetchingNextPage } =
useGetJobPinsArticles();
const {
mutate: getJobPinDetail,
Expand All @@ -22,9 +22,10 @@ const JobPins = () => {
} = useGetJobPinsArticleDetail();

const observerRef = useInfiniteScroll({
fetchNextPage,
hasNextPage,
root: scrollContainerRef,
loadMore: fetchNextPage,
hasMore: hasNextPage,
isLoadingMore: isFetchingNextPage,
rootRef: scrollContainerRef,
});

const articlesToDisplay =
Expand Down
4 changes: 3 additions & 1 deletion apps/client/src/pages/jobPins/apis/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import {
JobPinsDetailResponse,
} from './axios';

const PAGE_SIZE = 20;

export const useGetJobPinsArticles = () => {
return useInfiniteQuery<JobPinsResponse>({
queryKey: ['jobPinsArticles'],
queryFn: ({ pageParam = 0 }) => getJobPinsArticles(pageParam as number, 20),
initialPageParam: 0,
getNextPageParam: (lastPage, allPages) => {
if (lastPage.articles.length === 0) {
if (lastPage.articles.length < PAGE_SIZE) {
return undefined;
}

Expand Down
12 changes: 6 additions & 6 deletions apps/client/src/pages/myBookmark/apis/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import {
useSuspenseInfiniteQuery,
useSuspenseQuery,
} from '@tanstack/react-query';
import { CategoryBookmarkArticleResponse } from '../types/api';
import {
getBookmarkArticles,
getBookmarkArticlesCount,
getCategoryBookmarkArticles,
getCategoryBookmarkArticlesCount,
} from './axios';
import {
CategoryBookmarkArticleResponse,
} from '../types/api';

const PAGE_SIZE = 20;

export const useGetBookmarkArticles = (readStatus: boolean | null) => {
return useSuspenseInfiniteQuery({
Expand All @@ -20,7 +20,7 @@ export const useGetBookmarkArticles = (readStatus: boolean | null) => {
getBookmarkArticles(readStatus, Number(pageParam), 20),
initialPageParam: 0,
getNextPageParam: (lastPage, allPages) =>
lastPage.articles.length === 0 ? undefined : allPages.length,
lastPage.articles.length < PAGE_SIZE ? undefined : allPages.length,
});
};

Expand Down Expand Up @@ -55,8 +55,8 @@ export const useGetCategoryBookmarkArticles = (

initialPageParam: 0,
getNextPageParam: (lastPage, allPages) => {
if (!lastPage || lastPage.articles.length === 0) return undefined;
return allPages.length;
if (!lastPage) return undefined;
return lastPage.articles.length < PAGE_SIZE ? undefined : allPages.length;
},
Comment thread
coderabbitai[bot] marked this conversation as resolved.
});
};
Expand Down
20 changes: 14 additions & 6 deletions apps/client/src/pages/myBookmark/hooks/useMyBookmarkContentData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,18 @@ export const useMyBookmarkContentData = ({
data: bookmarkArticlesData,
fetchNextPage: fetchNextBookmarkArticles,
hasNextPage: hasNextBookmarkArticles,
isFetchingNextPage: isFetchingNextBookmarkArticles,
} = useGetBookmarkArticles(readStatus);

const { data: bookmarkCountData } = useGetBookmarkArticlesCount();
const { data: categoryCountData } = useGetCategoryBookmarkArticlesCount(
categoryId
);
const { data: categoryCountData } =
useGetCategoryBookmarkArticlesCount(categoryId);

const {
data: categoryArticlesData,
fetchNextPage: fetchNextCategoryArticles,
hasNextPage: hasNextCategoryArticles,
isFetchingNextPage: isFetchingNextCategoryArticles,
} = useGetCategoryBookmarkArticles(categoryId, readStatus);

const categoryList =
Expand All @@ -62,14 +64,20 @@ export const useMyBookmarkContentData = ({
const hasNextPage = isCategoryView
? hasNextCategoryArticles
: hasNextBookmarkArticles;

const fetchNextPage = isCategoryView
? fetchNextCategoryArticles
: fetchNextBookmarkArticles;

const isFetchingNextPage = isCategoryView
? isFetchingNextCategoryArticles
: isFetchingNextBookmarkArticles;

const sentinelRef = useInfiniteScroll({
fetchNextPage,
hasNextPage,
root: scrollContainerRef,
loadMore: fetchNextPage,
hasMore: hasNextPage,
isLoadingMore: isFetchingNextPage,
rootRef: scrollContainerRef,
});

return {
Expand Down
13 changes: 6 additions & 7 deletions apps/client/src/pages/remind/Remind.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,14 @@ const Remind = () => {
useGetArticleDetail();
const { mutate: updateToReadStatus } = usePutArticleReadStatus();
const { mutate: deleteArticle } = useDeleteRemindArticle();
const { data, isPending, fetchNextPage, hasNextPage } = useGetRemindArticles(
formattedDate,
activeBadge === 'read'
);
const { data, isPending, fetchNextPage, hasNextPage, isFetchingNextPage } =
useGetRemindArticles(formattedDate, activeBadge === 'read');

const observerRef = useInfiniteScroll({
fetchNextPage,
hasNextPage,
root: scrollContainerRef,
loadMore: fetchNextPage,
hasMore: hasNextPage,
isLoadingMore: isFetchingNextPage,
rootRef: scrollContainerRef,
});

const {
Expand Down
85 changes: 43 additions & 42 deletions apps/client/src/shared/hooks/useInfiniteScroll.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,52 @@
import { useEffect, useRef } from 'react';
import { useInView } from 'react-intersection-observer';

interface UseInfiniteScrollProps {
fetchNextPage: () => void;
hasNextPage?: boolean;
root?: React.RefObject<HTMLElement | null>;
threshold?: number;
}
type UseInfiniteScrollProps = {
/** 더 불러올 데이터가 있는지 */
hasMore?: boolean;
/** 추가 로딩 중인지 (중복 호출 방지용) */
isLoadingMore?: boolean;
/** 다음 데이터를 불러오는 함수 */
loadMore: () => void | Promise<unknown>;

/**
* Intersection Observer를 사용하여 무한 스크롤을 구현하는 커스텀 훅
* @returns observer가 감지할 엘리먼트에 연결할 ref 객체
*/
/** 내부 스크롤 컨테이너를 root로 쓰고 싶을 때 */
rootRef?: React.RefObject<HTMLElement | null>;

/** 바닥에 닿기 전에 미리 불러오기 (ex: '200px 0px') */
rootMargin?: string;

/** 무한 스크롤을 일시적으로 끄기 */
enabled?: boolean;
};

export const useInfiniteScroll = ({
fetchNextPage,
hasNextPage,
root,
threshold = 0.5,
hasMore,
isLoadingMore = false,
loadMore,
rootRef,
rootMargin = '200px 0px',
enabled = true,
}: UseInfiniteScrollProps) => {
const targetRef = useRef<HTMLDivElement>(null);
const lockRef = useRef(false);

const { ref, inView } = useInView({
root: rootRef?.current ?? null,
threshold: 0,
rootMargin,
});

useEffect(() => {
if (!hasNextPage) return;

const observer = new IntersectionObserver(
([entry]) => {
if (entry.isIntersecting) {
fetchNextPage();
}
},
{
root: root?.current,
threshold,
}
);

const currentTarget = targetRef.current;
if (currentTarget) {
observer.observe(currentTarget);
}

return () => {
if (currentTarget) {
observer.unobserve(currentTarget);
}
};
}, [fetchNextPage, hasNextPage, root, threshold]);

return targetRef;
if (!enabled) return;
if (!inView) return;
if (!hasMore) return;
if (isLoadingMore) return;
if (lockRef.current) return;

lockRef.current = true;
Promise.resolve(loadMore()).finally(() => {
lockRef.current = false;
});
}, [enabled, inView, hasMore, isLoadingMore, loadMore]);

return ref;
};
18 changes: 18 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading