Skip to content

Commit

Permalink
fix: add reach end status (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
izayl authored May 8, 2023
1 parent f411223 commit 1f62c00
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/loud-lies-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"useful-dependents": patch
---

fix: add reach end status to show correctly end status
10 changes: 6 additions & 4 deletions src/components/result.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ export const Result: React.FC = () => {
const repo = searchParams.get('repo')
const packageId = searchParams.get('package_id')
const [loadPage, setLoadPage] = useState(5)
const { data, setSize, packages, size, isLoading } = useDependents(repo as string, packageId as string)
const { data, setSize, packages, size, isLoading, isFinished } = useDependents(repo as string, packageId as string)
const [ignoreZeroStar, setIgnoreZeroStar] = useState(true)
const sortedData = useMemo(() => {
const filteredData = ignoreZeroStar ? data.filter(d => d.stars > 0) : data
return orderBy(filteredData ?? [], 'stars', 'desc')
}, [data, ignoreZeroStar])
const isFetching = useMemo(() => {
const last = 30 * (loadPage - 1)
if (isFinished) return false
return size > 0 && data && typeof data[last] === 'undefined'
}, [size, data, loadPage])
}, [size, data, loadPage, isFinished])

useEffect(() => {
if (repo && !isLoading) {
Expand Down Expand Up @@ -83,15 +84,16 @@ export const Result: React.FC = () => {
<div className="rounded-b-md border text-center p-2">
{isFetching ? (
<Icons.loading className="animate-spin h-4 w-4 my-2 mx-auto " />
) : (
) : null}
{!isFetching && !isFinished ? (
<Button
className=" font-semibold"
variant="link"
onClick={() => setLoadPage(p => p + 5)}
>
Load More
</Button>
)}
) : null}
</div>
</div>
</>
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useDependents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const useDependents = (repo: string, packageId?: string) => {

return {
...response,
isFinished: response.data?.some(d => !d?.nextURL),
data: data.filter(Boolean),
packages: response.data ? response.data[0]?.packages : [],
}
Expand Down

0 comments on commit 1f62c00

Please sign in to comment.