Skip to content

Commit

Permalink
feat: persist bridge state through ToS click (#29366)
Browse files Browse the repository at this point in the history
<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

## **Description**

Currently, clicking on Terms takes you to an external page and resets
your bridge progress. We want to follow the same UX as swaps, where if
the user clicks on the ToS link, the extension will close, but when the
user reopens the extension, they remain on the bridge page.

Acceptance Criteria:

When clicking on the ToS link, the extension closes. But when the user
re-opens the extension, they see the bridge page with tokens inputted
(but not amounts)

<!--
Write a short description of the changes included in this pull request,
also include relevant motivation and context. Have in mind the following
questions:
1. What is the reason for the change?
2. What is the improvement/solution?
-->

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/29366?quickstart=1)

## **Related issues**

Fixes:

## **Manual testing steps**

1. Go to bridge
2. Put in tokens/amounts to get quote
3. Click "terms" link
4. Reopen extension and see inputs persisted

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**

<!-- [screenshots/recordings] -->

### **After**

<!-- [screenshots/recordings] -->

## **Pre-merge author checklist**

- [x] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I’ve included tests if applicable
- [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
  • Loading branch information
bfullam authored Jan 13, 2025
1 parent 531643c commit 4e32d32
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 9 deletions.
2 changes: 1 addition & 1 deletion ui/pages/bridge/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,6 @@ describe('Bridge', () => {

expect(getByText('Bridge')).toBeInTheDocument();
expect(container).toMatchSnapshot();
expect(mockResetBridgeState).toHaveBeenCalledTimes(2);
expect(mockResetBridgeState).toHaveBeenCalledTimes(1);
});
});
2 changes: 0 additions & 2 deletions ui/pages/bridge/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ const CrossChainSwap = () => {

useEffect(() => {
// Reset controller and inputs before unloading the page
resetControllerAndInputStates();

window.addEventListener('beforeunload', resetControllerAndInputStates);

return () => {
Expand Down
8 changes: 6 additions & 2 deletions ui/pages/bridge/prepare/bridge-input-group.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import { TabName } from '../../../components/multichain/asset-picker-amount/asse
import { useI18nContext } from '../../../hooks/useI18nContext';
import { getLocale } from '../../../selectors';
import { getCurrentCurrency } from '../../../ducks/metamask/metamask';
import { formatCurrencyAmount, formatTokenAmount } from '../utils/quote';
import {
formatCurrencyAmount,
formatTokenAmount,
isNativeAddress,
} from '../utils/quote';
import { Column, Row } from '../layout';
import {
Display,
Expand Down Expand Up @@ -234,7 +238,7 @@ export const BridgeInputGroup = ({
: undefined}
{onMaxButtonClick &&
token &&
token.type !== AssetType.native &&
!isNativeAddress(token.address) &&
balanceAmount && (
<ButtonLink
variant={TextVariant.bodyMd}
Expand Down
29 changes: 26 additions & 3 deletions ui/pages/bridge/prepare/prepare-bridge-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ import { useI18nContext } from '../../../hooks/useI18nContext';
import { SWAPS_CHAINID_DEFAULT_TOKEN_MAP } from '../../../../shared/constants/swaps';
import { useTokensWithFiltering } from '../../../hooks/bridge/useTokensWithFiltering';
import { setActiveNetwork } from '../../../store/actions';
import { hexToDecimal } from '../../../../shared/modules/conversion.utils';
import {
hexToDecimal,
decimalToPrefixedHex,
} from '../../../../shared/modules/conversion.utils';
import type { QuoteRequest } from '../../../../shared/types/bridge';
import { calcTokenValue } from '../../../../shared/lib/swaps-utils';
import { BridgeQuoteCard } from '../quotes/bridge-quote-card';
Expand Down Expand Up @@ -214,8 +217,28 @@ const PrepareBridgePage = () => {
}, [rotateSwitchTokens]);

useEffect(() => {
// Reset controller and inputs on load
dispatch(resetBridgeState());
if (activeQuote) {
// Get input data from active quote
const { srcAsset, destAsset, destChainId } = activeQuote.quote;
const quoteSrcToken = fromTokens[srcAsset.address.toLowerCase()];
const quoteDestChainId = decimalToPrefixedHex(destChainId);
const quoteDestToken = toTokens[destAsset.address.toLowerCase()];

if (quoteSrcToken && quoteDestToken && quoteDestChainId) {
// Set inputs to values from active quote
dispatch(setFromTokenInputValue(null));
dispatch(
setFromToken({ ...quoteSrcToken, image: quoteSrcToken.iconUrl }),
);
dispatch(setToChainId(quoteDestChainId));
dispatch(
setToToken({ ...quoteDestToken, image: quoteDestToken.iconUrl }),
);
}
} else {
// Reset controller and inputs on load
dispatch(resetBridgeState());
}
}, []);

// Scroll to bottom of the page when banners are shown
Expand Down
11 changes: 10 additions & 1 deletion ui/pages/home/home.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import {
CONNECTED_ACCOUNTS_ROUTE,
AWAITING_SWAP_ROUTE,
PREPARE_SWAP_ROUTE,
CROSS_CHAIN_SWAP_ROUTE,
///: BEGIN:ONLY_INCLUDE_IF(build-main,build-beta,build-flask)
ONBOARDING_SECURE_YOUR_WALLET_ROUTE,
///: END:ONLY_INCLUDE_IF
Expand Down Expand Up @@ -159,6 +160,7 @@ export default class Home extends PureComponent {
onTabClick: PropTypes.func.isRequired,
haveSwapsQuotes: PropTypes.bool.isRequired,
showAwaitingSwapScreen: PropTypes.bool.isRequired,
haveBridgeQuotes: PropTypes.bool.isRequired,
setDataCollectionForMarketing: PropTypes.func.isRequired,
dataCollectionForMarketing: PropTypes.bool,
swapsFetchParams: PropTypes.object,
Expand Down Expand Up @@ -220,6 +222,7 @@ export default class Home extends PureComponent {
const {
closeNotificationPopup,
haveSwapsQuotes,
haveBridgeQuotes,
isNotification,
pendingApprovals,
showAwaitingSwapScreen,
Expand All @@ -235,7 +238,10 @@ export default class Home extends PureComponent {
pendingApprovals.length ||
(!isNotification &&
!stayOnHomePage &&
(showAwaitingSwapScreen || haveSwapsQuotes || swapsFetchParams))
(showAwaitingSwapScreen ||
haveSwapsQuotes ||
swapsFetchParams ||
haveBridgeQuotes))
) {
this.state.redirecting = true;
}
Expand Down Expand Up @@ -289,6 +295,7 @@ export default class Home extends PureComponent {
history,
isNotification,
haveSwapsQuotes,
haveBridgeQuotes,
showAwaitingSwapScreen,
swapsFetchParams,
location,
Expand All @@ -306,6 +313,8 @@ export default class Home extends PureComponent {
history.push(AWAITING_SWAP_ROUTE);
} else if (canRedirect && (haveSwapsQuotes || swapsFetchParams)) {
history.push(PREPARE_SWAP_ROUTE);
} else if (canRedirect && haveBridgeQuotes) {
history.push(CROSS_CHAIN_SWAP_ROUTE + PREPARE_SWAP_ROUTE);
} else if (pendingApprovals.length) {
navigateToConfirmation(
pendingApprovals[0].id,
Expand Down
2 changes: 2 additions & 0 deletions ui/pages/home/home.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ const mapStateToProps = (state) => {
connectedStatusPopoverHasBeenShown,
defaultHomeActiveTabName,
swapsState,
bridgeState,
dataCollectionForMarketing,
participateInMetaMetrics,
firstTimeFlowType,
Expand Down Expand Up @@ -161,6 +162,7 @@ const mapStateToProps = (state) => {
haveSwapsQuotes: Boolean(Object.values(swapsState.quotes || {}).length),
swapsFetchParams: swapsState.fetchParams,
showAwaitingSwapScreen: swapsState.routeState === 'awaiting',
haveBridgeQuotes: Boolean(Object.values(bridgeState?.quotes || {}).length),
isMainnet: getIsMainnet(state),
originOfCurrentTab,
shouldShowWeb3ShimUsageNotification,
Expand Down

0 comments on commit 4e32d32

Please sign in to comment.