Files
explore/lib/web3/useSwitchOrAddChain.tsx
T
zeekayandHanzo Dev bbcb1596fd explore v1.1.4: native all-chains wallet-connect, login/menu fixes, correct chain manifest
- wallet: brandFamilyChains derives sibling networks (Lux mainnet/testnet/devnet)
  from the multi-tenant chain registry so connect/add presents EVERY registered
  network for the brand, not just the env chain; dedupe by id.
- login/menu: useProvider queryFn returns null (react-query v5 forbids undefined)
  + callers switched from '= {}' destructure default to '?? {}' so a null result
  no longer crashes the wallet hooks (the busted-menu root cause).
- manifest: fix Pars chainId 7070 -> 494949 (regression) and drop the forbidden
  'Subnet' term from sovereign L1 network names (-> '<Brand> Network').
- Validators page + /api/pchain proxy present; rebuild makes the live 5-validator
  view visible on explore.lux.network.

Co-authored-by: Hanzo Dev <dev@hanzo.ai>
2026-07-08 11:44:04 -07:00

42 lines
1.1 KiB
TypeScript

import { get } from 'es-toolkit/compat';
import React from 'react';
import type config from 'configs/app';
import getErrorObj from 'lib/errors/getErrorObj';
import useAddChain from './useAddChain';
import useProvider from './useProvider';
import useSwitchChain from './useSwitchChain';
interface Params {
chainConfig?: typeof config;
}
export default function useSwitchOrAddChain(params?: Params) {
const { data: providerData } = useProvider();
const { wallet, provider } = providerData ?? {};
const addChain = useAddChain(params);
const switchChain = useSwitchChain(params);
return React.useCallback(async() => {
if (!wallet || !provider) {
return;
}
try {
return switchChain();
} catch (error) {
const errorObj = getErrorObj(error);
const code = get(errorObj, 'code');
const originalErrorCode = get(errorObj, 'data.originalError.code');
// This error code indicates that the chain has not been added to Wallet.
if (code === 4902 || originalErrorCode === 4902) {
return addChain();
}
throw error;
}
}, [ addChain, provider, wallet, switchChain ]);
}