-
-
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.
- Loading branch information
Showing
3 changed files
with
70 additions
and
79 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,38 @@ | ||
import useSWR from "swr"; | ||
import { useCallback } from "react"; | ||
interface PaginatedDataResponse { | ||
readonly data: DBContributors[]; | ||
readonly data: DBContributorsPR[]; | ||
readonly meta: Meta; | ||
} | ||
|
||
type Response = | ||
| { type: "loading" } | ||
| { type: "error"; error: Error } | ||
// Todo: figure out meta | ||
| { type: "result"; data: DBContributors[]; meta: { itemCount: number } }; | ||
|
||
// We're not currently using this, we're just using useSWR directly inside ChildWithSWR | ||
// this needs useCallback wrap if we want to use it in the other component | ||
const useContributorData = () => { | ||
useCallback(async (owner: string, repo: string): Promise<Response> => { | ||
// useSWR is probably going to be a sticking point | ||
// eslint-disable-next-line react-hooks/rules-of-hooks | ||
const { data, error, mutate } = useSWR<PaginatedDataResponse, Error>( | ||
`repos/${owner}/${repo}/contributions` | ||
); | ||
|
||
if (!error && !data) { | ||
return { type: "loading" }; | ||
} | ||
|
||
if (error) { | ||
return { | ||
type: "error", | ||
error: error | ||
}; | ||
} | ||
|
||
return { | ||
type: "result", | ||
data: data?.data ?? [], | ||
meta: data?.meta ?? { itemCount: 0 } | ||
// commenting for now to appease build | ||
// mutate | ||
}; | ||
}, []); | ||
const useContributorData = (repo: string, startDate?: number, status?: "closed" | "open") => { | ||
const query = new URLSearchParams(); | ||
|
||
if (startDate) { | ||
query.set("prev_days_start_date", `${startDate}`); | ||
} | ||
if (status) { | ||
query.set("status", `${status}`); | ||
} | ||
query.set("repo", `${repo}`); | ||
|
||
query.set("limit", "100"); | ||
|
||
const baseEndpoint = "prs/search"; | ||
|
||
const endpointString = `${baseEndpoint}?${query.toString()}`; | ||
|
||
const { data, error, mutate } = useSWR<PaginatedDataResponse, Error>(repo ? endpointString : null); | ||
|
||
return { | ||
data: data?.data ?? [], | ||
isLoading: !data && !error, | ||
isError: Object.keys(error ?? {}).length > 0, | ||
meta: data?.meta ?? { itemCount: 0 }, | ||
mutate, | ||
}; | ||
}; | ||
// good catch [] | ||
|
||
export { useContributorData }; |
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