Skip to content

Commit

Permalink
fix: A few things
Browse files Browse the repository at this point in the history
  • Loading branch information
drake-nathan committed Oct 11, 2023
1 parent 21a34a5 commit 2b39fe9
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 5 deletions.
4 changes: 3 additions & 1 deletion components/LandingPage/Projects/Card/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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`;
Expand Down
3 changes: 1 addition & 2 deletions components/ProjectPage/Details/Details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ const Details = ({ project }: Props): JSX.Element => {
<>
<St.Image src={image} alt="Project image" />
<St.ProjectImageNameContainer>
{/* NOTE Can leave out token id here for now */}
<St.ProjectImageName>{`${name}`}</St.ProjectImageName>
<St.ProjectImageName>{name}</St.ProjectImageName>
</St.ProjectImageNameContainer>
</>
)}
Expand Down
2 changes: 1 addition & 1 deletion components/TokenPage/Module/TokenInfo/TokenInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const TokenInfo = ({
onClick={() => setTab("more-info")}
$active={tab === "more-info"}
>
<h3>More Info</h3>
<h3>{projectSlug === "haiku" ? "AI Analysis" : "More Info"}</h3>
</St.Tab>
)}

Expand Down
11 changes: 11 additions & 0 deletions hooks/useCurrentSupply.ts
Original file line number Diff line number Diff line change
@@ -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<Record<string, number>, Error>(
"currentSupplies",
() => fetchCurrentSupplies(),
);

return currentSupplies?.[projectSlug];
};
44 changes: 43 additions & 1 deletion services/azureApi/fetches.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -50,3 +51,44 @@ export const fetchTxCounts = async (projectSlug: string): Promise<TxCounts> => {

return data;
};

export const fetchCurrentSupplies = async (): Promise<
Record<string, number>
> => {
const url = `${rootApiUrl}/projects`;

let projects: unknown;
try {
projects = (await axios.get<unknown>(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<string, number>;
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;
};

0 comments on commit 2b39fe9

Please sign in to comment.