Skip to content

Commit

Permalink
update type/check
Browse files Browse the repository at this point in the history
  • Loading branch information
codenamejason committed Apr 30, 2024
1 parent 6838367 commit b60de9a
Showing 1 changed file with 50 additions and 52 deletions.
102 changes: 50 additions & 52 deletions packages/grant-explorer/src/features/api/oso.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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: [
{
Expand Down Expand Up @@ -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<IOSOStats | null>(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
Expand All @@ -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`{
Expand All @@ -125,42 +125,41 @@ export function useOSO(projectGithub?: string) {
}
}`;

const items: IOSOStats = await graphQLClient.request<IOSOStats>(queryStats);
const items: IOSOStats =
await graphQLClient.request<IOSOStats>(queryStats);

if (!Array.isArray(items.code_metrics_by_project)) {
setStats(emptyReturn);
return;
}

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);
setStats(emptyReturn);
}
};

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
Expand All @@ -174,4 +173,3 @@ export function useOSO(projectGithub?: string) {
isStatsLoading: isLoading,
};
}

0 comments on commit b60de9a

Please sign in to comment.