diff --git a/packages/grant-explorer/src/features/api/oso.ts b/packages/grant-explorer/src/features/api/oso.ts index c80393bfc2..48e405abd2 100644 --- a/packages/grant-explorer/src/features/api/oso.ts +++ b/packages/grant-explorer/src/features/api/oso.ts @@ -3,7 +3,7 @@ import useSWR from "swr"; import { Hex } from "viem"; import { gql, GraphQLClient } from "graphql-request"; -const osoApiKey = process.env.REACT_APP_OSO_API_KEY; +const osoApiKey = process.env.REACT_APP_OSO_API_KEY as string; const osoUrl = "https://opensource-observer.hasura.app/v1/graphql"; const graphQLClient = new GraphQLClient(osoUrl, { headers: { @@ -14,14 +14,14 @@ let hasFetched = false; interface IOSOId { artifacts_by_project: { - project_id : Hex; + project_id: Hex; }; } export interface IOSOStats { code_metrics_by_project: { - contributors : number; - first_commit_date : number; + contributors: number; + first_commit_date: number; }; events_monthly_to_project: [ { @@ -52,43 +52,43 @@ export interface IOSOStats { } export function useOSO(projectGithub?: string) { - const emptyReturn : IOSOStats = { - code_metrics_by_project: + const emptyReturn: IOSOStats = { + code_metrics_by_project: { + contributors: 0, + first_commit_date: 0, + }, + events_monthly_to_project: [ { - contributors : 0, - first_commit_date : 0 + bucket_month: 0, + amount: 0, }, - events_monthly_to_project: [ - { - bucket_month: 0, - amount: 0, - }, - { - bucket_month: 0, - amount: 0, - }, - { - bucket_month: 0, - amount: 0, - }, - { - bucket_month: 0, - amount: 0, - }, - { - bucket_month: 0, - amount: 0, - }, - { - bucket_month: 0, - amount: 0, - } - ] + { + bucket_month: 0, + amount: 0, + }, + { + bucket_month: 0, + amount: 0, + }, + { + bucket_month: 0, + amount: 0, + }, + { + bucket_month: 0, + amount: 0, + }, + { + bucket_month: 0, + amount: 0, + }, + ], }; const [stats, setStats] = useState(null); const getStatsFor = async (projectRegistryGithub: string) => { - if (!osoApiKey) throw new Error("OpenSourceObserver API key not set."); + if (osoApiKey === "") + throw new Error("OpenSourceObserver API key not set."); const queryId = gql`{ artifacts_by_project(where: {artifact_name: {_ilike: "%${projectRegistryGithub}/%"}} distinct_on: project_id @@ -106,8 +106,8 @@ export function useOSO(projectGithub?: string) { return; } - const parsedId : IOSOId = { - artifacts_by_project: idData.artifacts_by_project[0] + const parsedId: IOSOId = { + artifacts_by_project: idData.artifacts_by_project[0], }; const queryStats = gql`{ @@ -125,7 +125,8 @@ export function useOSO(projectGithub?: string) { } }`; - const items: IOSOStats = await graphQLClient.request(queryStats); + const items: IOSOStats = + await graphQLClient.request(queryStats); if (!Array.isArray(items.code_metrics_by_project)) { setStats(emptyReturn); @@ -133,19 +134,18 @@ export function useOSO(projectGithub?: string) { } if (items.events_monthly_to_project.length === 6) { - const parsedItems : IOSOStats = { + const parsedItems: IOSOStats = { code_metrics_by_project: items.code_metrics_by_project[0], - events_monthly_to_project: items.events_monthly_to_project + events_monthly_to_project: items.events_monthly_to_project, }; setStats(parsedItems); - } else { - const parsedItems : IOSOStats = { + } else { + const parsedItems: IOSOStats = { code_metrics_by_project: items.code_metrics_by_project[0], - events_monthly_to_project: emptyReturn.events_monthly_to_project + events_monthly_to_project: emptyReturn.events_monthly_to_project, }; setStats(parsedItems); } - } catch (e) { console.error(`No stats found for project: ${projectGithub}`); console.error(e); @@ -153,14 +153,13 @@ export function useOSO(projectGithub?: string) { } }; - const { isLoading } = useSWR(osoUrl, - { - fetcher: async () => projectGithub && getStatsFor(projectGithub), - revalidateOnMount: true, - } - ); + const { isLoading } = useSWR(osoUrl, { + fetcher: async () => projectGithub && getStatsFor(projectGithub), + revalidateOnMount: true, + }); - if ( stats === null && !hasFetched) projectGithub && getStatsFor(projectGithub); + if (stats === null && !hasFetched) + projectGithub && getStatsFor(projectGithub); return { /** * Fetch OSO for stats on a project @@ -174,4 +173,3 @@ export function useOSO(projectGithub?: string) { isStatsLoading: isLoading, }; } -