-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(fe): refactor infinite scroll with tanstack query (#2244)
* feat(fe): add problem infinite list query option * feat(fe): add intersection area component * refactor(fe): refactor problem infinite table * feat(fe): add tanstack query error boundary * fix(fe): pass itemsPerPage parameter * chore(fe): use common error fallback ui
- Loading branch information
Showing
7 changed files
with
172 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,16 @@ | ||
'use client' | ||
|
||
import { Skeleton } from '@/components/shadcn/skeleton' | ||
import type { Problem } from '@/types/type' | ||
import FetchErrorFallback from '@/components/FetchErrorFallback' | ||
import { TanstackQueryErrorBoundary } from '@/components/TanstackQueryErrorBoundary' | ||
import { Suspense } from 'react' | ||
import ProblemInfiniteTable from './_components/ProblemInfiniteTable' | ||
import ProblemInfiniteTable, { | ||
ProblemInfiniteTableFallback | ||
} from './_components/ProblemInfiniteTable' | ||
|
||
export default function Problem() { | ||
export default function ProblemListPage() { | ||
return ( | ||
<Suspense | ||
fallback={ | ||
<> | ||
<div className="mt-4 flex"> | ||
<span className="w-5/12"> | ||
<Skeleton className="h-6 w-20" /> | ||
</span> | ||
<span className="w-2/12"> | ||
<Skeleton className="mx-auto h-6 w-20" /> | ||
</span> | ||
<span className="w-2/12"> | ||
<Skeleton className="mx-auto h-6 w-20" /> | ||
</span> | ||
<span className="w-2/12"> | ||
<Skeleton className="mx-auto h-6 w-20" /> | ||
</span> | ||
<span className="w-1/12"> | ||
<Skeleton className="mx-auto h-6 w-12" /> | ||
</span> | ||
</div> | ||
{[...Array(5)].map((_, i) => ( | ||
<Skeleton key={i} className="my-2 flex h-12 w-full rounded-xl" /> | ||
))} | ||
</> | ||
} | ||
> | ||
<ProblemInfiniteTable /> | ||
</Suspense> | ||
<TanstackQueryErrorBoundary fallback={FetchErrorFallback}> | ||
<Suspense fallback={<ProblemInfiniteTableFallback />}> | ||
<ProblemInfiniteTable /> | ||
</Suspense> | ||
</TanstackQueryErrorBoundary> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { | ||
infiniteQueryOptions, | ||
type QueryKey, | ||
type UnusedSkipTokenInfiniteOptions | ||
} from '@tanstack/react-query' | ||
|
||
interface Item { | ||
id: number | ||
} | ||
|
||
interface DataSet<T extends Item> { | ||
data: T[] | ||
total: number | ||
} | ||
|
||
type PageParam = number | undefined | ||
|
||
interface GetInfiniteQueryOptionsParams<T extends Item, K extends QueryKey> | ||
extends Partial< | ||
UnusedSkipTokenInfiniteOptions<DataSet<T>, Error, DataSet<T>, K, PageParam> | ||
> { | ||
queryKey: K | ||
itemsPerPage?: number | ||
} | ||
|
||
export const getInfiniteQueryOptions = <T extends Item, K extends QueryKey>({ | ||
queryFn, | ||
queryKey, | ||
itemsPerPage = 10 | ||
}: GetInfiniteQueryOptionsParams<T, K>) => { | ||
return infiniteQueryOptions({ | ||
queryKey, | ||
queryFn, | ||
staleTime: 0, | ||
initialPageParam: undefined, | ||
getNextPageParam: (lastPage) => { | ||
return lastPage.data.length === 0 || lastPage.data.length < itemsPerPage | ||
? undefined //페이지에 있는 아이템 수가 0이거나 itemsPerPage보다 작으면 undefined를 반환합니다. | ||
: lastPage.data.at(-1)?.id //cursor를 getData의 params로 넘겨줍니다. | ||
}, | ||
select: (data) => { | ||
const items = data.pages.flat().flatMap((page) => page.data) | ||
const total = data.pages.at(0)?.total | ||
|
||
return { items, total } | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { getProblemList, type GetProblemListRequest } from '../apis/problem' | ||
import { getInfiniteQueryOptions } from './infiniteQuery' | ||
|
||
export const problemQueries = { | ||
all: () => ['problem'] as const, | ||
infiniteList: (params: GetProblemListRequest) => | ||
getInfiniteQueryOptions({ | ||
queryKey: [...problemQueries.all(), 'list'] as const, | ||
queryFn: ({ pageParam }) => | ||
getProblemList({ | ||
...params, | ||
...(pageParam ? { cursor: pageParam } : undefined) | ||
}), | ||
itemsPerPage: params.take | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { useEffect, type ReactNode } from 'react' | ||
import { useInView } from 'react-intersection-observer' | ||
|
||
interface IntersectionAreaProps { | ||
children: ReactNode | ||
disabled: boolean | ||
onIntersect: () => void | ||
} | ||
|
||
export default function IntersectionArea({ | ||
children, | ||
disabled, | ||
onIntersect | ||
}: IntersectionAreaProps) { | ||
const { ref, inView } = useInView() | ||
|
||
useEffect(() => { | ||
if (inView && !disabled) { | ||
onIntersect() | ||
} | ||
}, [inView, disabled]) | ||
|
||
return ( | ||
<> | ||
{children} | ||
<div ref={ref} /> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use client' | ||
|
||
import { | ||
ErrorBoundary, | ||
type ErrorBoundaryFallbackProps | ||
} from '@suspensive/react' | ||
import { QueryErrorResetBoundary } from '@tanstack/react-query' | ||
import type { FunctionComponent, ReactNode } from 'react' | ||
|
||
interface TanstackQueryErrorBoundaryProps { | ||
children: ReactNode | ||
fallback: FunctionComponent<ErrorBoundaryFallbackProps<Error>> | ||
} | ||
|
||
export function TanstackQueryErrorBoundary({ | ||
children, | ||
fallback | ||
}: TanstackQueryErrorBoundaryProps) { | ||
return ( | ||
<QueryErrorResetBoundary> | ||
{({ reset }) => ( | ||
<ErrorBoundary onReset={reset} fallback={fallback}> | ||
{children} | ||
</ErrorBoundary> | ||
)} | ||
</QueryErrorResetBoundary> | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.