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

feat: show alert before closing transaction flow #2187

Merged
merged 7 commits into from
Jun 28, 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
1 change: 0 additions & 1 deletion src/components/safe-apps/AppFrame/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ const AppFrame = ({ appUrl, allowedFeaturesList }: AppFrameProps): ReactElement
if (signOffChain) {
setTxFlow(
<SignMessageFlow
onClose={() => setTxFlow(undefined)}
logoUri={safeAppFromManifest?.iconUrl || ''}
name={safeAppFromManifest?.name || ''}
message={message}
Expand Down
2 changes: 1 addition & 1 deletion src/components/safe-messages/SignMsgButton/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const SignMsgButton = ({ msg, compact = false }: { msg: SafeMessage; compact?: b

const onClick = (e: SyntheticEvent) => {
e.stopPropagation()
setTxFlow(<SignMessageFlow onClose={() => setTxFlow(undefined)} {...msg} />)
setTxFlow(<SignMessageFlow {...msg} />)
}

const isDisabled = !isSignable || isPending
Expand Down
48 changes: 48 additions & 0 deletions src/components/tx-flow/common/TxFlowExitWarning/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { DialogContent, DialogContentText, DialogActions, Button, SvgIcon } from '@mui/material'
import type { ReactElement } from 'react'

import ModalDialog from '@/components/common/ModalDialog'
import AlertIcon from '@/public/images/notifications/alert.svg'
import { useDarkMode } from '@/hooks/useDarkMode'

export const TxFlowExitWarning = ({
open,
onCancel,
onClose,
}: {
open: boolean
onCancel: () => void
onClose: () => void
}): ReactElement => {
const isDarkMode = useDarkMode()
return (
<ModalDialog
open={open}
onClose={onCancel}
dialogTitle={
<>
<SvgIcon
component={AlertIcon}
inheritViewBox
fontSize="inherit"
color="error"
sx={{ verticalAlign: 'middle', mr: '12px' }}
/>{' '}
Are you sure?
</>
}
>
<DialogContent sx={{ p: 'var(--space-3) !important' }}>
<DialogContentText color={isDarkMode ? undefined : 'primary'}>
Closing this window will discard your current progress.
</DialogContentText>
</DialogContent>
<DialogActions sx={{ padding: 'var(--space-2) var(--space-3) !important' }}>
<Button onClick={onCancel}>Back to editing</Button>
<Button variant="contained" onClick={onClose}>
Leave
</Button>
</DialogActions>
</ModalDialog>
)
}
63 changes: 7 additions & 56 deletions src/components/tx-flow/flows/SignMessage/SignMessage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ describe('SignMessage', () => {
logoUri="www.fake.com/test.png"
name="Test App"
message={hexlify(toUtf8Bytes(EXAMPLE_MESSAGE))}
onClose={jest.fn}
/>,
)

Expand All @@ -108,27 +107,15 @@ describe('SignMessage', () => {

it('displays the SafeMessage message', () => {
const { getByText } = render(
<SignMessage
logoUri="www.fake.com/test.png"
name="Test App"
message={EXAMPLE_MESSAGE}
requestId="123"
onClose={jest.fn}
/>,
<SignMessage logoUri="www.fake.com/test.png" name="Test App" message={EXAMPLE_MESSAGE} requestId="123" />,
)

expect(getByText('0xaa05af77f274774b8bdc7b61d98bc40da523dc2821fdea555f4d6aa413199bcc')).toBeInTheDocument()
})

it('generates the SafeMessage hash if not provided', () => {
const { getByText } = render(
<SignMessage
logoUri="www.fake.com/test.png"
name="Test App"
message={EXAMPLE_MESSAGE}
requestId="123"
onClose={jest.fn}
/>,
<SignMessage logoUri="www.fake.com/test.png" name="Test App" message={EXAMPLE_MESSAGE} requestId="123" />,
)

expect(getByText('0x73d0948ac608c5d00a6dd26dd396cce79b459307ea365f5a5bd5d3119c2d9708')).toBeInTheDocument()
Expand Down Expand Up @@ -176,13 +163,7 @@ describe('SignMessage', () => {

it('renders the message', () => {
const { getByText } = render(
<SignMessage
requestId="123"
logoUri="www.fake.com/test.png"
name="Test App"
message={EXAMPLE_MESSAGE}
onClose={jest.fn}
/>,
<SignMessage requestId="123" logoUri="www.fake.com/test.png" name="Test App" message={EXAMPLE_MESSAGE} />,
)

Object.keys(EXAMPLE_MESSAGE.message).forEach((key) => {
Expand All @@ -194,27 +175,15 @@ describe('SignMessage', () => {

it('displays the SafeMessage message', () => {
const { getByText } = render(
<SignMessage
logoUri="www.fake.com/test.png"
name="Test App"
message={EXAMPLE_MESSAGE}
requestId="123"
onClose={jest.fn}
/>,
<SignMessage logoUri="www.fake.com/test.png" name="Test App" message={EXAMPLE_MESSAGE} requestId="123" />,
)

expect(getByText('0xd5ffe9f6faa9cc9294673fb161b1c7b3e0c98241e90a38fc6c451941f577fb19')).toBeInTheDocument()
})

it('generates the SafeMessage hash if not provided', () => {
const { getByText } = render(
<SignMessage
logoUri="www.fake.com/test.png"
name="Test App"
message={EXAMPLE_MESSAGE}
requestId="123"
onClose={jest.fn}
/>,
<SignMessage logoUri="www.fake.com/test.png" name="Test App" message={EXAMPLE_MESSAGE} requestId="123" />,
)

expect(getByText('0x10c926c4f417e445de3fddc7ad8c864f81b9c81881b88eba646015de10d21613')).toBeInTheDocument()
Expand All @@ -233,7 +202,6 @@ describe('SignMessage', () => {
name="Test App"
message="Hello world!"
requestId="123"
onClose={jest.fn}
safeAppId={25}
/>,
)
Expand Down Expand Up @@ -300,13 +268,7 @@ describe('SignMessage', () => {
jest.spyOn(useSafeMessages, 'useSafeMessage').mockReturnValue(msg)

const { getByText } = render(
<SignMessage
logoUri="www.fake.com/test.png"
name="Test App"
message={messageText}
requestId="123"
onClose={jest.fn}
/>,
<SignMessage logoUri="www.fake.com/test.png" name="Test App" message={messageText} requestId="123" />,
)

await act(async () => {
Expand Down Expand Up @@ -348,7 +310,6 @@ describe('SignMessage', () => {
name="Test App"
message="Hello world!"
requestId="123"
onClose={jest.fn}
safeAppId={25}
/>,
)
Expand All @@ -370,7 +331,6 @@ describe('SignMessage', () => {
name="Test App"
message="Hello world!"
requestId="123"
onClose={jest.fn}
safeAppId={25}
/>,
)
Expand All @@ -396,7 +356,6 @@ describe('SignMessage', () => {
name="Test App"
message="Hello world!"
requestId="123"
onClose={jest.fn}
safeAppId={25}
/>,
)
Expand Down Expand Up @@ -445,13 +404,7 @@ describe('SignMessage', () => {
jest.spyOn(useSafeMessages, 'useSafeMessage').mockReturnValue(msg)

const { getByText } = render(
<SignMessage
logoUri="www.fake.com/test.png"
name="Test App"
message={messageText}
requestId="123"
onClose={jest.fn}
/>,
<SignMessage logoUri="www.fake.com/test.png" name="Test App" message={messageText} requestId="123" />,
)

await waitFor(() => {
Expand Down Expand Up @@ -484,7 +437,6 @@ describe('SignMessage', () => {
name="Test App"
message="Hello world!"
requestId="123"
onClose={jest.fn}
safeAppId={25}
/>,
)
Expand Down Expand Up @@ -526,7 +478,6 @@ describe('SignMessage', () => {
name="Test App"
message="Hello world!"
requestId="123"
onClose={jest.fn}
safeAppId={25}
/>,
)
Expand Down
72 changes: 7 additions & 65 deletions src/components/tx-flow/flows/SignMessage/SignMessage.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
import {
Grid,
DialogActions,
Button,
Box,
Typography,
DialogContent,
SvgIcon,
Dialog,
DialogTitle,
DialogContentText,
CardContent,
CardActions,
} from '@mui/material'
import { Grid, Button, Box, Typography, SvgIcon, CardContent, CardActions } from '@mui/material'
import { useTheme } from '@mui/material/styles'
import { useCallback, useState } from 'react'
import { useContext } from 'react'
import { SafeMessageListItemType, SafeMessageStatus } from '@safe-global/safe-gateway-typescript-sdk'
import type { ReactElement } from 'react'
import type { SafeMessage } from '@safe-global/safe-gateway-typescript-sdk'
Expand All @@ -27,7 +14,7 @@ import ErrorMessage from '@/components/tx/ErrorMessage'
import useWallet from '@/hooks/wallets/useWallet'
import { useSafeMessage } from '@/hooks/messages/useSafeMessages'
import useOnboard, { switchWallet } from '@/hooks/wallets/useOnboard'

import { TxModalContext } from '@/components/tx-flow'
import CopyButton from '@/components/common/CopyButton'
import { WrongChainWarning } from '@/components/tx/WrongChainWarning'
import MsgSigners from '@/components/safe-messages/MsgSigners'
Expand Down Expand Up @@ -129,39 +116,7 @@ const AlreadySignedByOwnerMessage = ({ hasSigned }: { hasSigned: boolean }) => {
)
}

const ConfirmationDialog = ({
open,
onCancel,
onClose,
}: {
open: boolean
onCancel: () => void
onClose: () => void
}) => (
<Dialog
open={open}
onClose={onClose}
aria-labelledby="alert-dialog-title"
aria-describedby="alert-dialog-description"
>
<DialogTitle id="alert-dialog-title">Cancel message signing request</DialogTitle>
<DialogContent>
<DialogContentText id="alert-dialog-description">
<Typography variant="body2">If you close this modal, the signing request will be aborted.</Typography>
</DialogContentText>
</DialogContent>
<DialogActions>
<Button onClick={onCancel}>Cancel</Button>
<Button variant="contained" onClick={onClose} autoFocus>
Abort signing
</Button>
</DialogActions>
</Dialog>
)

type BaseProps = {
onClose: () => void
} & Pick<SafeMessage, 'logoUri' | 'name' | 'message'>
type BaseProps = Pick<SafeMessage, 'logoUri' | 'name' | 'message'>

// Custom Safe Apps do not have a `safeAppId`
export type ProposeProps = BaseProps & {
Expand All @@ -175,9 +130,9 @@ export type ConfirmProps = BaseProps & {
requestId?: RequestId
}

const SignMessage = ({ onClose, message, safeAppId, requestId }: ProposeProps | ConfirmProps): ReactElement => {
const SignMessage = ({ message, safeAppId, requestId }: ProposeProps | ConfirmProps): ReactElement => {
// Hooks & variables
const [showCloseTooltip, setShowCloseTooltip] = useState<boolean>(false)
const { setTxFlow } = useContext(TxModalContext)
const { palette } = useTheme()
const { safe } = useSafeInfo()
const isOwner = useIsSafeOwner()
Expand All @@ -200,18 +155,9 @@ const SignMessage = ({ onClose, message, safeAppId, requestId }: ProposeProps |
safeMessageHash,
requestId,
safeAppId,
onClose,
() => setTxFlow(undefined),
)

const handleClose = useCallback(() => {
if (requestId && (!ongoingMessage || ongoingMessage.status === SafeMessageStatus.NEEDS_CONFIRMATION)) {
// If we are in a Safe app modal we want to keep the modal open
setShowCloseTooltip(true)
} else {
onClose()
}
}, [onClose, ongoingMessage, requestId])

return (
<>
<TxCard>
Expand Down Expand Up @@ -255,15 +201,11 @@ const SignMessage = ({ onClose, message, safeAppId, requestId }: ProposeProps |
<TxCard>
<CardActions>
<Box display="flex" justifyContent="space-between" width="100%">
{/* TODO: Remove this Cancel button once we can figure out how to move the logic outside */}
<Button onClick={handleClose}>Cancel</Button>

<Button variant="contained" color="primary" onClick={onSign} disabled={isDisabled}>
Sign
</Button>
</Box>
</CardActions>
<ConfirmationDialog open={showCloseTooltip} onCancel={() => setShowCloseTooltip(false)} onClose={onClose} />
</TxCard>
</>
)
Expand Down
Loading