-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat: improve price impact row and warnings #26390
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
Merged
Merged
Changes from 13 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
838ba41
feat: yellow warning price impact
bfullam 7e5a91b
chore: move constants to AppConstants
bfullam 49ceffd
Merge branch 'main' into codex/review-swaps4020-details
bfullam a2fd9de
chore: Merge branch 'main' into codex/review-swaps4020-details
GeorgeGkas cc9869c
fix: add missing icon in price impact value
GeorgeGkas a27d678
refactor: migrate to text design system component
GeorgeGkas fa53cfd
feat: add price impact friction
GeorgeGkas 9635b44
chore: Merge branch 'main' into codex/review-swaps4020-details
GeorgeGkas c741010
fix: various issues
GeorgeGkas 213c19e
fix: do not render loading state on cancel button
GeorgeGkas 3790391
Merge branch 'main' into codex/review-swaps4020-details
GeorgeGkas 82b2355
Merge branch 'main' into codex/review-swaps4020-details
GeorgeGkas c3d9436
fix: code review
GeorgeGkas a6d98c7
fix: formatPriceImpact edge cases
GeorgeGkas e82722c
fix: always render the price impact row
GeorgeGkas 4e11fa0
Merge branch 'main' into codex/review-swaps4020-details
GeorgeGkas 3941641
fix: linting issues
GeorgeGkas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
177 changes: 177 additions & 0 deletions
177
app/components/UI/Bridge/components/PriceImpactModal/PriceImpactDescription.test.tsx
This file contains hidden or 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,177 @@ | ||
| import React from 'react'; | ||
| import { render } from '@testing-library/react-native'; | ||
| import { PriceImpactDescription } from './PriceImpactDescription'; | ||
| import { PriceImpactModalType } from './constants'; | ||
| import { strings } from '../../../../../../locales/i18n'; | ||
|
|
||
| describe('PriceImpactDescription', () => { | ||
| describe('Execution type', () => { | ||
| it('renders the execution description with the given priceImpact', () => { | ||
| const { getByText } = render( | ||
| <PriceImpactDescription | ||
| type={PriceImpactModalType.Execution} | ||
| priceImpact="-30%" | ||
| />, | ||
| ); | ||
|
|
||
| expect( | ||
| getByText( | ||
| strings('bridge.price_impact_execution_description', { | ||
| priceImpact: '-30%', | ||
| }), | ||
| ), | ||
| ).toBeTruthy(); | ||
| }); | ||
|
|
||
| it('renders the execution description with "0" when priceImpact is undefined', () => { | ||
| const { getByText } = render( | ||
| <PriceImpactDescription | ||
| type={PriceImpactModalType.Execution} | ||
| priceImpact={undefined} | ||
| />, | ||
| ); | ||
|
|
||
| expect( | ||
| getByText( | ||
| strings('bridge.price_impact_execution_description', { | ||
| priceImpact: '0', | ||
| }), | ||
| ), | ||
| ).toBeTruthy(); | ||
| }); | ||
|
|
||
| it('does not render the info description', () => { | ||
| const { queryByText } = render( | ||
| <PriceImpactDescription | ||
| type={PriceImpactModalType.Execution} | ||
| priceImpact="-30%" | ||
| />, | ||
| ); | ||
|
|
||
| expect( | ||
| queryByText(strings('bridge.price_impact_info_description')), | ||
| ).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Info type — with priceImpact (warning state)', () => { | ||
| it('renders the warning description with the given priceImpact', () => { | ||
| const { getByText } = render( | ||
| <PriceImpactDescription | ||
| type={PriceImpactModalType.Info} | ||
| priceImpact="-10%" | ||
| />, | ||
| ); | ||
|
|
||
| expect( | ||
| getByText( | ||
| strings('bridge.price_impact_warning_description', { | ||
| priceImpact: '-10%', | ||
| }), | ||
| ), | ||
| ).toBeTruthy(); | ||
| }); | ||
|
|
||
| it('does not render the info description when priceImpact is provided', () => { | ||
| const { queryByText } = render( | ||
| <PriceImpactDescription | ||
| type={PriceImpactModalType.Info} | ||
| priceImpact="-10%" | ||
| />, | ||
| ); | ||
|
|
||
| expect( | ||
| queryByText(strings('bridge.price_impact_info_description')), | ||
| ).toBeNull(); | ||
| }); | ||
|
|
||
| it('treats the string "0" as a truthy priceImpact and renders the warning description', () => { | ||
| const { getByText } = render( | ||
| <PriceImpactDescription | ||
| type={PriceImpactModalType.Info} | ||
| priceImpact="0" | ||
| />, | ||
| ); | ||
|
|
||
| expect( | ||
| getByText( | ||
| strings('bridge.price_impact_warning_description', { | ||
| priceImpact: '0', | ||
| }), | ||
| ), | ||
| ).toBeTruthy(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Info type — without priceImpact (info state)', () => { | ||
| it('renders the info description when priceImpact is undefined', () => { | ||
| const { getByText } = render( | ||
| <PriceImpactDescription | ||
| type={PriceImpactModalType.Info} | ||
| priceImpact={undefined} | ||
| />, | ||
| ); | ||
|
|
||
| expect( | ||
| getByText(strings('bridge.price_impact_info_description')), | ||
| ).toBeTruthy(); | ||
| }); | ||
|
|
||
| it('renders the info description when priceImpact is an empty string', () => { | ||
| const { getByText } = render( | ||
| <PriceImpactDescription | ||
| type={PriceImpactModalType.Info} | ||
| priceImpact="" | ||
| />, | ||
| ); | ||
|
|
||
| expect( | ||
| getByText(strings('bridge.price_impact_info_description')), | ||
| ).toBeTruthy(); | ||
| }); | ||
|
|
||
| it('does not render the warning description when priceImpact is absent', () => { | ||
| const { queryByText } = render( | ||
| <PriceImpactDescription | ||
| type={PriceImpactModalType.Info} | ||
| priceImpact={undefined} | ||
| />, | ||
| ); | ||
|
|
||
| expect( | ||
| queryByText( | ||
| strings('bridge.price_impact_warning_description', { | ||
| priceImpact: undefined, | ||
| }), | ||
| ), | ||
| ).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('priority — Execution type takes precedence over warning state', () => { | ||
| it('renders the execution description rather than the warning description when type is Execution and priceImpact is provided', () => { | ||
| const { getByText, queryByText } = render( | ||
| <PriceImpactDescription | ||
| type={PriceImpactModalType.Execution} | ||
| priceImpact="-10%" | ||
| />, | ||
| ); | ||
|
|
||
| expect( | ||
| getByText( | ||
| strings('bridge.price_impact_execution_description', { | ||
| priceImpact: '-10%', | ||
| }), | ||
| ), | ||
| ).toBeTruthy(); | ||
|
|
||
| expect( | ||
| queryByText( | ||
| strings('bridge.price_impact_warning_description', { | ||
| priceImpact: '-10%', | ||
| }), | ||
| ), | ||
| ).toBeNull(); | ||
| }); | ||
| }); | ||
| }); |
36 changes: 36 additions & 0 deletions
36
app/components/UI/Bridge/components/PriceImpactModal/PriceImpactDescription.tsx
This file contains hidden or 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,36 @@ | ||
| import React, { useMemo } from 'react'; | ||
| import { strings } from '../../../../../../locales/i18n'; | ||
| import { Box, Text, TextColor } from '@metamask/design-system-react-native'; | ||
| import { PriceImpactModalType } from './constants'; | ||
|
|
||
| interface PriceImpactDescriptionProps { | ||
| type: PriceImpactModalType; | ||
| priceImpact?: string; | ||
| } | ||
|
|
||
| export function PriceImpactDescription({ | ||
| type, | ||
| priceImpact, | ||
| }: PriceImpactDescriptionProps) { | ||
| const isWarning = Boolean(priceImpact); | ||
|
|
||
| const body = useMemo(() => { | ||
| if (type === PriceImpactModalType.Execution) { | ||
| return strings('bridge.price_impact_execution_description', { | ||
| priceImpact: priceImpact ?? '0', | ||
| }); | ||
| } | ||
| if (isWarning) { | ||
| return strings('bridge.price_impact_warning_description', { | ||
| priceImpact: priceImpact ?? '0', | ||
| }); | ||
| } | ||
| return strings('bridge.price_impact_info_description'); | ||
| }, [type, priceImpact, isWarning]); | ||
|
|
||
| return ( | ||
| <Box paddingHorizontal={4} paddingVertical={2}> | ||
| <Text color={TextColor.TextAlternative}>{body}</Text> | ||
| </Box> | ||
| ); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should avoid using memoizing callbacks for logic that's not expensive. For example, this one can simply become: