Skip to content

Commit

Permalink
Fetch all repos
Browse files Browse the repository at this point in the history
  • Loading branch information
amanape committed Nov 21, 2024
1 parent 97a6411 commit 3630c36
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
3 changes: 3 additions & 0 deletions frontend/__tests__/utils/extract-next-page-from-link.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ test("extractNextPageFromLink", () => {

const noNextLink = `<https://api.github.com/repositories/1300192/issues?page=2>; rel="prev", <https://api.github.com/repositories/1300192/issues?page=1>; rel="first"`;
expect(extractNextPageFromLink(noNextLink)).toBeNull();

const extra = `<https://api.github.com/user/repos?sort=pushed&page=2&per_page=3>; rel="next", <https://api.github.com/user/repos?sort=pushed&page=22&per_page=3>; rel="last"`;
expect(extractNextPageFromLink(extra)).toBe(2);
});
21 changes: 18 additions & 3 deletions frontend/src/hooks/query/use-user-repositories.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useInfiniteQuery } from "@tanstack/react-query";
import React from "react";
import {
isGitHubErrorReponse,
retrieveGitHubUserRepositories,
Expand All @@ -15,7 +16,11 @@ const userRepositoriesQueryFn = async ({
pageParam,
ghToken,
}: UserRepositoriesQueryFnProps) => {
const response = await retrieveGitHubUserRepositories(ghToken, pageParam);
const response = await retrieveGitHubUserRepositories(
ghToken,
pageParam,
100,
);

if (!response.ok) {
throw new Error("Failed to fetch repositories");
Expand All @@ -36,13 +41,23 @@ const userRepositoriesQueryFn = async ({
export const useUserRepositories = () => {
const { gitHubToken } = useAuth();

return useInfiniteQuery({
const repos = useInfiniteQuery({
queryKey: ["repositories", gitHubToken],
queryFn: async ({ pageParam }) =>
userRepositoriesQueryFn({ pageParam, ghToken: gitHubToken! }),
initialPageParam: 1,
getNextPageParam: (lastPage) => lastPage.nextPage,
enabled: !!gitHubToken,
select: (data) => data.pages.flatMap((page) => page.data),
});

// TODO: Once we create our custom dropdown component, we should fetch data onEndReached
// (nextui autocomplete doesn't support onEndReached nor is it compatible for extending)
const { isSuccess, isFetchingNextPage, hasNextPage, fetchNextPage } = repos;
React.useEffect(() => {
if (!isFetchingNextPage && isSuccess && hasNextPage) {
fetchNextPage();
}
}, [isFetchingNextPage, isSuccess, hasNextPage, fetchNextPage]);

return repos;
};
6 changes: 4 additions & 2 deletions frontend/src/routes/_oh._index/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function Home() {

const { data: config } = useConfig();
const { data: user } = useGitHubUser();
const { data: repositories } = useUserRepositories(gitHubToken);
const { data: repositories } = useUserRepositories();

const gitHubAuthUrl = useGitHubAuthUrl({
gitHubToken,
Expand All @@ -49,7 +49,9 @@ function Home() {
<div className="flex gap-4 w-full">
<GitHubRepositoriesSuggestionBox
handleSubmit={() => formRef.current?.requestSubmit()}
repositories={repositories || []}
repositories={
repositories?.pages.flatMap((page) => page.data) || []
}
gitHubAuthUrl={gitHubAuthUrl}
user={user || null}
// onEndReached={}
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/routes/_oh.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export default function MainApp() {
const dispatch = useDispatch();
const endSession = useEndSession();

// FIXME: Bad practice to use localStorage directly
const analyticsConsent = localStorage.getItem("analytics-consent");

const [accountSettingsModalOpen, setAccountSettingsModalOpen] =
Expand All @@ -83,7 +84,7 @@ export default function MainApp() {

const config = useConfig();
const user = useGitHubUser();
const { data: isAuthed, isFetched } = useIsAuthed({ gitHubToken });
const { data: isAuthed, isFetched } = useIsAuthed();
const aiConfigOptions = useAIConfigOptions();

const gitHubAuthUrl = useGitHubAuthUrl({
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/utils/extract-next-page-from-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* @returns The next page number or null if there is no next page
*/
export const extractNextPageFromLink = (link: string): number | null => {
const match = link.match(/<.*\?page=(\d+)>; rel="next"/);
const regex = /<[^>]*[?&]page=(\d+)(?:&[^>]*)?>; rel="next"/;
const match = link.match(regex);

return match ? parseInt(match[1], 10) : null;
};

0 comments on commit 3630c36

Please sign in to comment.