From 55ea3e415b31b834004bee9f5367fbfb420bbeef Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Mon, 18 Dec 2023 15:22:14 +0100 Subject: 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. --- .../use-github-repo-meta/use-github-repo-meta.ts | 32 ++++++++++++++-------- 1 file changed, 20 insertions(+), 12 deletions(-) (limited to 'src/utils/hooks/use-github-repo-meta') 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> = { - isError: boolean; - isLoading: boolean; - isValidating: boolean; - meta: T extends undefined - ? Maybe - : GithubRepositoryMeta; +export type UseGithubRepoMetaReturn< + I extends Nullable, + T extends Maybe, +> = { + 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; }; -export const useGithubRepoMeta = >( - input: FetchGithubRepoMetaInput, +export const useGithubRepoMeta = < + I extends Nullable, + T extends Maybe, +>( + input: I, fallback?: T ) => { const { data, error, isLoading, isValidating } = useSWR( @@ -30,8 +38,8 @@ export const useGithubRepoMeta = >( return { isError: !!error, - isLoading, + isLoading: input !== null && isLoading && !data, isValidating, meta: data, - } as UseGithubRepoMetaReturn; + } as UseGithubRepoMetaReturn; }; -- cgit v1.2.3