Skip to content

Commit

Permalink
refactor(add): add comments, use isPending from the useSwitchChain, r…
Browse files Browse the repository at this point in the history
…emove hardcode
  • Loading branch information
solidovic committed Jan 21, 2025
1 parent 158e087 commit a85b15c
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions modules/web3/web3-provider/use-app-chain-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,12 @@ import { wagmiChainMap } from './web3-provider';

export const useAppChainManager = (supportedL2: boolean) => {
const [dappChainId, setDappChainId] = useState<number>(config.defaultChain);
const [isSwitchChainWait, setIsSwitchChainWait] = useState<boolean>(true);

// Hack for launching widget (see: "Sync the 'app chain id' with the 'wallet chain id' or use default")
const [isMounted, setIsMounted] = useState<boolean>(false);

const { chainId: walletChainId, isConnected } = useAccount();
const { switchChain } = useSwitchChain({
mutation: {
onMutate: () => setIsSwitchChainWait(true),
onSettled: () => setIsSwitchChainWait(false),
},
});
const { switchChain, isPending: isSwitchChainPending } = useSwitchChain();

const supportedChainIds = useMemo(
() =>
Expand All @@ -48,7 +45,8 @@ export const useAppChainManager = (supportedL2: boolean) => {
setDappChainId(chainId);
}

setIsSwitchChainWait(false);
// Hack for launching widget (at the moment of launching, the wallet network and widget network may not match)
setIsMounted(true);
}, [walletChainId, isConnected, supportedChainIds]);

const switchAppChainId = useCallback<Dispatch<number>>(
Expand All @@ -62,8 +60,8 @@ export const useAppChainManager = (supportedL2: boolean) => {

const [isChainIdOnL2, supportedChainLabels] = useMemo(() => {
const isChainIdOnL2 =
supportedL2 &&
getChainTypeByChainId(dappChainId) === DAPP_CHAIN_TYPE.Optimism;
// all L2 networks supported by the widget will be supported in SDK
supportedL2 && isSDKSupportedL2Chain(dappChainId);

const supportedChainTypes = supportedChainIds
.map(getChainTypeByChainId)
Expand Down Expand Up @@ -106,8 +104,9 @@ export const useAppChainManager = (supportedL2: boolean) => {
return {
chainId: dappChainId,
setChainId: switchAppChainId,
isSwitchChainWait,
isSwitchChainWait: !isMounted || isSwitchChainPending,

isTestnet: wagmiChainMap[dappChainId]?.isTestnet || false,
isChainIdOnL2,
supportedL2,

Expand Down

0 comments on commit a85b15c

Please sign in to comment.