From 2e20d73c69b2477ed169824324ece7f35e3db0f9 Mon Sep 17 00:00:00 2001 From: Aidan Starke <52016903+aidan-starke@users.noreply.github.com> Date: Wed, 27 Apr 2022 14:00:00 +1200 Subject: [PATCH] prevent window exit if txStatus is pending (#173) * prevent window exit if txStatus is pending * extract useEffect to hook * use `useBeforeUnload` * use `TxStatus` type --- components/BridgeProgress.tsx | 3 +++ components/PoolProgress.tsx | 3 +++ components/SwapProgress.tsx | 3 +++ hooks/index.ts | 1 + hooks/useBeforeUnload.ts | 15 +++++++++++++++ hooks/useTxStatus.ts | 8 +------- providers/PoolProvider.tsx | 2 +- types/index.d.ts | 14 +++++++------- 8 files changed, 34 insertions(+), 15 deletions(-) create mode 100644 hooks/useBeforeUnload.ts diff --git a/components/BridgeProgress.tsx b/components/BridgeProgress.tsx index 381c85bd..a9d26976 100644 --- a/components/BridgeProgress.tsx +++ b/components/BridgeProgress.tsx @@ -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 {} @@ -20,6 +21,8 @@ const BridgeProgress: VFC = ( const dismissible = txStatus?.status === "Success" || txStatus?.status === "Failure"; + useBeforeUnload(txStatus); + return ( = ( const dismissible = txStatus?.status === "Success" || txStatus?.status === "Failure"; + useBeforeUnload(txStatus); + return ( = ( const dismissible = txStatus?.status === "Success" || txStatus?.status === "Failure"; + useBeforeUnload(txStatus); + return ( { + const beforeUnload = (event: BeforeUnloadEvent) => { + event.preventDefault(); + if (txStatus?.status === "Pending") return (event.returnValue = ""); + }; + + window.addEventListener("beforeunload", beforeUnload); + + return () => window.removeEventListener("beforeunload", beforeUnload); + }, [txStatus]); +} diff --git a/hooks/useTxStatus.ts b/hooks/useTxStatus.ts index b7cf6f33..4d564e54 100644 --- a/hooks/useTxStatus.ts +++ b/hooks/useTxStatus.ts @@ -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; diff --git a/providers/PoolProvider.tsx b/providers/PoolProvider.tsx index 22c72281..78da934f 100644 --- a/providers/PoolProvider.tsx +++ b/providers/PoolProvider.tsx @@ -1,4 +1,4 @@ -import { CENNZAsset, PoolAction, TxStatus } from "@/types"; +import { CENNZAsset, PoolAction } from "@/types"; import { createContext, Dispatch, diff --git a/types/index.d.ts b/types/index.d.ts index 276ef483..2ed6b0d1 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -6,7 +6,6 @@ import { HTMLAttributes, HTMLFormElement, InputHTMLAttributes, - ReactElement, } from "react"; export type BridgeChain = "Ethereum" | "CENNZnet"; @@ -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" @@ -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; +}