Skip to content
Merged
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
42 changes: 32 additions & 10 deletions src/pages/all-contribution-page.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useEffect, useRef } from 'react';
import { useInfiniteQuery } from '@tanstack/react-query';
import { getAllContributions } from '../apis/contribution';
import logo from '../assets/icons/logo.svg';
Expand Down Expand Up @@ -82,6 +83,7 @@ function ContributionFeedCard({ feed }: { feed: ContributionFeed }) {
}

export default function AllContributionPage() {
const loadMoreRef = useRef<HTMLDivElement>(null);
const {
data,
isPending,
Expand All @@ -102,6 +104,29 @@ export default function AllContributionPage() {
lastPage.hasNext ? lastPage.nextCursor : undefined,
});

useEffect(() => {
const target = loadMoreRef.current;

if (!target) return;

const observer = new IntersectionObserver(
([entry]) => {
if (
entry?.isIntersecting &&
hasNextPage &&
!isFetchingNextPage
) {
void fetchNextPage();
}
},
{ rootMargin: '200px 0px' },
);

observer.observe(target);

return () => observer.disconnect();
}, [fetchNextPage, hasNextPage, isFetchingNextPage]);

if (isPending) {
return (
<div className='flex min-h-[calc(100dvh-56px)] items-center justify-center bg-[#F9FBFB]'>
Expand Down Expand Up @@ -152,16 +177,13 @@ export default function AllContributionPage() {
</p>
)}

{hasNextPage && (
<button
type='button'
onClick={() => fetchNextPage()}
disabled={isFetchingNextPage}
className='mt-5 flex h-12 w-full items-center justify-center rounded-full bg-white text-sm font-bold text-main-green2 shadow-lg shadow-black/5 disabled:cursor-not-allowed disabled:opacity-60'
>
{isFetchingNextPage ? '불러오는 중...' : '더 보기'}
</button>
)}
<div
ref={loadMoreRef}
className='flex min-h-12 items-center justify-center'
aria-live='polite'
>
{isFetchingNextPage && <LoadingSpinner />}
</div>
</div>
</div>
);
Expand Down
Loading