1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-25 11:28:51 +01:00
metamask-extension/ui/store/institutional/institution-actions.ts
António Regadas c022d2eb9a
[MMI] Move mmi actions to extension (#18057)
* MMI adds actions and background files to the institution/ folder

* MMI lint fix

* MMI lint fix

* MMI import path fixed to be relative

* MMI import path fixed

* MMI adds the relative path to isErrorWithMessage

* MMI adds the tests for mmi actions

* MMI lint fix

* adds tests to mmi actions

* prettier fix

* MMI prettier and adds test

* MMI prettier

* MMI lint fix

* MMI prettier fix

* MMI rename folder
2023-03-14 10:57:05 +00:00

131 lines
3.4 KiB
TypeScript

import { ThunkAction, ThunkDispatch } from 'redux-thunk';
import { AnyAction } from 'redux';
import {
closeCurrentNotificationWindow,
hideModal,
showModal,
} from '../actions';
import {
CombinedBackgroundAndReduxState,
MetaMaskReduxState,
TemporaryMessageDataType,
} from '../store';
import { toChecksumHexAddress } from '../../../shared/modules/hexstring-utils';
export function showInteractiveReplacementTokenModal(): ThunkAction<
void,
MetaMaskReduxState,
unknown,
AnyAction
> {
return (dispatch) => {
dispatch(
showModal({
name: 'INTERACTIVE_REPLACEMENT_TOKEN_MODAL',
}),
);
};
}
export function showCustodyConfirmLink(
link: string,
address: string,
closeNotification: boolean,
custodyId: string,
): ThunkAction<void, MetaMaskReduxState, unknown, AnyAction> {
return (dispatch) => {
dispatch(
showModal({
name: 'CUSTODY_CONFIRM_LINK',
link,
address,
closeNotification,
custodyId,
}),
);
};
}
export function updateCustodyState(
dispatch: ThunkDispatch<CombinedBackgroundAndReduxState, unknown, AnyAction>,
newState: MetaMaskReduxState['metamask'],
state: CombinedBackgroundAndReduxState & any,
) {
if (!newState.currentNetworkTxList || !state.metamask.currentNetworkTxList) {
return;
}
const differentTxs = newState.currentNetworkTxList.filter(
(item) =>
state.metamask.currentNetworkTxList.filter(
(tx: { [key: string]: any }) =>
tx.custodyId === item.custodyId &&
tx.custodyStatus !== item.custodyStatus,
).length > 0,
);
const txStateSaysDeepLinkShouldClose = Boolean(
differentTxs.find((tx) => {
const custodyAccountDetails =
state.metamask.custodyAccountDetails[
toChecksumHexAddress(tx.txParams.from)
];
const custody = custodyAccountDetails?.custodyType
.split(' - ')[1]
.toLowerCase();
if (!custody) {
return false;
}
return (
tx.custodyId === state.appState.modal.modalState.props?.custodyId &&
(state.metamask.custodyStatusMaps[custody][tx.custodyStatus]
?.mmStatus !== 'approved' ||
tx.custodyStatus === 'created')
);
}),
);
if (
state.appState.modal.open &&
state.appState.modal.modalState.name === 'CUSTODY_CONFIRM_LINK' &&
txStateSaysDeepLinkShouldClose
) {
if (state.appState.modal.modalState.props?.closeNotification) {
dispatch(closeCurrentNotificationWindow());
}
dispatch(hideModal());
}
if (
state.appState.modal.open &&
state.appState.modal.modalState.name ===
'INTERACTIVE_REPLACEMENT_TOKEN_MODAL'
) {
if (state.appState.modal.modalState.props?.closeNotification) {
dispatch(closeCurrentNotificationWindow());
}
}
}
export function checkForUnapprovedTypedMessages(
msgData: TemporaryMessageDataType['msgParams'],
newState: MetaMaskReduxState['metamask'],
) {
const custodianUnapprovedMessages = Object.keys(
newState.unapprovedTypedMessages,
)
.map((key) => newState.unapprovedTypedMessages[key])
.filter((message) => message.custodyId && message.status === 'unapproved');
if (custodianUnapprovedMessages && custodianUnapprovedMessages.length > 0) {
return {
...msgData,
custodyId:
newState.unapprovedTypedMessages[msgData.metamaskId]?.custodyId,
};
}
return msgData;
}