forked from open-sauced/app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseListContributions.ts
47 lines (37 loc) · 1.17 KB
/
useListContributions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { useState } from "react";
import useSWR, { Fetcher } from "swr";
import { publicApiFetcher } from "lib/utils/public-api-fetcher";
interface PaginatedResponse {
readonly data: DbRepoPREvents[];
readonly meta: Meta;
}
const useListContributions = (listId: string, intialLimit = 1000, range = 30) => {
const [page, setPage] = useState(1);
const [limit, setLimit] = useState(intialLimit);
const query = new URLSearchParams();
if (page) {
query.set("page", `${page}`);
}
if (limit) {
query.set("limit", `${limit}`);
}
query.set("listId", listId);
query.set("range", `${range}`);
const baseEndpoint = "prs/search";
const endpointString = `${baseEndpoint}?${query.toString()}`;
const { data, error, mutate } = useSWR<PaginatedResponse, Error>(
listId ? endpointString : null,
publicApiFetcher as Fetcher<PaginatedResponse, Error>
);
return {
data: data?.data ?? [],
meta: data?.meta ?? { itemCount: 0, limit: 0, page: 0, hasNextPage: false, hasPreviousPage: false, pageCount: 0 },
isLoading: !error && !data,
isError: !!error,
mutate,
page,
setPage,
setLimit,
};
};
export default useListContributions;