aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/hooks/use-github-repo-meta/use-github-repo-meta.ts
blob: 888682e92fbf270643418103706f9b37ea7f3a8a (plain)
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
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>;
};