Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Hide confirmation dialog if set via setTxFlow #2233

Merged
merged 1 commit into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/sidebar/NewTxButton/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const NewTxButton = (): ReactElement => {
const { setTxFlow } = useContext(TxModalContext)

const onClick = () => {
setTxFlow(<NewTxMenu />)
setTxFlow(<NewTxMenu />, undefined, false)
trackEvent(OVERVIEW_EVENTS.NEW_TRANSACTION)
}

Expand Down
1 change: 0 additions & 1 deletion src/components/tx-flow/common/TxNonce/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ const TxNonceForm = ({ nonce, recommendedNonce }: { nonce: number; recommendedNo
readOnly,
}}
className={css.input}
title={field.value}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This caused MUI errors because Tooltips can't also contain a title.

sx={{
minWidth: `clamp(1ch, ${field.value.length}ch, 200px)`,
}}
Expand Down
8 changes: 2 additions & 6 deletions src/components/tx-flow/flows/NewTx/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ const NewTxMenu = () => {
setTxFlow(<TokenTransferFlow />)
}, [setTxFlow])

const onNavigate = useCallback(() => {
setTxFlow(undefined)
}, [setTxFlow])

const progress = 10

return (
Expand Down Expand Up @@ -53,7 +49,7 @@ const NewTxMenu = () => {

<SendTokensButton onClick={onTokensClick} sx={buttonSx} />

<SendNFTsButton onClick={onNavigate} sx={buttonSx} />
<SendNFTsButton sx={buttonSx} />

{txBuilder?.app && (
<>
Expand All @@ -62,7 +58,7 @@ const NewTxMenu = () => {
interaction
</Typography>

<TxBuilderButton onClick={onNavigate} sx={buttonSx} />
<TxBuilderButton sx={buttonSx} />
</>
)}
</Grid>
Expand Down
65 changes: 30 additions & 35 deletions src/components/tx-flow/index.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { createContext, type ReactElement, type ReactNode, useState, useEffect, useCallback } from 'react'
import TxModalDialog from '@/components/common/TxModalDialog'
import { useRouter } from 'next/router'
import { TxFlowExitWarning } from '@/components/tx-flow/common/TxFlowExitWarning'
import NewTxMenu from './flows/NewTx'

const noop = () => {}

type TxModalContextType = {
txFlow: JSX.Element | undefined
setTxFlow: (txFlow: TxModalContextType['txFlow'], onClose?: () => void) => void
setTxFlow: (txFlow: TxModalContextType['txFlow'], onClose?: () => void, shouldWarn?: boolean) => void
setFullWidth: (fullWidth: boolean) => void
}

Expand All @@ -20,63 +18,60 @@ export const TxModalContext = createContext<TxModalContextType>({

export const TxModalProvider = ({ children }: { children: ReactNode }): ReactElement => {
const [txFlow, setFlow] = useState<TxModalContextType['txFlow']>(undefined)
const [showWarning, setShowWarning] = useState(false)
const [shouldWarn, setShouldWarn] = useState<boolean>(true)
const [, setOnClose] = useState<Parameters<TxModalContextType['setTxFlow']>[1]>(noop)
const [fullWidth, setFullWidth] = useState<boolean>(false)
const router = useRouter()

const handleShowWarning = useCallback(() => {
setShowWarning(true)
}, [setShowWarning])

const handleCloseWarning = useCallback(() => {
setShowWarning(false)
}, [setShowWarning])

const handleModalClose = useCallback(() => {
setOnClose((prevOnClose) => {
prevOnClose?.()
return noop
})
handleCloseWarning()
setFlow(undefined)
}, [handleCloseWarning, setFlow, setOnClose])
}, [setFlow, setOnClose])

const handleShowWarning = useCallback(() => {
if (!shouldWarn) {
handleModalClose()
return
}

const ok = confirm('Closing this window will discard your current progress.')
if (!ok) {
router.events.emit('routeChangeError')
throw 'routeChange aborted. This error can be safely ignored - https://github.com/zeit/next.js/issues/2476.'
}

handleModalClose()
}, [shouldWarn, handleModalClose, router.events])

const setTxFlow = useCallback(
(txFlow: TxModalContextType['txFlow'], onClose?: () => void) => {
if (!txFlow) {
handleCloseWarning()
}
(txFlow: TxModalContextType['txFlow'], onClose?: () => void, shouldWarn?: boolean) => {
setFlow(txFlow)
setOnClose(() => onClose ?? noop)
setShouldWarn(shouldWarn ?? true)
},
[setFlow, setOnClose, handleCloseWarning],
[setFlow, setOnClose],
)

// Show the modal if user navigates
// Show the confirmation dialog if user navigates
useEffect(() => {
const shouldWarn = txFlow && !(txFlow.type instanceof NewTxMenu)
if (!shouldWarn) {
return
}
if (!txFlow) return

router.events.on('routeChangeStart', handleShowWarning)
return () => {
router.events.off('routeChangeStart', handleShowWarning)
}
}, [txFlow, router, handleShowWarning])
}, [txFlow, handleShowWarning, router.events])

return (
<>
<TxModalContext.Provider value={{ txFlow, setTxFlow, setFullWidth }}>
{children}

<TxModalDialog open={!!txFlow} onClose={handleShowWarning} fullWidth={fullWidth}>
{txFlow}
</TxModalDialog>
</TxModalContext.Provider>
<TxModalContext.Provider value={{ txFlow, setTxFlow, setFullWidth }}>
{children}

<TxFlowExitWarning open={showWarning} onCancel={handleCloseWarning} onClose={handleModalClose} />
</>
<TxModalDialog open={!!txFlow} onClose={handleShowWarning} fullWidth={fullWidth}>
{txFlow}
</TxModalDialog>
</TxModalContext.Provider>
)
}
2 changes: 1 addition & 1 deletion src/components/tx/SignOrExecuteForm/ExecuteForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const ExecuteForm = ({

try {
const executedTxId = await executeTx(txOptions, safeTx, txId, origin, willRelay)
setTxFlow(<SuccessScreen txId={executedTxId} />)
setTxFlow(<SuccessScreen txId={executedTxId} />, undefined, false)
} catch (_err) {
const err = asError(_err)
logError(Errors._804, err)
Expand Down
Loading