1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-23 02:10:12 +01:00
metamask-extension/ui/store/institutional/institution-background.ts
Albert Olivé 4c9bf40688
[MMI] Add interactive-replacement-token-page (#18683)
* Initial commit

* converted the component from class-based to functional.

* Refactored component

* Improved code and tests

* Finished adding tests

* Fixed eslint problems

* Added back custodyLabels component

* Fixed eslint problems

* fixed ts lint problem

* Fixed eslint problems

* Added more tests and improved code

* Added comments

* Fixed eslint problems

* Fixed eslint problems
2023-04-27 10:45:37 +02:00

216 lines
6.3 KiB
TypeScript

import log from 'loglevel';
import { ThunkAction } from 'redux-thunk';
import { AnyAction } from 'redux';
import {
forceUpdateMetamaskState,
displayWarning,
hideLoadingIndication,
showLoadingIndication,
} from '../actions';
import {
callBackgroundMethod,
submitRequestToBackground,
} from '../action-queue';
import { MetaMaskReduxState } from '../store';
import { isErrorWithMessage } from '../../../shared/modules/error';
export function showInteractiveReplacementTokenBanner({
url,
oldRefreshToken,
}: {
url: string;
oldRefreshToken: string;
}): ThunkAction<void, MetaMaskReduxState, unknown, AnyAction> {
return async (dispatch) => {
try {
await submitRequestToBackground('showInteractiveReplacementTokenBanner', [
url,
oldRefreshToken,
]);
} catch (err: any) {
if (err) {
dispatch(displayWarning(err.message));
throw new Error(err.message);
}
}
};
}
/**
* A factory that contains all MMI actions ready to use
* Example usage:
* const mmiActions = mmiActionsFactory();
* mmiActions.connectCustodyAddresses(...)
*/
export function mmiActionsFactory() {
function createAsyncAction(
name: string,
params: any,
useForceUpdateMetamaskState?: any,
loadingText?: string,
): ThunkAction<void, MetaMaskReduxState, unknown, AnyAction> {
log.debug(`background.${name}`);
return async (dispatch) => {
if (loadingText) {
dispatch(showLoadingIndication(loadingText));
}
let result;
try {
result = await submitRequestToBackground(name, [...params]);
} catch (error) {
dispatch(displayWarning(error));
if (isErrorWithMessage(error)) {
throw new Error(error.message);
} else {
throw error;
}
}
if (loadingText) {
dispatch(hideLoadingIndication());
}
if (useForceUpdateMetamaskState) {
await forceUpdateMetamaskState(dispatch);
}
return result;
};
}
function createAction(name: string, payload: any) {
return () => {
callBackgroundMethod(name, [payload], (err) => {
if (isErrorWithMessage(err)) {
throw new Error(err.message);
}
});
};
}
return {
connectCustodyAddresses: (
custodianType: string,
custodianName: string,
newAccounts: string[],
) =>
createAsyncAction(
'connectCustodyAddresses',
[custodianType, custodianName, newAccounts],
forceUpdateMetamaskState,
'Looking for your custodian account...',
),
getCustodianAccounts: (
token: string,
apiUrl: string,
custody: string,
getNonImportedAccounts: boolean,
) =>
createAsyncAction(
'getCustodianAccounts',
[token, apiUrl, custody, getNonImportedAccounts],
forceUpdateMetamaskState,
'Getting custodian accounts...',
),
getCustodianAccountsByAddress: (
jwt: string,
apiUrl: string,
address: string,
custody: string,
) =>
createAsyncAction(
'getCustodianAccountsByAddress',
[jwt, apiUrl, address, custody],
forceUpdateMetamaskState,
'Getting custodian accounts...',
),
getCustodianTransactionDeepLink: (address: string, txId: string) =>
createAsyncAction(
'getCustodianTransactionDeepLink',
[address, txId],
forceUpdateMetamaskState,
),
getCustodianConfirmDeepLink: (txId: string) =>
createAsyncAction(
'getCustodianConfirmDeepLink',
[txId],
forceUpdateMetamaskState,
),
getCustodianSignMessageDeepLink: (from: string, custodyTxId: string) =>
createAsyncAction(
'getCustodianSignMessageDeepLink',
[from, custodyTxId],
forceUpdateMetamaskState,
),
getCustodianToken: (custody: string) =>
createAsyncAction(
'getCustodianToken',
[custody],
forceUpdateMetamaskState,
),
getCustodianJWTList: (custody: string) =>
createAsyncAction(
'getCustodianJWTList',
[custody],
forceUpdateMetamaskState,
),
setWaitForConfirmDeepLinkDialog: (waitForConfirmDeepLinkDialog: boolean) =>
createAction(
'setWaitForConfirmDeepLinkDialog',
waitForConfirmDeepLinkDialog,
),
setComplianceAuthData: (clientId: string, projectId: string) =>
createAsyncAction('setComplianceAuthData', [{ clientId, projectId }]),
deleteComplianceAuthData: () =>
createAsyncAction('deleteComplianceAuthData', []),
generateComplianceReport: (address: string) =>
createAction('generateComplianceReport', address),
getComplianceHistoricalReportsByAddress: (
address: string,
projectId: string,
) =>
createAsyncAction('getComplianceHistoricalReportsByAddress', [
address,
projectId,
]),
syncReportsInProgress: (address: string, historicalReports: []) =>
createAction('syncReportsInProgress', { address, historicalReports }),
removeConnectInstitutionalFeature: (origin: string, projectId: string) =>
createAction('removeConnectInstitutionalFeature', { origin, projectId }),
removeAddTokenConnectRequest: (
origin: string,
apiUrl: string,
token: string,
) =>
createAction('removeAddTokenConnectRequest', { origin, apiUrl, token }),
setCustodianConnectRequest: (
token: string,
apiUrl: string,
custodianType: string,
custodianName: string,
) =>
createAsyncAction('setCustodianConnectRequest', [
{ token, apiUrl, custodianType, custodianName },
]),
getCustodianConnectRequest: () =>
createAsyncAction('getCustodianConnectRequest', []),
getMmiConfiguration: () => createAsyncAction('getMmiConfiguration', []),
getAllCustodianAccountsWithToken: (custodyType: string, token: string) =>
createAsyncAction('getAllCustodianAccountsWithToken', [
custodyType,
token,
]),
setCustodianNewRefreshToken: (
address: string,
oldAuthDetails: string,
oldApiUrl: string,
newAuthDetails: string,
newApiUrl: string,
) =>
createAsyncAction('setCustodianNewRefreshToken', [
address,
oldAuthDetails,
oldApiUrl,
newAuthDetails,
newApiUrl,
]),
};
}