Skip to content

Commit

Permalink
feat(ExecutionCode): added useCeloExplorer() hook
Browse files Browse the repository at this point in the history
this will dynamically load the righ celo explorer
api url depending on the current chain id
  • Loading branch information
chapati23 committed Feb 28, 2024
1 parent ba64ef8 commit 6479171
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import BlockExplorerLink from "@/app/components/_shared/block-explorer-link/block-explorer-link.component";

import {
GetContractsInfo,
type GetContractsInfoQuery,
type ProposalCall,
} from "@/app/graphql";
import { useCeloExplorerApi } from "@/app/hooks/useCeloExplorer";
import { useQuery } from "@apollo/experimental-nextjs-app-support/ssr";
import { useMemo } from "react";
import { decodeFunctionData } from "viem";
Expand All @@ -21,11 +21,13 @@ type ContractInfo = {
};

export default function ExecutionCode({ calls }: Props) {
const { name: apiName } = useCeloExplorerApi();

const { data, error: apolloError } = useQuery(GetContractsInfo, {
variables: {
addresses: calls.map((call) => call.target.id),
},
context: { apiName: "celoExplorer" },
context: { apiName },
skip: !calls.length,
});

Expand Down
41 changes: 41 additions & 0 deletions app/hooks/useCeloExplorer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { useChainId } from "wagmi";
import loadEnvVar from "../helpers/load-env-var";

const CELO_EXPLORER_API_URL = loadEnvVar(
process.env.NEXT_PUBLIC_CELO_EXPLORER_API_URL,
);
const CELO_EXPLORER_API_URL_ALFAJORES = loadEnvVar(
process.env.NEXT_PUBLIC_CELO_EXPLORER_API_URL_ALFAJORES,
);
const CELO_EXPLORER_API_URL_BAKLAVA = loadEnvVar(
process.env.NEXT_PUBLIC_CELO_EXPLORER_API_URL_BAKLAVA,
);

type CeloExplorerApi = {
name: "celoExplorer" | "celoExplorerAlfajores" | "celoExplorerBaklava";
url: string;
};

export const useCeloExplorerApi = (): CeloExplorerApi => {
const chainId = useChainId();
let result: CeloExplorerApi = {
name: "celoExplorer",
url: CELO_EXPLORER_API_URL,
};

if (chainId === 44787) {
result = {
name: "celoExplorerAlfajores",
url: CELO_EXPLORER_API_URL_ALFAJORES,
};
}

if (chainId === 62320) {
result = {
name: "celoExplorerBaklava",
url: CELO_EXPLORER_API_URL_BAKLAVA,
};
}

return result;
};

0 comments on commit 6479171

Please sign in to comment.