diff options
| author | Armand Philippot <git@armandphilippot.com> | 2023-12-18 15:22:14 +0100 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2023-12-18 18:19:58 +0100 |
| commit | 55ea3e415b31b834004bee9f5367fbfb420bbeef (patch) | |
| tree | b0be263db09e2c921d0d4b02b36a139ad7fe82aa /src/utils/hooks/use-github-repo-meta | |
| parent | d592085dc0fec023dd9f3437d4c756d402ed8c8f (diff) | |
refactor(pages): merge Github/Gitlab overview on project pages
By using conditional fetchning we can avoid to duplicate the
ProjectOverview component and be more accurate about what data is
loading.
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>; }; |
