1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 01:47:00 +01:00
metamask-extension/ui/selectors/metametrics.js
2022-01-12 13:31:54 -06:00

37 lines
1.1 KiB
JavaScript

import { createSelector } from 'reselect';
export const selectFragments = (state) => state.metamask.fragments;
export const selectFragmentBySuccessEvent = createSelector(
selectFragments,
(_, fragmentOptions) => fragmentOptions,
(fragments, fragmentOptions) => {
if (fragmentOptions.persist) {
return Object.values(fragments).find(
(fragment) => fragment.successEvent === fragmentOptions.successEvent,
);
}
return undefined;
},
);
export const selectFragmentById = createSelector(
selectFragments,
(_, fragmentId) => fragmentId,
(fragments, fragmentId) => {
// A valid existing fragment must exist in state.
// If these conditions are not meant we will create a new fragment.
if (fragmentId && fragments?.[fragmentId]) {
return fragments[fragmentId];
}
return undefined;
},
);
export const selectMatchingFragment = createSelector(
(state, params) =>
selectFragmentBySuccessEvent(state, params.fragmentOptions),
(state, params) => selectFragmentById(state, params.existingId),
(matchedBySuccessEvent, matchedById) => matchedById ?? matchedBySuccessEvent,
);