Skip to content

Commit

Permalink
Fixed useDepositPending
Browse files Browse the repository at this point in the history
  • Loading branch information
makkelie-dev committed Dec 11, 2024
1 parent d6073c9 commit 40ad879
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/components/@widgets/MakeWidget/MakeWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ const MakeWidget: FC = () => {
makerTokenInfo?.address === ADDRESS_ZERO &&
!!nativeCurrencySafeTransactionFee[makerTokenInfo.chainId];
const approvalTransaction = useApprovalPending(makerTokenInfo?.address, true);
const depositTransaction = useDepositPending();
const depositTransaction = useDepositPending(true);
const wrappedNativeToken = useNativeWrappedToken(chainId);
const shouldDepositNativeTokenAmount = useShouldDepositNativeToken(
makerTokenInfo?.address,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const ApprovalSubmittedScreen: FC<ApprovalSubmittedScreenProps> = ({
<OverlayContainer
className={className}
style={{
transform: isAnimatedToCenter ? "translateY(5rem)" : "translateY(0)",
transform: isAnimatedToCenter ? "translateY(2.5rem)" : "translateY(0)",
}}
>
<OverlayLoader isSucceeded={isSucceeded} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const DepositSubmittedScreen: FC<DepositSubmittedScreenProps> = ({
<OverlayContainer
className={className}
style={{
transform: isAnimatedToCenter ? "translateY(5rem)" : "translateY(0)",
transform: isAnimatedToCenter ? "translateY(2.5rem)" : "translateY(0)",
}}
>
<OverlayLoader isSucceeded={isSucceeded} />
Expand Down
5 changes: 5 additions & 0 deletions src/features/transactions/transactionsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ export const selectSuccessfulTransactions = (
(tx) => tx.status === TransactionStatusType.succeeded
) as SubmittedTransaction[];

export const selectAllDeposits = (state: RootState) =>
state.transactions.transactions.filter(
(tx) => tx.type === TransactionTypes.deposit
) as SubmittedDepositTransaction[];

export const selectPendingDeposits = (
state: RootState
): SubmittedDepositTransaction[] =>
Expand Down
47 changes: 44 additions & 3 deletions src/hooks/useDepositPending.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,52 @@
import { useEffect, useMemo, useState } from "react";
import { useDebounce } from "react-use";

import { useAppSelector } from "../app/hooks";
import { SubmittedDepositTransaction } from "../entities/SubmittedTransaction/SubmittedTransaction";
import { selectPendingDeposits } from "../features/transactions/transactionsSlice";
import {
selectAllDeposits,
selectPendingDeposits,
} from "../features/transactions/transactionsSlice";

const useDepositPending = (): SubmittedDepositTransaction | undefined => {
const useDepositPending = (
showResolvedDeposit: boolean = false
): SubmittedDepositTransaction | undefined => {
const pendingDeposits = useAppSelector(selectPendingDeposits);
const allDeposits = useAppSelector(selectAllDeposits);

const [debouncedDeposit, setDebouncedDeposit] = useState<
SubmittedDepositTransaction | undefined
>(undefined);

const pendingDeposit = pendingDeposits.length
? pendingDeposits[0]
: undefined;

const resolvedDeposit = useMemo(() => {
if (debouncedDeposit) {
return allDeposits.find((tx) => tx.hash === debouncedDeposit.hash);
}

return undefined;
}, [debouncedDeposit, allDeposits]);

useEffect(() => {
if (pendingDeposit) {
setDebouncedDeposit(pendingDeposit);
}
}, [pendingDeposit]);

useDebounce(
() => {
if (pendingDeposit === undefined) {
setDebouncedDeposit(undefined);
}
},
3000,
[pendingDeposit]
);

return pendingDeposits.length ? pendingDeposits[0] : undefined;
return pendingDeposit || (showResolvedDeposit ? resolvedDeposit : undefined);
};

export default useDepositPending;

0 comments on commit 40ad879

Please sign in to comment.