diff options
Diffstat (limited to 'src/utils/hooks/use-github-repo-meta')
| -rw-r--r-- | src/utils/hooks/use-github-repo-meta/use-github-repo-meta.ts | 32 |
1 files changed, 20 insertions, 12 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 index 888682e..3567311 100644 --- 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 @@ -3,19 +3,27 @@ import { type FetchGithubRepoMetaInput, fetchGithubRepoMeta, } from '../../../services/github'; -import type { GithubRepositoryMeta, Maybe } from '../../../types'; +import type { GithubRepositoryMeta, Maybe, Nullable } from '../../../types'; -export type UseGithubRepoMetaReturn<T extends Maybe<GithubRepositoryMeta>> = { - isError: boolean; - isLoading: boolean; - isValidating: boolean; - meta: T extends undefined - ? Maybe<GithubRepositoryMeta> - : GithubRepositoryMeta; +export type UseGithubRepoMetaReturn< + I extends Nullable<FetchGithubRepoMetaInput>, + T extends Maybe<GithubRepositoryMeta>, +> = { + isError: I extends null ? false : boolean; + isLoading: I extends null ? false : boolean; + isValidating: I extends null ? false : boolean; + meta: I extends null + ? null + : T extends undefined + ? Maybe<GithubRepositoryMeta> + : GithubRepositoryMeta; }; -export const useGithubRepoMeta = <T extends Maybe<GithubRepositoryMeta>>( - input: FetchGithubRepoMetaInput, +export const useGithubRepoMeta = < + I extends Nullable<FetchGithubRepoMetaInput>, + T extends Maybe<GithubRepositoryMeta>, +>( + input: I, fallback?: T ) => { const { data, error, isLoading, isValidating } = useSWR( @@ -30,8 +38,8 @@ export const useGithubRepoMeta = <T extends Maybe<GithubRepositoryMeta>>( return { isError: !!error, - isLoading, + isLoading: input !== null && isLoading && !data, isValidating, meta: data, - } as UseGithubRepoMetaReturn<T>; + } as UseGithubRepoMetaReturn<I, T>; }; |
