-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract out confirm-data and confirm-hex-data components from confirm…
…-transaction-base.component.js (#17822)
- Loading branch information
Showing
21 changed files
with
628 additions
and
178 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Story, Canvas, ArgsTable } from '@storybook/addon-docs'; | ||
import { ConfirmData } from '.'; | ||
|
||
# Confirm Data | ||
|
||
Confirm Data is used on confirmation screen to display transaction data. | ||
|
||
<Canvas> | ||
<Story id="components-app-ConfirmData--default-story" /> | ||
</Canvas> |
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,72 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { useSelector } from 'react-redux'; | ||
|
||
import { | ||
Color, | ||
TextVariant, | ||
TEXT_TRANSFORM, | ||
} from '../../../helpers/constants/design-system'; | ||
import { getKnownMethodData } from '../../../selectors'; | ||
import { useI18nContext } from '../../../hooks/useI18nContext'; | ||
import { useTransactionFunctionType } from '../../../hooks/useTransactionFunctionType'; | ||
|
||
import Box from '../../ui/box/box'; | ||
import Disclosure from '../../ui/disclosure'; | ||
import TransactionDecoding from '../transaction-decoding'; | ||
import { Text } from '../../component-library'; | ||
|
||
const ConfirmData = ({ txData, dataComponent }) => { | ||
const t = useI18nContext(); | ||
const { txParams = {} } = txData; | ||
const methodData = useSelector( | ||
(state) => getKnownMethodData(state, txParams.data) || {}, | ||
); | ||
const { functionType } = useTransactionFunctionType(txData); | ||
|
||
if (dataComponent) { | ||
return dataComponent; | ||
} | ||
|
||
if (!txParams.data) { | ||
return null; | ||
} | ||
|
||
const { params } = methodData; | ||
const functionParams = params?.length | ||
? `(${params.map(({ type }) => type).join(', ')})` | ||
: ''; | ||
|
||
return ( | ||
<Box color={Color.textAlternative} className="confirm-data" padding={4}> | ||
<Box paddingBottom={3} paddingTop={2}> | ||
<Text | ||
as="span" | ||
textTransform={TEXT_TRANSFORM.UPPERCASE} | ||
variant={TextVariant.bodySm} | ||
> | ||
{`${t('functionType')}:`} | ||
</Text> | ||
<Text | ||
as="span" | ||
color={Color.textDefault} | ||
paddingLeft={1} | ||
textTransform={TEXT_TRANSFORM.CAPITALIZE} | ||
variant={TextVariant.bodySmBold} | ||
> | ||
{`${functionType} ${functionParams}`} | ||
</Text> | ||
</Box> | ||
<Disclosure> | ||
<TransactionDecoding to={txParams?.to} inputData={txParams?.data} /> | ||
</Disclosure> | ||
</Box> | ||
); | ||
}; | ||
|
||
ConfirmData.propTypes = { | ||
txData: PropTypes.object, | ||
dataComponent: PropTypes.element, | ||
}; | ||
|
||
export default ConfirmData; |
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,46 @@ | ||
import React from 'react'; | ||
import README from './README.mdx'; | ||
import ConfirmData from './confirm-data'; | ||
|
||
export default { | ||
title: 'Components/App/ConfirmData', | ||
|
||
component: ConfirmData, | ||
parameters: { | ||
docs: { | ||
page: README, | ||
}, | ||
}, | ||
argTypes: { | ||
txData: { | ||
control: 'object', | ||
}, | ||
dataHexComponent: { | ||
control: 'element', | ||
}, | ||
}, | ||
args: { | ||
txData: { | ||
txParams: { | ||
data: '0xa9059cbb000000000000000000000000b19ac54efa18cc3a14a5b821bfec73d284bf0c5e0000000000000000000000000000000000000000000000003782dace9d900000', | ||
}, | ||
origin: 'https://metamask.github.io', | ||
type: 'transfer', | ||
}, | ||
}, | ||
}; | ||
|
||
export const DefaultStory = (args) => { | ||
return <ConfirmData {...args} />; | ||
}; | ||
|
||
DefaultStory.storyName = 'Default'; | ||
|
||
export const DataComponentStory = (args) => { | ||
return <ConfirmData {...args} />; | ||
}; | ||
|
||
DataComponentStory.storyName = 'DataComponent'; | ||
DataComponentStory.args = { | ||
dataComponent: <div>Any custom component passed in props</div>, | ||
}; |
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,58 @@ | ||
import React from 'react'; | ||
|
||
import mockState from '../../../../test/data/mock-state.json'; | ||
import { renderWithProvider } from '../../../../test/jest'; | ||
|
||
import configureStore from '../../../store/store'; | ||
import ConfirmData from './confirm-data'; | ||
|
||
jest.mock('../../../../shared/lib/fetch-with-cache'); | ||
|
||
describe('ConfirmData', () => { | ||
const store = configureStore(mockState); | ||
|
||
it('should render function type', async () => { | ||
const { findByText } = renderWithProvider( | ||
<ConfirmData | ||
txData={{ | ||
txParams: { | ||
data: '0x608060405234801', | ||
}, | ||
origin: 'https://metamask.github.io', | ||
type: 'transfer', | ||
}} | ||
/>, | ||
store, | ||
); | ||
expect(await findByText('Transfer')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should return null if transaction has no data', () => { | ||
const { container } = renderWithProvider( | ||
<ConfirmData | ||
txData={{ | ||
txParams: {}, | ||
origin: 'https://metamask.github.io', | ||
type: 'transfer', | ||
}} | ||
/>, | ||
store, | ||
); | ||
expect(container.firstChild).toStrictEqual(null); | ||
}); | ||
|
||
it('should render dataComponent if passed', () => { | ||
const { getByText } = renderWithProvider( | ||
<ConfirmData | ||
txData={{ | ||
txParams: {}, | ||
origin: 'https://metamask.github.io', | ||
type: 'transfer', | ||
}} | ||
dataComponent={<span>Data Component</span>} | ||
/>, | ||
store, | ||
); | ||
expect(getByText('Data Component')).toBeInTheDocument(); | ||
}); | ||
}); |
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 @@ | ||
export { default as ConfirmData } from './confirm-data'; |
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,5 @@ | ||
.confirm-data { | ||
& > .disclosure { | ||
margin-top: 0; | ||
} | ||
} |
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,10 @@ | ||
import { Story, Canvas, ArgsTable } from '@storybook/addon-docs'; | ||
import { ConfirmHexData } from '.'; | ||
|
||
# Confirm Data | ||
|
||
Confirm Hex Data is used on confirmation screen to display transaction data. | ||
|
||
<Canvas> | ||
<Story id="components-app-ConfirmHexData--default-story" /> | ||
</Canvas> |
Oops, something went wrong.