Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(suite): #4512 add AOPP autofill notification
- Loading branch information
1 parent
288a23a
commit 92f7737
Showing
8 changed files
with
195 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,7 +57,8 @@ | |
"protocols": { | ||
"name": "Trezor Suite", | ||
"schemes": [ | ||
"bitcoin" | ||
"bitcoin", | ||
"aopp" | ||
] | ||
}, | ||
"publish": { | ||
|
118 changes: 46 additions & 72 deletions
118
packages/suite/src/components/suite/hocNotification/components/withCoinProtocolScheme.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,63 @@ | ||
import React from 'react'; | ||
import { connect } from 'react-redux'; | ||
import styled from 'styled-components'; | ||
import { useRouteMatch } from 'react-router-dom'; | ||
|
||
import * as protocolActions from '@suite-actions/protocolActions'; | ||
|
||
import { useSelector } from '@suite-hooks'; | ||
import { Translation } from '@suite-components'; | ||
import { CoinLogo, variables } from '@trezor/components'; | ||
import { CoinLogo } from '@trezor/components'; | ||
import { capitalizeFirstLetter } from '@suite-utils/string'; | ||
import { PROTOCOL_TO_NETWORK } from '@suite-constants/protocol'; | ||
import withConditionalAction from './withConditionalAction'; | ||
|
||
import type { NotificationEntry } from '@suite-reducers/notificationReducer'; | ||
import type { Dispatch } from '@suite-types'; | ||
import type { ViewProps } from '../definitions'; | ||
|
||
type StrictViewProps = ViewProps & { | ||
notification: Extract<NotificationEntry, { type: 'coin-scheme-protocol' }>; | ||
}; | ||
|
||
const Header = styled.div` | ||
font-weight: ${variables.FONT_WEIGHT.MEDIUM}; | ||
color: ${props => props.theme.TYPE_LIGHT_GREY}; | ||
margin-top: 1px; | ||
`; | ||
|
||
const Body = styled.div` | ||
font-weight: ${variables.FONT_WEIGHT.MEDIUM}; | ||
margin-top: 1px; | ||
`; | ||
import type { Network } from '@wallet-types'; | ||
|
||
const Row = styled.span` | ||
display: flex; | ||
`; | ||
|
||
const withCoinProtocolScheme = (View: React.ComponentType<ViewProps>, props: StrictViewProps) => { | ||
const WrappedView = connect()(({ dispatch }: { dispatch: Dispatch }) => { | ||
const { message, notification } = props; | ||
|
||
const { selectedAccount } = useSelector(state => ({ | ||
selectedAccount: state.wallet.selectedAccount, | ||
})); | ||
|
||
if (typeof message !== 'string') { | ||
message.values = { | ||
header: ( | ||
<Header> | ||
<Translation id="TOAST_COIN_SCHEME_PROTOCOL_HEADER" /> | ||
</Header> | ||
), | ||
body: ( | ||
<Body> | ||
<Row> | ||
{notification.amount && `${notification.amount} `} | ||
{capitalizeFirstLetter(notification.scheme)} | ||
</Row> | ||
<Row>{notification.address}</Row> | ||
</Body> | ||
), | ||
}; | ||
} | ||
|
||
const isCorrectCoinSendForm = | ||
useRouteMatch(`${process.env.ASSET_PREFIX || ''}/accounts/send`) && | ||
selectedAccount?.network?.symbol === PROTOCOL_TO_NETWORK[notification.scheme]; | ||
|
||
return ( | ||
<View | ||
{...props} | ||
action={ | ||
isCorrectCoinSendForm | ||
? { | ||
onClick: () => dispatch(protocolActions.fillSendForm(true)), | ||
label: 'TOAST_COIN_SCHEME_PROTOCOL_ACTION', | ||
position: 'bottom', | ||
variant: 'primary', | ||
} | ||
: undefined | ||
} | ||
icon={<CoinLogo symbol={PROTOCOL_TO_NETWORK[notification.scheme] as any} size={20} />} | ||
onCancel={() => dispatch(protocolActions.resetProtocol())} | ||
/> | ||
); | ||
const getIcon = (symbol?: Network['symbol']) => symbol && <CoinLogo symbol={symbol} size={20} />; | ||
|
||
export const withAoppProtocol = ( | ||
View: React.ComponentType<ViewProps>, | ||
notification: Extract<NotificationEntry, { type: 'aopp-protocol' }>, | ||
) => | ||
withConditionalAction(View, { | ||
notification, | ||
header: <Translation id="TOAST_AOPP_FILL_HEADER" />, | ||
body: notification.message, | ||
icon: getIcon(notification.asset), | ||
actionLabel: 'TOAST_AOPP_FILL_ACTION', | ||
actionCondition: { | ||
path: '/accounts/sign-verify', | ||
network: notification.asset, | ||
}, | ||
onAction: protocolActions.fillAopp(true), | ||
onCancel: protocolActions.resetProtocol(), | ||
}); | ||
return <WrappedView key={props.notification.id} />; | ||
}; | ||
|
||
export default withCoinProtocolScheme; | ||
export const withCoinProtocol = ( | ||
View: React.ComponentType<ViewProps>, | ||
notification: Extract<NotificationEntry, { type: 'coin-scheme-protocol' }>, | ||
) => | ||
withConditionalAction(View, { | ||
notification, | ||
header: <Translation id="TOAST_COIN_SCHEME_PROTOCOL_HEADER" />, | ||
body: ( | ||
<> | ||
<Row> | ||
{notification.amount && `${notification.amount} `} | ||
{capitalizeFirstLetter(notification.scheme)} | ||
</Row> | ||
<Row>{notification.address}</Row> | ||
</> | ||
), | ||
actionLabel: 'TOAST_COIN_SCHEME_PROTOCOL_ACTION', | ||
actionCondition: { | ||
path: '/accounts/send', | ||
network: PROTOCOL_TO_NETWORK[notification.scheme], | ||
}, | ||
onAction: protocolActions.fillSendForm(true), | ||
onCancel: protocolActions.resetProtocol(), | ||
icon: getIcon(PROTOCOL_TO_NETWORK[notification.scheme]), | ||
}); |
94 changes: 94 additions & 0 deletions
94
packages/suite/src/components/suite/hocNotification/components/withConditionalAction.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import React from 'react'; | ||
import styled from 'styled-components'; | ||
import { connect } from 'react-redux'; | ||
import { useRouteMatch } from 'react-router-dom'; | ||
|
||
import { useSelector } from '@suite-hooks'; | ||
import { variables } from '@trezor/components'; | ||
|
||
import type { NotificationEntry } from '@suite-reducers/notificationReducer'; | ||
import type { Dispatch, Action, ExtendedMessageDescriptor } from '@suite-types'; | ||
import type { Network } from '@wallet-types'; | ||
import type { ViewProps } from '../definitions'; | ||
|
||
const Header = styled.div` | ||
font-weight: ${variables.FONT_WEIGHT.MEDIUM}; | ||
color: ${props => props.theme.TYPE_LIGHT_GREY}; | ||
margin-top: 1px; | ||
`; | ||
|
||
const Body = styled.div` | ||
font-weight: ${variables.FONT_WEIGHT.MEDIUM}; | ||
margin-top: 1px; | ||
`; | ||
|
||
type WithConditionalActionProps = { | ||
notification: NotificationEntry; | ||
header: React.ReactNode; | ||
body: React.ReactNode; | ||
icon?: JSX.Element; | ||
actionLabel: ExtendedMessageDescriptor['id']; | ||
actionCondition: { | ||
path?: string; | ||
network?: Network['symbol']; | ||
}; | ||
onAction: Action; | ||
onCancel: Action; | ||
}; | ||
|
||
const withConditionalAction = ( | ||
View: React.ComponentType<ViewProps>, | ||
props: WithConditionalActionProps, | ||
) => { | ||
const WrappedView = connect()(({ dispatch }: { dispatch: Dispatch }) => { | ||
const { | ||
notification, | ||
header, | ||
body, | ||
icon, | ||
actionCondition: { path, network }, | ||
actionLabel, | ||
onAction, | ||
onCancel, | ||
} = props; | ||
|
||
const { selectedAccount } = useSelector(state => ({ | ||
selectedAccount: state.wallet.selectedAccount, | ||
})); | ||
|
||
const actionAllowed = | ||
(!path || useRouteMatch(`${process.env.ASSET_PREFIX || ''}${path}`)) && | ||
(!network || selectedAccount?.network?.symbol === network); | ||
|
||
const action = actionAllowed | ||
? ({ | ||
onClick: () => dispatch(onAction), | ||
label: actionLabel, | ||
position: 'bottom', | ||
variant: 'primary', | ||
} as const) | ||
: undefined; | ||
|
||
const message = { | ||
id: 'TOAST_COIN_SCHEME_PROTOCOL', | ||
values: { | ||
header: <Header>{header}</Header>, | ||
body: <Body>{body}</Body>, | ||
}, | ||
} as const; | ||
|
||
return ( | ||
<View | ||
variant="transparent" | ||
notification={notification} | ||
message={message} | ||
icon={icon} | ||
action={action} | ||
onCancel={() => dispatch(onCancel)} | ||
/> | ||
); | ||
}); | ||
return <WrappedView key={props.notification.id} />; | ||
}; | ||
|
||
export default withConditionalAction; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters