forked from open-sauced/app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseFetchAllHighlights.ts
32 lines (27 loc) · 1000 Bytes
/
useFetchAllHighlights.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
import { useState } from "react";
import useSWR, { Fetcher } from "swr";
import { publicApiFetcher } from "lib/utils/public-api-fetcher";
interface UseFetchUserHighlightsResponse {
data: DbHighlight[];
meta: Meta;
}
const useFetchAllHighlights = (repo = "") => {
const [page, setPage] = useState(1);
const [limit, setLimit] = useState(10);
const pageQuery = page ? `page=${page}` : "";
const limitQuery = limit ? `&limit=${limit}` : "";
const repoQuery = repo ? `&repo=${repo}` : "";
const { data, error, mutate } = useSWR<UseFetchUserHighlightsResponse, Error>(
`highlights/list?${pageQuery}${limitQuery}${repoQuery}`,
publicApiFetcher as Fetcher<UseFetchUserHighlightsResponse, 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,
setPage,
mutate,
};
};
export { useFetchAllHighlights };