From abec87276a25618c3be4d06b9cbb369154d99b29 Mon Sep 17 00:00:00 2001 From: Worrex <68468180+worrex@users.noreply.github.com> Date: Wed, 18 Oct 2023 15:19:38 +0200 Subject: [PATCH] fix(manage-liquidity-url-poolId): debug statement (#404) * fix(manage-liquidity-url-poolId): debug statement * fix(manage-liquidity-url-poolId): introduced state var * fix(manage-liquidity-url-poolId): handling Wallet.tsx undefined chainInfos --- .../Pages/Trade/Liquidity/ManageLiquidity.tsx | 20 ++++++-- components/Wallet/Wallet.tsx | 47 ++++++++++--------- 2 files changed, 42 insertions(+), 25 deletions(-) diff --git a/components/Pages/Trade/Liquidity/ManageLiquidity.tsx b/components/Pages/Trade/Liquidity/ManageLiquidity.tsx index 3272e45d..6ebd1b29 100644 --- a/components/Pages/Trade/Liquidity/ManageLiquidity.tsx +++ b/components/Pages/Trade/Liquidity/ManageLiquidity.tsx @@ -40,6 +40,7 @@ import { tokenItemState } from 'state/tokenItemState' import { TxStep } from 'types/common' const ManageLiquidity = ({ poolIdFromUrl }) => { + const [poolIdState, setPoolIdState] = useState(null) const [isMobile] = useMediaQuery('(max-width: 640px)') const router = useRouter() const chains: Array = useChainInfos() @@ -99,14 +100,25 @@ const ManageLiquidity = ({ poolIdFromUrl }) => { const chainIdParam = router.query.chainId as string const chainName = useMemo(() => window.location.pathname.split('/')[1].split('/')[0], [window.location.pathname]) + // @ts-ignore + if (window.debugLogsEnabled) { + console.log( + 'ManageLiquidity; Chain Name: ', chainName, ' ', poolIdFromUrl, + ) + } + + useEffect(() => { + setPoolIdState(poolIdFromUrl || poolIdState) + }, [poolIdFromUrl]) + useEffect(() => { if (chainName) { - if (poolIdFromUrl) { + if (poolIdState) { const pools = poolData?.pools - if (pools && !pools.find((pool: any) => pool.pool_id === poolIdFromUrl)) { + if (pools && !pools.find((pool: any) => pool.pool_id === poolIdState)) { router.push(`/${chainName.toLowerCase()}/pools`) } else { - router.push(`/${chainName.toLowerCase()}/pools/manage_liquidity?poolId=${poolIdFromUrl}`) + router.push(`/${chainName.toLowerCase()}/pools/manage_liquidity?poolId=${poolIdState}`) } } else { if (poolData?.pools) { @@ -116,7 +128,7 @@ const ManageLiquidity = ({ poolIdFromUrl }) => { } } // eslint-disable-next-line react-hooks/exhaustive-deps - }, [chainId, poolIdFromUrl, poolData, chainName]) + }, [chainId, poolIdState, poolData, chainName]) useEffect(() => { if (poolIdFromUrl) { diff --git a/components/Wallet/Wallet.tsx b/components/Wallet/Wallet.tsx index 499b03f0..fdbf91d1 100644 --- a/components/Wallet/Wallet.tsx +++ b/components/Wallet/Wallet.tsx @@ -36,6 +36,14 @@ const Wallet = () => { const [walletChains, setWalletChains] = useState([]) const chainName = useMemo(() => window.location.pathname.split('/')[1].split('/')[0], [window.location.pathname]) const [currentConnectedChainIds, setCurrentConnectedChainIds] = useState([]) + const allChains = useChains(walletChains) + const router = useRouter() + + const [chainInfo] = useChainInfo(currentChainState.chainId) + const { isWalletConnected, disconnect, openView } = allChains[WALLET_CHAIN_NAMES_BY_CHAIN_ID[ACTIVE_NETWORKS[currentChainState.network][chainName]]] || {} + const queryClient = useQueryClient() + + const [chainIdParam, setChainIdParam] = useState(null) useEffect(() => { if (chainName && currentChainState.network === NetworkType.mainnet) { @@ -48,7 +56,7 @@ const Wallet = () => { }) } - const walletType = window.localStorage.getItem(COSMOS_KIT_WALLET_KEY); + const walletType = window.localStorage.getItem(COSMOS_KIT_WALLET_KEY) if (walletType === WalletType.leapExtension || walletType === WalletType.leapMobile) { // Window.leap.defaultOptions @@ -67,33 +75,37 @@ const Wallet = () => { setWalletChains(snapChainIds); setCurrentConnectedChainIds(snapChainIds); } else if (walletType === WalletType.terraExtension || walletType === WalletType.keplrExtension) { - const walletWindowConnection = walletType === WalletType.terraExtension ? (window.station?.keplr) : (window?.keplr); + const walletWindowConnection = walletType === WalletType.terraExtension ? (window.station?.keplr) : (window?.keplr) const getAddedStationChainsIds = async () => { - const chainInfos = await walletWindowConnection.getChainInfosWithoutEndpoints(); - return chainInfos.map((chain: { chainId: string }) => chain.chainId); + const chainInfos = await walletWindowConnection?.getChainInfosWithoutEndpoints() + if (!chainInfos) { + await disconnect() + return null + } + return chainInfos.map((chain: { chainId: string }) => chain.chainId) }; const filterChains = async () => { - const addedChains = await getAddedStationChainsIds(); - const filteredChains = chains.filter((chain) => addedChains.includes(chain.chainId) && WALLET_CHAIN_NAMES_BY_CHAIN_ID[chain.chainId]); - const chainNames = filteredChains.map((chain) => WALLET_CHAIN_NAMES_BY_CHAIN_ID[chain.chainId]); - return [chainNames, filteredChains.map((chain) => chain.chainId)]; + const addedChains = await getAddedStationChainsIds() + const filteredChains = chains.filter((chain) => addedChains.includes(chain.chainId) && WALLET_CHAIN_NAMES_BY_CHAIN_ID[chain.chainId]) + const chainNames = filteredChains.map((chain) => WALLET_CHAIN_NAMES_BY_CHAIN_ID[chain.chainId]) + return [chainNames, filteredChains.map((chain) => chain.chainId)] }; filterChains().then(async ([chainNames, ids]) => { if (chainNames.includes('injective')) { try { - await walletWindowConnection.getKey(ChainId.injective); + await walletWindowConnection.getKey(ChainId.injective) } catch { console.error('Injective not activated'); - const injIndex = chainNames.indexOf('injective'); + const injIndex = chainNames.indexOf('injective') if (injIndex !== -1) { - chainNames.splice(injIndex, 1); - ids.splice(injIndex, 1); + chainNames.splice(injIndex, 1) + ids.splice(injIndex, 1) } } } - setWalletChains(chainNames); - setCurrentConnectedChainIds(ids); + setWalletChains(chainNames) + setCurrentConnectedChainIds(ids) }); } else if (walletChains.length === 0) { setCurrentConnectedChainIds(Object.values(ACTIVE_NETWORKS[currentChainState.network])) @@ -102,14 +114,7 @@ const Wallet = () => { setCurrentConnectedChainIds(Object.values(ACTIVE_NETWORKS[currentChainState.network])) } }, [chains, currentChainState.network, chainName, window.localStorage.getItem(COSMOS_KIT_WALLET_KEY)]) - const allChains = useChains(walletChains) - const router = useRouter() - - const [chainInfo] = useChainInfo(currentChainState.chainId) - const { isWalletConnected, disconnect, openView } = allChains[WALLET_CHAIN_NAMES_BY_CHAIN_ID[ACTIVE_NETWORKS[currentChainState.network][chainName]]] || {} - const queryClient = useQueryClient() - const [chainIdParam, setChainIdParam] = useState(null) const resetWallet = () => { setCurrentConnectedChainIds([]) queryClient.clear()