From 2b39fe933e2718efc977ee6582be64d41c9d70d3 Mon Sep 17 00:00:00 2001 From: Nathan Drake Date: Wed, 11 Oct 2023 18:04:57 -0500 Subject: [PATCH] fix: A few things --- components/LandingPage/Projects/Card/Card.tsx | 4 +- components/ProjectPage/Details/Details.tsx | 3 +- .../TokenPage/Module/TokenInfo/TokenInfo.tsx | 2 +- hooks/useCurrentSupply.ts | 11 +++++ services/azureApi/fetches.ts | 44 ++++++++++++++++++- 5 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 hooks/useCurrentSupply.ts diff --git a/components/LandingPage/Projects/Card/Card.tsx b/components/LandingPage/Projects/Card/Card.tsx index 689b683..d52fc9f 100644 --- a/components/LandingPage/Projects/Card/Card.tsx +++ b/components/LandingPage/Projects/Card/Card.tsx @@ -2,6 +2,7 @@ import Link from "next/link"; import { intlNumberFormat } from "utils/helpers"; import * as St from "./Card.styled"; import { Project } from "../../../staticData/projects"; +import { useCurrentSupply } from "hooks/useCurrentSupply"; interface Props { project: Project; @@ -15,11 +16,12 @@ const Card = ({ project }: Props): JSX.Element => { local, projectSlug, externalUrl, - currentSupply, maxSupply, status, } = project; + const currentSupply = useCurrentSupply(projectSlug); + const supplyText = `${ currentSupply ? intlNumberFormat(currentSupply) : 0 }/${intlNumberFormat(maxSupply)} Minted`; diff --git a/components/ProjectPage/Details/Details.tsx b/components/ProjectPage/Details/Details.tsx index 8514d71..73004e0 100644 --- a/components/ProjectPage/Details/Details.tsx +++ b/components/ProjectPage/Details/Details.tsx @@ -56,8 +56,7 @@ const Details = ({ project }: Props): JSX.Element => { <> - {/* NOTE Can leave out token id here for now */} - {`${name}`} + {name} )} diff --git a/components/TokenPage/Module/TokenInfo/TokenInfo.tsx b/components/TokenPage/Module/TokenInfo/TokenInfo.tsx index 054e272..10d3088 100644 --- a/components/TokenPage/Module/TokenInfo/TokenInfo.tsx +++ b/components/TokenPage/Module/TokenInfo/TokenInfo.tsx @@ -41,7 +41,7 @@ const TokenInfo = ({ onClick={() => setTab("more-info")} $active={tab === "more-info"} > -

More Info

+

{projectSlug === "haiku" ? "AI Analysis" : "More Info"}

)} diff --git a/hooks/useCurrentSupply.ts b/hooks/useCurrentSupply.ts new file mode 100644 index 0000000..c12f8af --- /dev/null +++ b/hooks/useCurrentSupply.ts @@ -0,0 +1,11 @@ +import { useQuery } from "react-query"; +import { fetchCurrentSupplies } from "services/azureApi/fetches"; + +export const useCurrentSupply = (projectSlug: string): number | undefined => { + const { data: currentSupplies } = useQuery, Error>( + "currentSupplies", + () => fetchCurrentSupplies(), + ); + + return currentSupplies?.[projectSlug]; +}; diff --git a/services/azureApi/fetches.ts b/services/azureApi/fetches.ts index 9274a60..46d09a8 100644 --- a/services/azureApi/fetches.ts +++ b/services/azureApi/fetches.ts @@ -1,5 +1,6 @@ import axios from "axios"; -import { CollectionResponse, IProject, IToken, TxCounts } from "./types"; +import { z } from "zod"; +import type { CollectionResponse, IProject, IToken, TxCounts } from "./types"; const rootApiUrl = process.env.NEXT_PUBLIC_API_ROOT; @@ -50,3 +51,44 @@ export const fetchTxCounts = async (projectSlug: string): Promise => { return data; }; + +export const fetchCurrentSupplies = async (): Promise< + Record +> => { + const url = `${rootApiUrl}/projects`; + + let projects: unknown; + try { + projects = (await axios.get(url)).data; + } catch (error) { + throw new Error(`Error fetching current supplies`, { + cause: error, + }); + } + + const schema = z.array( + z.object({ + project_slug: z.string(), + current_supply: z.number(), + }), + ); + + let currentSupplies: Record; + try { + const parsedProjects = schema.parse(projects); + + currentSupplies = parsedProjects.reduce( + (acc, project) => ({ + ...acc, + [project.project_slug]: project.current_supply, + }), + {}, + ); + } catch (error) { + throw new Error(`Error parsing current supplies`, { + cause: error, + }); + } + + return currentSupplies; +};