-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use react-query for infinite fetch scrolling
- Loading branch information
1 parent
4bee1b6
commit e26257f
Showing
17 changed files
with
219 additions
and
82 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
File renamed without changes.
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
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,33 @@ | ||
"use client"; | ||
import Link from "next/link"; | ||
import { useQueryClient } from "@tanstack/react-query"; | ||
import { getRepositoriesOnScroll } from "@/actions/getRepositories.action"; | ||
import { URL } from "@/constants"; | ||
|
||
export const PrefetchLink = () => { | ||
const queryClient = useQueryClient(); | ||
|
||
const prefetchRepositories = () => { | ||
queryClient | ||
.prefetchInfiniteQuery({ | ||
queryKey: ["repositories", { query: "", language: "" }], | ||
queryFn: ({ pageParam }) => | ||
getRepositoriesOnScroll({ cursor: pageParam }), | ||
initialPageParam: 0, | ||
getNextPageParam: (lastPage) => lastPage.nextCursor, | ||
pages: 1, | ||
staleTime: 1000 * 60 * 5, | ||
}) | ||
.catch(console.error); | ||
}; | ||
|
||
return ( | ||
<Link | ||
className="rounded-md bg-indigo-500 px-3.5 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-indigo-400 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-400" | ||
href={URL.REPOSITORIES} | ||
onMouseEnter={prefetchRepositories} | ||
> | ||
Get started | ||
</Link> | ||
); | ||
}; |
62 changes: 28 additions & 34 deletions
62
src/components/organisms/RepositoriesGrid/RepositoriesGridInfiniteScroll.tsx
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,52 +1,46 @@ | ||
"use client"; | ||
import { useInfiniteScroll } from "@/hooks/useInfiniteScroll"; | ||
|
||
import { useFetchInfiniteRepositories } from "@/hooks/useFetchInfiniteRepositories"; | ||
import { useFetchNextPage } from "@/hooks/useFetchNextPage"; | ||
import { RepositoryCard } from "@/components/organisms/RepositoryCard/_index"; | ||
import { RepositoriesLoader } from "@/components/organisms/RepositoriesGrid/RepositoriesLoader"; | ||
import { RepositoriesGridLayout } from "./RepositoriesGridLayout"; | ||
import { useRepositoriesContext } from "@/context/repositoriesContext"; | ||
import { useToggleFilter } from "@/stores/useToggleFilter"; | ||
import { getFilteredRepositories } from "@/utils/getFilteredRepositories"; | ||
import { User } from "@/types/prisma.type"; | ||
|
||
type Props = { | ||
user: User | null; | ||
query: string; | ||
language: string; | ||
}; | ||
|
||
export const RepositoriesGridInfiniteScroll = ({ query, language }: Props) => { | ||
const { data: initialRepositories, user } = useRepositoriesContext(); | ||
const { repositories, repositoriesAlreadyStarred, ref, isDisabled } = | ||
useInfiniteScroll({ | ||
initialRepositories, | ||
limit: 20, | ||
user, | ||
}); | ||
|
||
const { toggleFilter } = useToggleFilter(); | ||
export const RepositoriesGridInfiniteScroll = ({ | ||
user, | ||
query, | ||
language, | ||
}: Props) => { | ||
const { data, fetchNextPage, hasNextPage, isFetchingNextPage } = | ||
useFetchInfiniteRepositories({ query, language }); | ||
|
||
const filteredRepositories = getFilteredRepositories({ | ||
query, | ||
language, | ||
initialRepositories, | ||
repositories, | ||
user, | ||
toggleFilter, | ||
const { ref } = useFetchNextPage({ | ||
action: fetchNextPage, | ||
hasNextPage, | ||
}); | ||
|
||
console.log("rerendering RepositoriesGridInfiniteScroll"); | ||
|
||
return ( | ||
<> | ||
<RepositoriesGridLayout> | ||
{filteredRepositories.map((repository) => ( | ||
<RepositoryCard | ||
key={repository.id} | ||
user={user} | ||
repository={repository} | ||
repositoriesAlreadyStarred={repositoriesAlreadyStarred} | ||
/> | ||
))} | ||
</RepositoriesGridLayout> | ||
<RepositoriesLoader isDisabled={isDisabled} ref={ref} /> | ||
{data?.pages.map((page, i) => ( | ||
<RepositoriesGridLayout key={i}> | ||
{page.data.map((repository) => ( | ||
<RepositoryCard | ||
key={repository.id} | ||
user={user} | ||
repository={repository} | ||
// repositoriesAlreadyStarred={repositoriesAlreadyStarred} | ||
/> | ||
))} | ||
</RepositoriesGridLayout> | ||
))} | ||
<RepositoriesLoader isDisabled={isFetchingNextPage} ref={ref} /> | ||
</> | ||
); | ||
}; |
12 changes: 8 additions & 4 deletions
12
src/components/organisms/RepositoriesGrid/RepositoriesLoader.tsx
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
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { useSuspenseInfiniteQuery } from "@tanstack/react-query"; | ||
import { getRepositoriesOnScroll } from "@/actions/getRepositories.action"; | ||
|
||
type Props = { | ||
query: string; | ||
language: string; | ||
}; | ||
|
||
export const useFetchInfiniteRepositories = ({ query, language }: Props) => { | ||
const { data, fetchNextPage, hasNextPage, isFetchingNextPage, isLoading } = | ||
useSuspenseInfiniteQuery({ | ||
queryKey: ["repositories", { query, language }], | ||
queryFn: ({ pageParam }) => | ||
getRepositoriesOnScroll({ cursor: pageParam }), | ||
initialPageParam: 0, | ||
getNextPageParam: (lastPage) => lastPage.nextCursor, | ||
}); | ||
|
||
return { data, fetchNextPage, hasNextPage, isFetchingNextPage, isLoading }; | ||
}; |
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,26 @@ | ||
import { | ||
FetchNextPageOptions, | ||
InfiniteData, | ||
InfiniteQueryObserverResult, | ||
} from "@tanstack/react-query"; | ||
import { useEffect } from "react"; | ||
import { useInView } from "react-intersection-observer"; | ||
|
||
type Props = { | ||
action: ( | ||
options?: FetchNextPageOptions | undefined, | ||
) => Promise<InfiniteQueryObserverResult<InfiniteData<any>, Error>>; | ||
hasNextPage: boolean; | ||
}; | ||
|
||
export const useFetchNextPage = ({ action, hasNextPage }: Props) => { | ||
const { ref, inView } = useInView(); | ||
|
||
useEffect(() => { | ||
if (inView && hasNextPage) { | ||
void action(); | ||
} | ||
}, [inView]); | ||
|
||
return { ref, inView }; | ||
}; |
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
Oops, something went wrong.