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: persist bridge state through ToS click #29366

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
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
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) {
infiniteflower marked this conversation as resolved.
Show resolved Hide resolved
// 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
Loading