Skip to content

Commit

Permalink
feat: select spawn chain by default if it's running
Browse files Browse the repository at this point in the history
  • Loading branch information
marslavish committed Oct 9, 2024
1 parent 9aea9b6 commit 2c00c33
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 23 deletions.
24 changes: 15 additions & 9 deletions examples/spawn/components/common/Header/ChainDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,23 @@ export const ChainDropdown = () => {
const { chain } = useChain(selectedChain);
const [input, setInput] = useState<string>(chain.pretty_name);
const { isMobile } = useDetectBreakpoints();
const { data: spawnChains, refetch } = useSpawnChains();
const { data: spawnData, refetch, isLoading } = useSpawnChains();

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

useEffect(() => {
if (
spawnChains?.chains?.length &&
spawnChains?.assets?.length &&
!isChainsAdded
) {
addChains(spawnChains.chains, spawnChains.assets, getSignerOptions());
if (spawnData && !isChainsAdded) {
addChains(spawnData.chains, spawnData.assets, getSignerOptions());
setIsChainsAdded(true);
}
}, [spawnChains, isChainsAdded]);
}, [spawnData, isChainsAdded]);

useEffect(() => {
if (spawnData && !isLoading) {
chainStore.setSelectedChain(spawnData.chains[0].chain_name);
}
}, [isLoading]);

const onOpenChange = (isOpen: boolean) => {
if (isOpen && !isChainsAdded) {
Expand All @@ -36,9 +38,13 @@ export const ChainDropdown = () => {
};

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

if (isLoading) {
return null;
}

return (
<Combobox
onInputChange={(input) => {
Expand Down
5 changes: 3 additions & 2 deletions examples/spawn/components/common/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { RxHamburgerMenu } from 'react-icons/rx';

import { ChainDropdown } from './ChainDropdown';
import { Button } from '../Button';
import { useDetectBreakpoints } from '@/hooks';
import { useDetectBreakpoints, useSpawnChains } from '@/hooks';
import { AddressButton } from './AddressButton';

interface HeaderProps {
Expand All @@ -15,6 +15,7 @@ interface HeaderProps {
export const Header = ({ onOpenSidebar }: HeaderProps) => {
const { theme, setTheme } = useTheme();
const { isDesktop, isMobile } = useDetectBreakpoints();
const { isLoading } = useSpawnChains();

const brandLogo = useColorModeValue(
'/logos/brand-logo.svg',
Expand Down Expand Up @@ -45,7 +46,7 @@ export const Header = ({ onOpenSidebar }: HeaderProps) => {
</Link>
)}
<Box display="flex" alignItems="center" gap="10px">
<AddressButton />
{!isLoading && <AddressButton />}
<ChainDropdown />
<Button
leftIcon={theme === 'dark' ? 'moonLine' : 'sunLine'}
Expand Down
9 changes: 4 additions & 5 deletions examples/spawn/hooks/common/useSpawnChains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ export const useSpawnChains = () => {
fetcher<AssetList>(SPAWN_ASSETS_URL),
]);

return {
chains: spawnChain ? [spawnChain] : [],
assets: spawnAssets ? [spawnAssets] : [],
};
return spawnChain && spawnAssets
? { chains: [spawnChain], assets: [spawnAssets] }
: null;
} catch (error) {
console.error(error);
return undefined;
return null;
}
},
staleTime: Infinity,
Expand Down
11 changes: 4 additions & 7 deletions examples/spawn/pages/faucet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@ export default function Faucet() {
const { selectedChain } = useChainStore();
const { address, chain } = useChain(selectedChain);
const { toast } = useToast();
const { data: spawnChains } = useSpawnChains();
const { data: spawnData } = useSpawnChains();

const checkIsChainSupported = () => {
const isSpawnRunning =
spawnChains?.chains?.length && spawnChains?.assets?.length;

if (!isSpawnRunning) {
if (!spawnData) {
toast({
type: 'error',
title: 'Spawn is not running',
Expand All @@ -30,8 +27,8 @@ export default function Faucet() {
return false;
}

const isSpawnChain = spawnChains?.chains?.some(
(c) => c.chain_id === chain.chain_id,
const isSpawnChain = spawnData?.chains?.some(
(c) => c.chain_id === chain.chain_id
);

if (!isSpawnChain) {
Expand Down

0 comments on commit 2c00c33

Please sign in to comment.