Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: chain routes #2

Merged
merged 4 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
0xArdy marked this conversation as resolved.
Show resolved Hide resolved

import { EcosystemChainData } from '~/types';
import { CustomHead } from '~/components';
import { useData } from '~/hooks/useContext/useData';
0xArdy marked this conversation as resolved.
Show resolved Hide resolved
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
Loading