Skip to content

Commit

Permalink
feat: chain routes
Browse files Browse the repository at this point in the history
  • Loading branch information
0xtiti committed Jul 15, 2024
1 parent 7ab5a8d commit 285c55b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 20 deletions.
49 changes: 46 additions & 3 deletions src/pages/[chain]/index.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,57 @@
import { GetStaticPaths, GetStaticProps } from 'next';
import { useEffect } from 'react';

import { ecosystemChainData } from '~/types';
import { CustomHead } from '~/components';
import { useData } from '~/hooks/useContext/useData';
import { fetchEcosystemData } from '~/utils';

interface ChainProps {
chain: ecosystemChainData;
}

const Chain = ({ chain }: ChainProps) => {
const { setSelectedChainId } = useData();

const Chain = () => {
const title = 'Chain placeholder';
useEffect(() => {
setSelectedChainId(chain.id);
}, [chain.id, setSelectedChainId]);

return (
<>
<CustomHead title={title} />
<CustomHead title={chain.name} />
<h1>{chain.name}</h1>
{/* TODO: Add chain page containers */}
</>
);
};

export const getStaticPaths: GetStaticPaths = async () => {
const ecosystemData = await fetchEcosystemData();
const chains = ecosystemData.chains;

const paths = chains.map((chain: ecosystemChainData) => ({
params: { chain: chain.id.toString() },
}));

return { paths, fallback: false };
};

export const getStaticProps: GetStaticProps = async ({ params }) => {
const ecosystemData = await fetchEcosystemData();
const chains = ecosystemData.chains;
const chainId = parseInt(params?.chain as string);
const chain = chains.find((chain: ecosystemChainData) => chain.id === chainId);

if (!chain) {
return { notFound: true };
}

return {
props: {
chain,
},
};
};

export default Chain;
16 changes: 8 additions & 8 deletions src/providers/DataProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { ChainData, EcosystemData } from '~/types';
import { fetchEcosystemData, fetchChainData } from '~/utils';

type ContextType = {
selectedChain?: ChainData;
setSelectedChain: (val: ChainData) => void;
selectedChainId?: number;
setSelectedChainId: (val: number) => void;

isEcosystemLoading: boolean;
isChainLoading: boolean;
Expand All @@ -24,7 +24,7 @@ interface DataProps {
export const DataContext = createContext({} as ContextType);

export const DataProvider = ({ children }: DataProps) => {
const [selectedChain, setSelectedChain] = useState<ChainData>();
const [selectedChainId, setSelectedChainId] = useState<number>();
const router = useRouter();

const {
Expand All @@ -42,9 +42,9 @@ export const DataProvider = ({ children }: DataProps) => {
isError: isChainError,
refetch: refetchChainData,
} = useQuery({
queryKey: ['chainData', selectedChain?.chainId],
queryFn: () => fetchChainData(selectedChain!.chainId!),
enabled: !!selectedChain?.chainId,
queryKey: ['chainData', selectedChainId],
queryFn: () => fetchChainData(selectedChainId!),
enabled: !!selectedChainId,
});

useEffect(() => {
Expand All @@ -56,8 +56,8 @@ export const DataProvider = ({ children }: DataProps) => {
return (
<DataContext.Provider
value={{
selectedChain,
setSelectedChain,
selectedChainId,
setSelectedChainId,
isEcosystemLoading,
isChainLoading,
ecosystemData,
Expand Down
20 changes: 11 additions & 9 deletions src/types/Data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,18 @@ export interface ChainData {
};
}

export interface EcosystemData {
chain: {
name: string;
id: number;
nativeToken: string;
tvl: {
[token: string]: number;
};
type: string;
export interface ecosystemChainData {
name: string;
id: number;
nativeToken: string;
tvl: {
[token: string]: number;
};
type: string;
}

export interface EcosystemData {
chains: ecosystemChainData[];
tvl: {
[token: string]: number;
}[];
Expand Down

0 comments on commit 285c55b

Please sign in to comment.