Skip to content

Commit

Permalink
prevent window exit if txStatus is pending (#173)
Browse files Browse the repository at this point in the history
* prevent window exit if txStatus is pending

* extract useEffect to hook

* use `useBeforeUnload`

* use `TxStatus` type
  • Loading branch information
aidan-starke authored Apr 27, 2022
1 parent 850cad7 commit 2e20d73
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 15 deletions.
3 changes: 3 additions & 0 deletions components/BridgeProgress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import StandardButton from "@/components/shared/StandardButton";
import ProgressOverlay from "@/components/shared/ProgressOverlay";
import Link from "@/components/Link";
import { Balance, selectMap } from "@/utils";
import { useBeforeUnload } from "@/hooks";

interface BridgeProgressProps {}

Expand All @@ -20,6 +21,8 @@ const BridgeProgress: VFC<IntrinsicElements["div"] & BridgeProgressProps> = (
const dismissible =
txStatus?.status === "Success" || txStatus?.status === "Failure";

useBeforeUnload(txStatus);

return (
<ProgressOverlay
onRequestClose={setTxIdle}
Expand Down
3 changes: 3 additions & 0 deletions components/PoolProgress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import StandardButton from "@/components/shared/StandardButton";
import Link from "@/components/Link";
import ProgressOverlay from "@/components/shared/ProgressOverlay";
import { Balance, selectMap } from "@/utils";
import { useBeforeUnload } from "@/hooks";

interface PoolProgressProps {}

Expand All @@ -20,6 +21,8 @@ const PoolProgress: VFC<IntrinsicElements["div"] & PoolProgressProps> = (
const dismissible =
txStatus?.status === "Success" || txStatus?.status === "Failure";

useBeforeUnload(txStatus);

return (
<ProgressOverlay
onRequestClose={setTxIdle}
Expand Down
3 changes: 3 additions & 0 deletions components/SwapProgress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import StandardButton from "@/components/shared/StandardButton";
import { Balance, selectMap } from "@/utils";
import Link from "@/components/Link";
import ProgressOverlay from "@/components/shared/ProgressOverlay";
import { useBeforeUnload } from "@/hooks";

interface SwapProgressProps {}

Expand All @@ -20,6 +21,8 @@ const SwapProgress: VFC<IntrinsicElements["div"] & SwapProgressProps> = (
const dismissible =
txStatus?.status === "Success" || txStatus?.status === "Failure";

useBeforeUnload(txStatus);

return (
<ProgressOverlay
onRequestClose={setTxIdle}
Expand Down
1 change: 1 addition & 0 deletions hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export { default as useTxStatus } from "@/hooks/useTxStatus";
export { default as useDepositRequest } from "@/hooks/useDepositRequest";
export { default as useWithdrawRequest } from "@/hooks/useWithdrawRequest";
export { default as useHistoricalWithdrawRequest } from "@/hooks/useHistoricalWithdrawRequest";
export { default as useBeforeUnload } from "@/hooks/useBeforeUnload";

export type { TokenInputHook } from "@/hooks/useTokenInput";
export type { PoolExchangeInfoHook } from "@/hooks/usePoolExchangeInfo";
Expand Down
15 changes: 15 additions & 0 deletions hooks/useBeforeUnload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useEffect } from "react";
import { TxStatus } from "@/types";

export default function useBeforeUnload(txStatus: TxStatus) {
useEffect(() => {
const beforeUnload = (event: BeforeUnloadEvent) => {
event.preventDefault();
if (txStatus?.status === "Pending") return (event.returnValue = "");
};

window.addEventListener("beforeunload", beforeUnload);

return () => window.removeEventListener("beforeunload", beforeUnload);
}, [txStatus]);
}
8 changes: 1 addition & 7 deletions hooks/useTxStatus.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { useCallback, useState } from "react";

type TxType = "Idle" | "Pending" | "Success" | "Failure";

interface TxStatus {
status: TxType;
props?: any;
}
import { TxStatus, TxType } from "@/types";

export interface TxStatusHook {
txStatus: TxStatus;
Expand Down
2 changes: 1 addition & 1 deletion providers/PoolProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CENNZAsset, PoolAction, TxStatus } from "@/types";
import { CENNZAsset, PoolAction } from "@/types";
import {
createContext,
Dispatch,
Expand Down
14 changes: 7 additions & 7 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
HTMLAttributes,
HTMLFormElement,
InputHTMLAttributes,
ReactElement,
} from "react";

export type BridgeChain = "Ethereum" | "CENNZnet";
Expand Down Expand Up @@ -65,12 +64,6 @@ export interface MetaMaskAccount {
address: string;
}

export interface TxStatus {
status: "in-progress" | "success" | "fail";
title: string | ReactElement;
message: string | ReactElement;
}

export type RelayerStatus =
| "Successful"
| "Failed"
Expand Down Expand Up @@ -108,3 +101,10 @@ export interface HistoricalEventProof {
s: string[];
v: number[];
}

export type TxType = "Idle" | "Pending" | "Success" | "Failure";

export interface TxStatus {
status: TxType;
props?: any;
}

0 comments on commit 2e20d73

Please sign in to comment.