1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-29 07:16:36 +01:00
metamask-extension/ui/selectors/approvals.ts

50 lines
1.3 KiB
TypeScript
Raw Normal View History

2023-04-25 11:47:49 +02:00
import { ApprovalControllerState } from '@metamask/approval-controller';
import { ApprovalType } from '@metamask/controller-utils';
type ApprovalsMetaMaskState = {
metamask: {
pendingApprovals: ApprovalControllerState['pendingApprovals'];
approvalFlows: ApprovalControllerState['approvalFlows'];
2023-04-25 11:47:49 +02:00
};
};
export function hasPendingApprovals(
2023-04-25 11:47:49 +02:00
state: ApprovalsMetaMaskState,
approvalType: ApprovalType,
predicate?: (
approval: ApprovalControllerState['pendingApprovals'][string],
) => boolean,
) {
2023-04-25 11:47:49 +02:00
const pendingApprovalRequests = Object.values(
state.metamask.pendingApprovals,
).filter(({ type }) => type === approvalType);
if (predicate) {
return pendingApprovalRequests.some(predicate);
}
return pendingApprovalRequests.length > 0;
}
export const getApprovalRequestsByType = (
state: ApprovalsMetaMaskState,
approvalType: ApprovalType,
predicate?: (
approval: ApprovalControllerState['pendingApprovals'][string],
) => boolean,
) => {
const pendingApprovalRequests = Object.values(
state.metamask.pendingApprovals,
).filter(({ type }) => type === approvalType);
if (predicate) {
return pendingApprovalRequests.filter(predicate);
}
return pendingApprovalRequests;
};
export function getApprovalFlows(state: ApprovalsMetaMaskState) {
return state.metamask.approvalFlows;
}