Skip to content

Commit

Permalink
feat: fetch spawn chains
Browse files Browse the repository at this point in the history
  • Loading branch information
marslavish committed Aug 24, 2024
1 parent 7392b37 commit 8e96a48
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useEffect, useState } from 'react';
import { useChain, useManager } from '@cosmos-kit/react';
import { Box, Combobox, Skeleton, Stack, Text } from '@interchain-ui/react';

import { useStarshipChains, useDetectBreakpoints } from '@/hooks';
import { useDetectBreakpoints, useSpawnChains } from '@/hooks';
import { chainStore, useChainStore } from '@/contexts';
import { chainOptions } from '@/config';

Expand All @@ -12,21 +12,20 @@ export const ChainDropdown = () => {
const { chain } = useChain(selectedChain);
const [input, setInput] = useState<string>(chain.pretty_name);
const { isMobile } = useDetectBreakpoints();
const { data: starshipChains } = useStarshipChains();
const { data: spawnChains } = useSpawnChains();

const [isChainsAdded, setIsChainsAdded] = useState(false);
const { addChains, getChainLogo } = useManager();

useEffect(() => {
if (starshipChains) {
// @ts-ignore
addChains(starshipChains.chains, starshipChains.assets);
if (spawnChains && !isChainsAdded) {
addChains(spawnChains.chains, spawnChains.assets);
setIsChainsAdded(true);
}
}, [starshipChains]);
}, [spawnChains, isChainsAdded]);

const chains = isChainsAdded
? chainOptions.concat(starshipChains?.chains ?? [])
? chainOptions.concat(spawnChains?.chains ?? [])
: chainOptions;

return (
Expand Down
1 change: 1 addition & 0 deletions examples/chain-template-spawn/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './theme';
export * from './wallets';
export * from './products';
export * from './breakpoints';
export * from './spawn';
11 changes: 11 additions & 0 deletions examples/chain-template-spawn/config/spawn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const api = {
baseUrl: 'http://127.0.0.1:8080',
endpoints: {
chain: '/chain_registry',
assets: '/chain_registry_assets',
},
};

export const SPAWN_API_BASE_URL = api.baseUrl;
export const SPAWN_CHAIN_URL = `${api.baseUrl}${api.endpoints.chain}`;
export const SPAWN_ASSETS_URL = `${api.baseUrl}${api.endpoints.assets}`;
1 change: 1 addition & 0 deletions examples/chain-template-spawn/hooks/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './useOutsideClick';
export * from './useMediaQuery';
export * from './useDetectBreakpoints';
export * from './useStarshipChains';
export * from './useSpawnChains';
41 changes: 41 additions & 0 deletions examples/chain-template-spawn/hooks/common/useSpawnChains.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { useQuery } from '@tanstack/react-query';
import { AssetList, Chain } from '@chain-registry/types';

import { SPAWN_CHAIN_URL, SPAWN_ASSETS_URL } from '@/config';

export const useSpawnChains = () => {
return useQuery({
queryKey: ['spawn-chains'],
queryFn: async () => {
try {
const [spawnChain, spawnAssets] = await Promise.all([
fetcher<Chain>(SPAWN_CHAIN_URL),
fetcher<AssetList>(SPAWN_ASSETS_URL),
]);

return {
chains: spawnChain ? [spawnChain] : [],
assets: spawnAssets ? [spawnAssets] : [],
};
} catch (error) {
console.error(error);
return undefined;
}
},
staleTime: Infinity,
cacheTime: Infinity,
refetchOnMount: false,
refetchOnReconnect: false,
});
};

const fetcher = async <T>(url: string): Promise<T | null> => {
try {
const response = await fetch(url);
const data = await response.json();
return data;
} catch (error) {
console.error(error);
return null;
}
};
4 changes: 2 additions & 2 deletions examples/chain-template-spawn/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -802,9 +802,9 @@ __metadata:
languageName: node
linkType: hard

"@cosmology/chain-template@workspace:.":
"@cosmology/chain-template-spawn@workspace:.":
version: 0.0.0-use.local
resolution: "@cosmology/chain-template@workspace:."
resolution: "@cosmology/chain-template-spawn@workspace:."
dependencies:
"@chain-registry/assets": "npm:1.63.5"
"@chain-registry/osmosis": "npm:1.61.3"
Expand Down

0 comments on commit 8e96a48

Please sign in to comment.