Skip to content

Commit

Permalink
feat: UX: Multichain: Close other popups when new popup is opened
Browse files Browse the repository at this point in the history
  • Loading branch information
darkwing committed Mar 23, 2024
1 parent fffd8a0 commit c00f0a5
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app/scripts/controllers/app-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export default class AppStateController extends EventEmitter {
showAccountBanner: true,
trezorModel: null,
currentPopupId: undefined,
currentExtensionPopupId: undefined,
// This key is only used for checking if the user had set advancedGasFee
// prior to Migration 92.3 where we split out the setting to support
// multiple networks.
Expand Down Expand Up @@ -523,6 +524,10 @@ export default class AppStateController extends EventEmitter {
});
}

setCurrentExtensionPopupId(currentExtensionPopupId) {
this.store.updateState({ currentExtensionPopupId });
}

/**
* A getter to retrieve currentPopupId saved in the appState
*/
Expand Down
2 changes: 2 additions & 0 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3106,6 +3106,8 @@ export default class MetamaskController extends EventEmitter {
// AppStateController
setLastActiveTime:
appStateController.setLastActiveTime.bind(appStateController),
setCurrentExtensionPopupId:
appStateController.setCurrentExtensionPopupId.bind(appStateController),
setDefaultHomeActiveTabName:
appStateController.setDefaultHomeActiveTabName.bind(appStateController),
setConnectedStatusPopoverHasBeenShown:
Expand Down
13 changes: 13 additions & 0 deletions ui/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
getSelectedInternalAccount,
getUnapprovedTransactions,
getNetworkToAutomaticallySwitchTo,
getUseRequestQueue,
} from './selectors';
import { ALERT_STATE } from './ducks/alerts';
import {
Expand Down Expand Up @@ -210,6 +211,18 @@ async function startApp(metamaskState, backgroundConnection, opts) {
},
};

// Register this window as the current popup
// and set in background state
if (
process.env.MULTICHAIN &&
getUseRequestQueue(state) &&
getEnvironmentType() === ENVIRONMENT_TYPE_POPUP
) {
const thisPopupId = Date.now();
global.metamask.id = thisPopupId;
actions.setCurrentExtensionPopupId(thisPopupId);
}

// start app
render(<Root store={store} />, opts.container);

Expand Down
15 changes: 15 additions & 0 deletions ui/pages/routes/routes.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ export default class Routes extends Component {
neverShowSwitchedNetworkMessage: PropTypes.bool.isRequired,
automaticallySwitchNetwork: PropTypes.func.isRequired,
unapprovedTransactions: PropTypes.number.isRequired,
currentExtensionPopupId: PropTypes.number,
useRequestQueue: PropTypes.bool,
///: BEGIN:ONLY_INCLUDE_IF(keyring-snaps)
isShowKeyringSnapRemovalResultModal: PropTypes.bool.isRequired,
hideShowKeyringSnapRemovalResultModal: PropTypes.func.isRequired,
Expand Down Expand Up @@ -254,6 +256,7 @@ export default class Routes extends Component {
unapprovedTransactions,
isUnlocked,
} = this.props;

if (theme !== prevProps.theme) {
this.setTheme();
}
Expand All @@ -277,6 +280,18 @@ export default class Routes extends Component {
activeTabOrigin,
);
}

// Terminate the popup when another popup is opened
// if the user is using RPC queueing
if (
useRequestQueue &&
process.env.MULTICHAIN &&
currentExtensionPopupId !== undefined &&
global.metamask.id !== undefined &&
currentExtensionPopupId !== global.metamask.id
) {
window.close();
}
}

UNSAFE_componentWillMount() {
Expand Down
4 changes: 4 additions & 0 deletions ui/pages/routes/routes.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
getNeverShowSwitchedNetworkMessage,
getNetworkToAutomaticallySwitchTo,
getNumberOfAllUnapprovedTransactions,
getUseRequestQueue,
} from '../../selectors';
import {
lockMetamask,
Expand Down Expand Up @@ -128,6 +129,9 @@ function mapStateToProps(state) {
networkToAutomaticallySwitchTo,
unapprovedTransactions: getNumberOfAllUnapprovedTransactions(state),
neverShowSwitchedNetworkMessage: getNeverShowSwitchedNetworkMessage(state),
currentExtensionPopupId: state.metamask.currentExtensionPopupId,
useRequestQueue: getUseRequestQueue(state),

///: BEGIN:ONLY_INCLUDE_IF(keyring-snaps)
isShowKeyringSnapRemovalResultModal:
state.appState.showKeyringRemovalSnapModal,
Expand Down
10 changes: 10 additions & 0 deletions ui/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4943,6 +4943,16 @@ export function updateInterfaceState(
}) as any;
}

/**
* Update the currentPopupid generated when the user opened the popup
*
* @param id - The Snap interface ID.
* @returns Promise Resolved on successfully submitted background request.
*/
export async function setCurrentExtensionPopupId(id: number) {
await submitRequestToBackground<void>('setCurrentExtensionPopupId', [id]);
}

/**
* Delete the Snap interface from state.
*
Expand Down

0 comments on commit c00f0a5

Please sign in to comment.