Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as React from 'react';
import { fireEvent, waitFor } from '@testing-library/react';
import { useLocation } from 'react-router-dom';
import configureStore from '../../../store/store';
import { renderWithProvider } from '../../../../test/lib/render-helpers';
import { renderWithProvider } from '../../../../test/lib/render-helpers-navigate';
import * as Actions from '../../../store/actions';
import {
hideBasicFunctionalityModal,
Expand Down Expand Up @@ -32,9 +31,10 @@ jest.mock('react-redux', () => {
};
});

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useLocation: jest.fn(),
const mockUseLocation = jest.fn();
jest.mock('react-router-dom-v5-compat', () => ({
...jest.requireActual('react-router-dom-v5-compat'),
useLocation: () => mockUseLocation(),
}));

// TODO: Fix in https://github.com/MetaMask/metamask-extension/issues/31860
Expand Down Expand Up @@ -68,7 +68,7 @@ const arrangeMocks = <T extends boolean>({
}: ArrangeMocksParams<T> = {}): ArrangeMocksReturn<T> => {
jest.clearAllMocks();

(useLocation as jest.Mock).mockReturnValue({
(mockUseLocation as jest.Mock).mockReturnValue({
pathname: isOnboarding
? ONBOARDING_PRIVACY_SETTINGS_ROUTE
: '/any-other-path',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useContext, useMemo, useState } from 'react';
import { useLocation } from 'react-router-dom';
import { useLocation } from 'react-router-dom-v5-compat';
import { useDispatch, useSelector } from 'react-redux';
import {
Display,
Expand Down
6 changes: 3 additions & 3 deletions ui/components/app/connected-sites-list/connected-snaps.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState } from 'react';
import PropTypes from 'prop-types';
import { useHistory } from 'react-router-dom';
import { useNavigate } from 'react-router-dom-v5-compat';
import { useDispatch, useSelector } from 'react-redux';
import { Box, IconName, IconSize, Text } from '../../component-library';
import { useI18nContext } from '../../../hooks/useI18nContext';
Expand All @@ -22,7 +22,7 @@ import { SnapIcon } from '../snaps/snap-icon';
export default function ConnectedSnaps({ connectedSubjects }) {
const [showOptions, setShowOptions] = useState();
const t = useI18nContext();
const history = useHistory();
const navigate = useNavigate();
const dispatch = useDispatch();
const connectedOrigin = useSelector(getOriginOfCurrentTab);

Expand All @@ -48,7 +48,7 @@ export default function ConnectedSnaps({ connectedSubjects }) {
</MenuItem>
<MenuItem
iconName={IconName.Setting}
onClick={() => history.push(getSnapRoute(snapId))}
onClick={() => navigate(getSnapRoute(snapId))}
>
{t('snapsSettings')}
</MenuItem>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import PropTypes from 'prop-types';
import { useHistory } from 'react-router-dom';
import { useNavigate } from 'react-router-dom-v5-compat';
import { useDispatch, useSelector } from 'react-redux';
import Modal from '../../modal';
import { Text } from '../../../component-library/text';
Expand All @@ -13,7 +13,7 @@ import { isEqualCaseInsensitive } from '../../../../../shared/modules/string-uti
import { getSelectedNetworkClientId } from '../../../../../shared/modules/selectors/networks';

const ConvertTokenToNFTModal = ({ hideModal, tokenAddress }) => {
const history = useHistory();
const navigate = useNavigate();
const t = useI18nContext();
const dispatch = useDispatch();
const allNfts = useSelector(getNfts);
Expand All @@ -34,9 +34,7 @@ const ConvertTokenToNFTModal = ({ hideModal, tokenAddress }) => {
}),
);
const { tokenId } = tokenAddedAsNFT;
history.push({
pathname: `${ASSET_ROUTE}/${tokenAddress}/${tokenId}`,
});
navigate(`${ASSET_ROUTE}/${tokenAddress}/${tokenId}`);
} else {
dispatch(
showImportNftsModal({ tokenAddress, ignoreErc20Token: true }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ jest.mock('react-redux', () => ({
useDispatch: () => mockDispatch,
}));

const mockHistoryPush = jest.fn();
jest.mock('react-router-dom', () => ({
useHistory: () => ({
push: mockHistoryPush,
}),
}));
const mockUseNavigate = jest.fn();
jest.mock('react-router-dom-v5-compat', () => {
return {
...jest.requireActual('react-router-dom-v5-compat'),
useNavigate: () => mockUseNavigate,
};
});

const mockSetIsBackupAndSyncFeatureEnabled = jest.fn();
jest.mock('../../../../../hooks/identity/useBackupAndSync', () => ({
Expand Down Expand Up @@ -151,7 +152,7 @@ describe('TurnOnBackupAndSyncModal', () => {
fireEvent.click(button);

await waitFor(() => {
expect(mockHistoryPush).toHaveBeenCalledWith(BACKUPANDSYNC_ROUTE);
expect(mockUseNavigate).toHaveBeenCalledWith(BACKUPANDSYNC_ROUTE);
expect(mockSetIsBackupAndSyncFeatureEnabled).toHaveBeenCalledWith(
BACKUPANDSYNC_FEATURES.main,
true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useContext } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { useHistory } from 'react-router-dom';
import { useNavigate } from 'react-router-dom-v5-compat';
import { BACKUPANDSYNC_FEATURES } from '@metamask/profile-sync-controller/user-storage';
import { useModalProps } from '../../../../../hooks/useModalProps';
import {
Expand Down Expand Up @@ -47,7 +47,7 @@ export const turnOnBackupAndSyncModalTestIds = {
// eslint-disable-next-line @typescript-eslint/naming-convention
export function TurnOnBackupAndSyncModal() {
const { hideModal } = useModalProps();
const history = useHistory();
const navigate = useNavigate();
const dispatch = useDispatch();
const t = useI18nContext();
const trackEvent = useContext(MetaMetricsContext);
Expand Down Expand Up @@ -89,7 +89,7 @@ export function TurnOnBackupAndSyncModal() {
showModal({
name: CONFIRM_TURN_ON_BACKUP_AND_SYNC_MODAL_NAME,
enableBackupAndSync: async () => {
history.push(BACKUPANDSYNC_ROUTE);
navigate(BACKUPANDSYNC_ROUTE);
await setIsBackupAndSyncFeatureEnabled(
BACKUPANDSYNC_FEATURES.main,
true,
Expand All @@ -102,7 +102,7 @@ export function TurnOnBackupAndSyncModal() {
if (!isBackupAndSyncEnabled) {
await setIsBackupAndSyncFeatureEnabled(BACKUPANDSYNC_FEATURES.main, true);
}
history.push(BACKUPANDSYNC_ROUTE);
navigate(BACKUPANDSYNC_ROUTE);
hideModal();
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import React, {
useMemo,
} from 'react';
import { useDispatch } from 'react-redux';
import { useHistory } from 'react-router-dom';
import { useNavigate } from 'react-router-dom-v5-compat';

import { captureException } from '../../../../../../shared/lib/sentry';

Expand All @@ -28,7 +28,7 @@ export const MultichainAccountIntroModalContainer: React.FC<ContainerProps> = ({
onClose,
}) => {
const dispatch = useDispatch();
const history = useHistory();
const navigate = useNavigate();
const [isLoading, setIsLoading] = useState(false);
const isClosingRef = useRef(false);

Expand Down Expand Up @@ -78,8 +78,8 @@ export const MultichainAccountIntroModalContainer: React.FC<ContainerProps> = ({
onClose();

// Navigate to account list
history.push(ACCOUNT_LIST_PAGE_ROUTE);
}, [alignmentPromise, dispatch, history, onClose]);
navigate(ACCOUNT_LIST_PAGE_ROUTE);
}, [alignmentPromise, dispatch, navigate, onClose]);

const handleLearnMore = useCallback(() => {
// Open multichain accounts support page
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import configureMockStore from 'redux-mock-store';
import mockStore from '../../../../../test/data/mock-state.json';
import { renderWithProvider } from '../../../../../test/jest';
import { renderWithProvider } from '../../../../../test/lib/render-helpers-navigate';
import { MetamaskNotificationsProvider } from '../../../../contexts/metamask-notifications/metamask-notifications';
import TurnOnMetamaskNotifications from './turn-on-metamask-notifications';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useContext, useEffect, useState } from 'react';
import { useSelector } from 'react-redux';
import { useHistory } from 'react-router-dom';
import { useNavigate } from 'react-router-dom-v5-compat';
import { I18nContext } from '../../../../contexts/i18n';
import { useModalProps } from '../../../../hooks/useModalProps';
import { useMetamaskNotificationsContext } from '../../../../contexts/metamask-notifications/metamask-notifications';
Expand Down Expand Up @@ -40,7 +40,7 @@ import {
// eslint-disable-next-line @typescript-eslint/naming-convention
export default function TurnOnMetamaskNotifications() {
const { hideModal } = useModalProps();
const history = useHistory();
const navigate = useNavigate();
const t = useContext(I18nContext);
const trackEvent = useContext(MetaMetricsContext);
const { listNotifications } = useMetamaskNotificationsContext();
Expand Down Expand Up @@ -99,11 +99,11 @@ export default function TurnOnMetamaskNotifications() {

useEffect(() => {
if (isNotificationEnabled && !error) {
history.push(NOTIFICATIONS_ROUTE);
navigate(NOTIFICATIONS_ROUTE);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Stale Dependency in useEffect

The useEffect dependency array still references history after the variable was renamed to navigate. This makes history undefined, which will cause a ReferenceError.

Fix in Cursor Fix in Web

hideModal();
listNotifications();
}
}, [isNotificationEnabled, error, history, hideModal, listNotifications]);
}, [isNotificationEnabled, error, navigate, hideModal, listNotifications]);

const privacyLink = (
<Text
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { useDispatch } from 'react-redux';
import { useHistory } from 'react-router-dom';
import { useNavigate } from 'react-router-dom-v5-compat';
import { useI18nContext } from '../../../hooks/useI18nContext';
import {
AlignItems,
Expand Down Expand Up @@ -32,7 +32,7 @@ import { setShowPasswordChangeToast } from '../toast-master/utils';
export default function PasswordOutdatedModal() {
const t = useI18nContext();
const dispatch = useDispatch();
const history = useHistory();
const navigate = useNavigate();

return (
<Modal
Expand Down Expand Up @@ -73,7 +73,7 @@ export default function PasswordOutdatedModal() {
// remove the password change toast from the app state
await dispatch(setShowPasswordChangeToast(null));
await dispatch(lockMetamask());
history.push(DEFAULT_ROUTE);
navigate(DEFAULT_ROUTE);
}}
>
{t('continue')}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import PropTypes from 'prop-types';
import { useHistory } from 'react-router-dom';
import { useNavigate } from 'react-router-dom-v5-compat';
import { useI18nContext } from '../../../hooks/useI18nContext';
// Helpers
import {
Expand Down Expand Up @@ -33,11 +33,11 @@ import {

export default function RecoveryPhraseReminder({ onConfirm }) {
const t = useI18nContext();
const history = useHistory();
const navigate = useNavigate();

const handleBackUp = () => {
const backUpSRPRoute = `${ONBOARDING_REVEAL_SRP_ROUTE}/?isFromReminder=true`;
history.push(backUpSRPRoute);
navigate(backUpSRPRoute);
};

return (
Expand Down
2 changes: 1 addition & 1 deletion ui/components/app/srp-quiz-modal/SRPQuiz/SRPQuiz.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { fireEvent, screen, waitFor } from '@testing-library/react';
import React from 'react';
import mockState from '../../../../../test/data/mock-state.json';
import { renderWithProvider } from '../../../../../test/jest';
import { renderWithProvider } from '../../../../../test/lib/render-helpers-navigate';
import ZENDESK_URLS from '../../../../helpers/constants/zendesk-url';
import configureStore from '../../../../store/store';
import { QuizStage } from '../types';
Expand Down
16 changes: 8 additions & 8 deletions ui/components/app/toast-master/toast-master.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

import React, { useEffect, useMemo, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { useHistory, useLocation } from 'react-router-dom';
import { useNavigate } from 'react-router-dom-v5-compat';
import classnames from 'classnames';
import { getAllScopesFromCaip25CaveatValue } from '@metamask/chain-agnostic-permission';
import { AvatarAccountSize } from '@metamask/design-system-react';
import { PRODUCT_TYPES } from '@metamask/subscription-controller';
import { useNavigate } from 'react-router-dom-v5-compat';
import { MILLISECOND, SECOND } from '../../../../shared/constants/time';
import {
PRIVACY_POLICY_LINK,
Expand Down Expand Up @@ -98,14 +97,15 @@ import {
setShieldEndingToastLastClickedOrClosed,
} from './utils';

export function ToastMaster() {
const location = useLocation();
export function ToastMaster({ location } = {}) {
const isMultichainAccountsFeatureState2Enabled = useSelector(
getIsMultichainAccountsState2Enabled,
);

const onHomeScreen = location.pathname === DEFAULT_ROUTE;
const onSettingsScreen = location.pathname.startsWith(SETTINGS_ROUTE);
// Use passed location or fallback to DEFAULT_ROUTE
const currentPathname = location?.pathname ?? DEFAULT_ROUTE;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ToastMaster was using useLocation() from v5-compat while being rendered in a v5 router context, causing the survey toast to not render correctly. By passing location as a prop rather than having the component fetch it via a mismatched router context can eliminate the router context mismatch.

const onHomeScreen = currentPathname === DEFAULT_ROUTE;
const onSettingsScreen = currentPathname.startsWith(SETTINGS_ROUTE);

if (onHomeScreen) {
return (
Expand Down Expand Up @@ -391,7 +391,7 @@ function PermittedNetworkToast() {
const activeTabOrigin = useSelector(getOriginOfCurrentTab);
const dappActiveNetwork = useSelector(getDappActiveNetwork);
const safeEncodedHost = encodeURIComponent(activeTabOrigin);
const history = useHistory();
const navigate = useNavigate();

// Use dapp's active network if available, otherwise fall back to global network
const displayNetwork = dappActiveNetwork || currentNetwork;
Expand Down Expand Up @@ -429,7 +429,7 @@ function PermittedNetworkToast() {
actionText={t('editPermissions')}
onActionClick={() => {
dispatch(hidePermittedNetworkToast());
history.push(`${REVIEW_PERMISSIONS}/${safeEncodedHost}`);
navigate(`${REVIEW_PERMISSIONS}/${safeEncodedHost}`);
}}
onClose={() => dispatch(hidePermittedNetworkToast())}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { connect } from 'react-redux';
import { withRouter } from 'react-router-dom';
import { compose } from 'redux';
import withRouterHooks from '../../../helpers/higher-order-components/with-router-hooks/with-router-hooks';
import { getNetworkConfigurationsByChainId } from '../../../../shared/modules/selectors/networks';
import {
getAccountName,
Expand Down Expand Up @@ -49,6 +49,6 @@ const mapDispatchToProps = (dispatch) => {
};

export default compose(
withRouter,
withRouterHooks,
connect(mapStateToProps, mapDispatchToProps),
)(TransactionListItemDetails);
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import React, { useMemo, useState, useCallback, useContext } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { useHistory } from 'react-router-dom';
import { useNavigate } from 'react-router-dom-v5-compat';
import { useDispatch, useSelector } from 'react-redux';

import {
Expand Down Expand Up @@ -78,7 +78,7 @@ function TransactionListItemInner({
chainId,
}) {
const t = useI18nContext();
const history = useHistory();
const navigate = useNavigate();
const { hasCancelled } = transactionGroup;
const [showDetails, setShowDetails] = useState(false);
const [showCancelEditGasPopover, setShowCancelEditGasPopover] =
Expand Down Expand Up @@ -232,7 +232,7 @@ function TransactionListItemInner({

const toggleShowDetails = useCallback(() => {
if (isUnapproved) {
history.push(`${CONFIRM_TRANSACTION_ROUTE}/${id}`);
navigate(`${CONFIRM_TRANSACTION_ROUTE}/${id}`);
return;
}
setShowDetails((prev) => {
Expand All @@ -247,7 +247,7 @@ function TransactionListItemInner({
});
return !prev;
});
}, [isUnapproved, history, id, trackEvent, category]);
}, [isUnapproved, navigate, id, trackEvent, category]);

const isSpeedUpButtonVisible = useMemo(() => {
if (
Expand Down
Loading
Loading