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

Onclose callback #2094

Merged
merged 1 commit into from
Jun 14, 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
7 changes: 3 additions & 4 deletions src/components/common/NewModalDialog/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { type ReactElement, type ReactNode, useContext } from 'react'
import { type ReactElement, type ReactNode } from 'react'
import { IconButton, type ModalProps } from '@mui/material'
import { Dialog, DialogTitle, type DialogProps, useMediaQuery } from '@mui/material'
import { useTheme } from '@mui/material/styles'
import ChainIndicator from '@/components/common/ChainIndicator'
import CloseIcon from '@mui/icons-material/Close'
import css from './styles.module.css'
import { TxModalContext } from '@/components/tx-flow'

interface ModalDialogProps extends DialogProps {
dialogTitle?: React.ReactNode
Expand Down Expand Up @@ -47,12 +46,12 @@ const NewModalDialog = ({
dialogTitle,
hideChainIndicator,
children,
onClose,
fullScreen = false,
...restProps
}: ModalDialogProps): ReactElement => {
const theme = useTheme()
const isSmallScreen = useMediaQuery(theme.breakpoints.down('md'))
const { setTxFlow } = useContext(TxModalContext)

return (
<Dialog
Expand Down Expand Up @@ -85,7 +84,7 @@ const NewModalDialog = ({
<span style={{ flex: 1 }} />
<IconButton
aria-label="close"
onClick={() => setTxFlow(undefined)}
onClick={(e) => onClose?.(e, 'backdropClick')}
Copy link
Member

Choose a reason for hiding this comment

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

Do we have to call onClose with e and backdropClick here even if its not being used?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, that's the signature of that prop inherited from the MUI Dialog.

size="small"
sx={{
ml: 2,
Expand Down
23 changes: 12 additions & 11 deletions src/components/safe-apps/AppFrame/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,17 @@ const AppFrame = ({ appUrl, allowedFeaturesList }: AppFrameProps): ReactElement
const { getPermissions, hasPermission, permissionsRequest, setPermissionsRequest, confirmPermissionRequest } =
useSafePermissions()
const appName = useMemo(() => (remoteApp ? remoteApp.name : appUrl), [appUrl, remoteApp])
const { txFlow, setTxFlow } = useContext(TxModalContext)
const { setTxFlow } = useContext(TxModalContext)

const onTxFlowClose = () => {
setCurrentRequestId((prevId) => {
if (prevId) {
communicator?.send(CommunicatorMessages.REJECT_TRANSACTION_MESSAGE, prevId, true)
trackSafeAppEvent(SAFE_APPS_EVENTS.PROPOSE_TRANSACTION_REJECTED, appName)
}
return undefined
})
}

const communicator = useAppCommunicator(iframeRef, remoteApp || safeAppFromManifest, chain, {
onConfirmTransactions: (txs: BaseTransaction[], requestId: RequestId, params?: SendTransactionRequestParams) => {
Expand All @@ -102,8 +112,7 @@ const AppFrame = ({ appUrl, allowedFeaturesList }: AppFrameProps): ReactElement
}

setCurrentRequestId(requestId)

setTxFlow(<SafeAppsTxFlow data={data} />)
setTxFlow(<SafeAppsTxFlow data={data} />, onTxFlowClose)
},
onSignMessage: (
message: string | EIP712TypedData,
Expand Down Expand Up @@ -177,14 +186,6 @@ const AppFrame = ({ appUrl, allowedFeaturesList }: AppFrameProps): ReactElement
},
})

// @TODO: figure out how to handle this
const onSafeAppsModalClose = () => {
if (currentRequestId) {
communicator?.send(CommunicatorMessages.REJECT_TRANSACTION_MESSAGE, currentRequestId, true)
trackSafeAppEvent(SAFE_APPS_EVENTS.PROPOSE_TRANSACTION_REJECTED, appName)
}
}

const onAcceptPermissionRequest = (_origin: string, requestId: RequestId) => {
const permissions = confirmPermissionRequest(PermissionStatus.GRANTED)
communicator?.send(permissions, requestId as string)
Expand Down
42 changes: 25 additions & 17 deletions src/components/tx-flow/index.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,41 @@
import {
createContext,
type Dispatch,
type ReactElement,
type ReactNode,
type SetStateAction,
useState,
useEffect,
useCallback,
} from 'react'
import { createContext, type ReactElement, type ReactNode, useState, useEffect, useCallback } from 'react'
import NewModalDialog from '@/components/common/NewModalDialog'
import { useRouter } from 'next/router'

const noop = () => {}

export const TxModalContext = createContext<{
type TxModalContextType = {
txFlow: ReactNode | undefined
setTxFlow: Dispatch<SetStateAction<ReactNode | undefined>>
}>({
setTxFlow: (txFlow: TxModalContextType['txFlow'], onClose?: () => void) => void
onClose: () => void
}

export const TxModalContext = createContext<TxModalContextType>({
txFlow: undefined,
setTxFlow: noop,
onClose: noop,
})

export const TxModalProvider = ({ children }: { children: ReactNode }): ReactElement => {
const [txFlow, setTxFlow] = useState<ReactNode | undefined>()
const [txFlow, setFlow] = useState<TxModalContextType['txFlow']>(undefined)
const [onClose, setOnClose] = useState<TxModalContextType['onClose']>(noop)
const router = useRouter()

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

const setTxFlow = useCallback(
(txFlow: TxModalContextType['txFlow'], onClose?: () => void) => {
setFlow(txFlow)
setOnClose(() => onClose ?? noop)
},
[setFlow, setOnClose],
)

// Close the modal if user navigates
useEffect(() => {
Expand All @@ -36,7 +44,7 @@ export const TxModalProvider = ({ children }: { children: ReactNode }): ReactEle
}, [router, handleModalClose])

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

<NewModalDialog open={!!txFlow} onClose={handleModalClose}>
Expand Down