Hey is a decentralized and permissionless social media app built with Lens Protocol 馃尶
at main 36 lines 956 B view raw
1import { CHAIN } from "@hey/data/constants"; 2import logger from "@hey/helpers/logger"; 3import { useConnections, useSwitchChain } from "wagmi"; 4 5interface HandleWrongNetworkParams { 6 chainId?: number; 7} 8 9const useHandleWrongNetwork = () => { 10 const activeConnection = useConnections(); 11 const { switchChainAsync } = useSwitchChain(); 12 const isConnected = () => activeConnection[0] !== undefined; 13 14 const handleWrongNetwork = async (params?: HandleWrongNetworkParams) => { 15 const chainId = params?.chainId ?? CHAIN.id; 16 17 const isWrongNetwork = () => activeConnection[0]?.chainId !== chainId; 18 19 if (!isConnected()) { 20 logger.warn("No active connection found."); 21 return; 22 } 23 24 if (isWrongNetwork()) { 25 try { 26 await switchChainAsync({ chainId }); 27 } catch (error) { 28 logger.error("Failed to switch chains:", error); 29 } 30 } 31 }; 32 33 return handleWrongNetwork; 34}; 35 36export default useHandleWrongNetwork;