From ac18b821b4a8f5c668c83265454ccbc994370692 Mon Sep 17 00:00:00 2001 From: piersss <86911296+piersss@users.noreply.github.com> Date: Wed, 20 Dec 2023 23:08:36 +0100 Subject: [PATCH] 852: Fixed loading transactions from localStorage (#877) --- .../@widgets/SwapWidget/SwapWidget.tsx | 2 +- src/features/metadata/metadataSubscriber.ts | 9 +++++++- .../transactions/useHistoricalTransactions.ts | 22 +++++++++++++++---- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/components/@widgets/SwapWidget/SwapWidget.tsx b/src/components/@widgets/SwapWidget/SwapWidget.tsx index abc849d85..6fd35e46a 100644 --- a/src/components/@widgets/SwapWidget/SwapWidget.tsx +++ b/src/components/@widgets/SwapWidget/SwapWidget.tsx @@ -215,7 +215,7 @@ const SwapWidget: FC = () => { baseTokenInfo, baseAmount ); - const shouldApprove = !hasSufficientAllowance && swapType !== 'wrapOrUnwrap'; + const shouldApprove = !hasSufficientAllowance && swapType !== "wrapOrUnwrap"; const hasApprovalPending = useApprovalPending(baseToken); const maxAmount = useMaxAmount(baseToken); diff --git a/src/features/metadata/metadataSubscriber.ts b/src/features/metadata/metadataSubscriber.ts index 996600aa9..2e1dd0eb1 100644 --- a/src/features/metadata/metadataSubscriber.ts +++ b/src/features/metadata/metadataSubscriber.ts @@ -93,10 +93,17 @@ export const subscribeToSavedTokenChangesForLocalStoragePersisting = () => { ) { transactionCache[wallet.address!][wallet.chainId!] = mostRecentTransactions; + + // Filter out processing transactions to prevent them being stuck perpetually, we should enable this again once we have a better solution + // TODO: https://github.com/airswap/airswap-web/issues/876 + const updatedLocalStorageTransactions = mostRecentTransactions.filter( + (tx) => tx.status !== "processing" + ); + localStorage.setItem( getTransactionsLocalStorageKey(wallet.address!, wallet.chainId!), JSON.stringify({ - all: mostRecentTransactions, + all: updatedLocalStorageTransactions, }) ); } diff --git a/src/features/transactions/useHistoricalTransactions.ts b/src/features/transactions/useHistoricalTransactions.ts index ee55901b3..ad9a4b44f 100644 --- a/src/features/transactions/useHistoricalTransactions.ts +++ b/src/features/transactions/useHistoricalTransactions.ts @@ -1,4 +1,4 @@ -import { useEffect } from "react"; +import { useEffect, useState } from "react"; import { useCustomCompareEffect } from "@react-hookz/web/esm"; import { useWeb3React } from "@web3-react/core"; @@ -10,6 +10,7 @@ import Weth9 from "../../constants/Weth9"; import { checkPendingTransactionState, getSwapArgsFromWrappedSwapForLog, + getTransactionsLocalStorageKey, SwapEventArgs, } from "./transactionUtils"; import { @@ -23,13 +24,14 @@ import useTransactionsFromLocalStorage from "./useTransactionsFromLocalStorage"; const useHistoricalTransactions = () => { const { chainId, library, account } = useWeb3React(); const { result: swapLogs, status: swapLogStatus } = useSwapLogs(); - const transactions = useAppSelector(selectTransactions); const { transactions: localStorageTransactions, setTransactions: setLocalStorageTransactions, } = useTransactionsFromLocalStorage(); const dispatch = useAppDispatch(); + const [activeLocalStorageKey, setActiveLocalStorageKey] = useState(); + useCustomCompareEffect( () => { if (swapLogStatus === "loading" || swapLogStatus === "not-executed") @@ -170,8 +172,20 @@ const useHistoricalTransactions = () => { ); useEffect(() => { - setLocalStorageTransactions({ all: transactions }); - }, [transactions]); + if (!chainId || !account) { + return; + } + + const localStorageKey = getTransactionsLocalStorageKey(account, chainId); + + if (localStorageKey === activeLocalStorageKey) { + return; + } + + setActiveLocalStorageKey(localStorageKey); + + dispatch(setTransactions(localStorageTransactions?.all || [])); + }, [localStorageTransactions]); }; export default useHistoricalTransactions;