aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/hooks/use-github-repo-meta/use-github-repo-meta.ts
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2023-12-06 18:20:54 +0100
committerArmand Philippot <git@armandphilippot.com>2023-12-07 19:12:11 +0100
commitb8eb008dd5927fb736e56699637f5f8549965eae (patch)
tree648274babea3d3d09ed3e0f5f1fef013f94158fb /src/utils/hooks/use-github-repo-meta/use-github-repo-meta.ts
parent802285872a2c57e7a5e130f32a2b45497d7687f1 (diff)
refactor(hooks): replace useGithubApi with useGithubRepoMeta
* use GraphQL API instead of REST (the inconvenient however is that we now need an authorization token...) * move fetcher in services * add tests * mock response using MSW
Diffstat (limited to 'src/utils/hooks/use-github-repo-meta/use-github-repo-meta.ts')
-rw-r--r--src/utils/hooks/use-github-repo-meta/use-github-repo-meta.ts37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/utils/hooks/use-github-repo-meta/use-github-repo-meta.ts b/src/utils/hooks/use-github-repo-meta/use-github-repo-meta.ts
new file mode 100644
index 0000000..888682e
--- /dev/null
+++ b/src/utils/hooks/use-github-repo-meta/use-github-repo-meta.ts
@@ -0,0 +1,37 @@
+import useSWR from 'swr';
+import {
+ type FetchGithubRepoMetaInput,
+ fetchGithubRepoMeta,
+} from '../../../services/github';
+import type { GithubRepositoryMeta, Maybe } from '../../../types';
+
+export type UseGithubRepoMetaReturn<T extends Maybe<GithubRepositoryMeta>> = {
+ isError: boolean;
+ isLoading: boolean;
+ isValidating: boolean;
+ meta: T extends undefined
+ ? Maybe<GithubRepositoryMeta>
+ : GithubRepositoryMeta;
+};
+
+export const useGithubRepoMeta = <T extends Maybe<GithubRepositoryMeta>>(
+ input: FetchGithubRepoMetaInput,
+ fallback?: T
+) => {
+ const { data, error, isLoading, isValidating } = useSWR(
+ input,
+ fetchGithubRepoMeta,
+ {
+ fallbackData: fallback,
+ }
+ );
+
+ if (error) console.error(error);
+
+ return {
+ isError: !!error,
+ isLoading,
+ isValidating,
+ meta: data,
+ } as UseGithubRepoMetaReturn<T>;
+};