|
7 | 7 | ProductType, |
8 | 8 | RecurringInterval, |
9 | 9 | Subscription, |
10 | | - BalanceCategory, |
11 | 10 | SubscriptionEligibility, |
12 | 11 | SubscriptionStatus, |
13 | 12 | ModalType, |
@@ -52,17 +51,22 @@ import { getInternalAccountBySelectedAccountGroupAndCaip } from '../../selectors |
52 | 51 | import { |
53 | 52 | getMetaMetricsId, |
54 | 53 | getModalTypeForShieldEntryModal, |
| 54 | + getSelectedInternalAccount, |
55 | 55 | getUnapprovedConfirmations, |
56 | 56 | selectNetworkConfigurationByChainId, |
57 | 57 | } from '../../selectors'; |
58 | 58 | import { useSubscriptionMetrics } from '../shield/metrics/useSubscriptionMetrics'; |
59 | 59 | import { CaptureShieldSubscriptionRequestParams } from '../shield/metrics/types'; |
60 | 60 | import { EntryModalSourceEnum } from '../../../shared/constants/subscriptions'; |
61 | 61 | import { DefaultSubscriptionPaymentOptions } from '../../../shared/types'; |
62 | | -import { getLatestSubscriptionStatus } from '../../../shared/modules/shield'; |
| 62 | +import { |
| 63 | + getLatestSubscriptionStatus, |
| 64 | + getUserBalanceCategory, |
| 65 | +} from '../../../shared/modules/shield'; |
63 | 66 | import { openWindow } from '../../helpers/utils/window'; |
64 | 67 | import { SUPPORT_LINK } from '../../../shared/lib/ui-utils'; |
65 | 68 | import { MetaMetricsEventName } from '../../../shared/constants/metametrics'; |
| 69 | +import { useAccountTotalFiatBalance } from '../useAccountTotalFiatBalance'; |
66 | 70 | import { |
67 | 71 | TokenWithApprovalAmount, |
68 | 72 | useSubscriptionPricing, |
@@ -396,41 +400,46 @@ export const useSubscriptionEligibility = (product: ProductType) => { |
396 | 400 | const dispatch = useDispatch<MetaMaskReduxDispatch>(); |
397 | 401 | const isSignedIn = useSelector(selectIsSignedIn); |
398 | 402 | const isUnlocked = useSelector(getIsUnlocked); |
| 403 | + const selectedAccount = useSelector(getSelectedInternalAccount); |
| 404 | + const { totalFiatBalance } = useAccountTotalFiatBalance( |
| 405 | + selectedAccount, |
| 406 | + false, |
| 407 | + true, // use USD conversion rate instead of the current currency |
| 408 | + ); |
399 | 409 |
|
400 | | - const getSubscriptionEligibility = useCallback( |
401 | | - async (params?: { |
402 | | - balanceCategory?: BalanceCategory; |
403 | | - }): Promise<SubscriptionEligibility | undefined> => { |
404 | | - try { |
405 | | - // if user is not signed in or unlocked, return undefined |
406 | | - if (!isSignedIn || !isUnlocked) { |
407 | | - return undefined; |
408 | | - } |
409 | | - |
410 | | - // get the subscriptions before making the eligibility request |
411 | | - // here, we cannot `useUserSubscriptions` hook as the hook's initial state has empty subscriptions array and loading state is false |
412 | | - // that mistakenly makes `user does not have a subscription` and triggers the eligibility request |
413 | | - const subscriptions = await dispatch(getSubscriptions()); |
414 | | - const isShieldSubscriptionActive = |
415 | | - getIsShieldSubscriptionActive(subscriptions); |
416 | | - |
417 | | - if (!isShieldSubscriptionActive) { |
418 | | - // only if shield subscription is not active, get the eligibility |
419 | | - const eligibilities = await dispatch( |
420 | | - getSubscriptionsEligibilities(params), |
421 | | - ); |
422 | | - return eligibilities.find( |
423 | | - (eligibility) => eligibility.product === product, |
424 | | - ); |
425 | | - } |
426 | | - return undefined; |
427 | | - } catch (error) { |
428 | | - log.warn('[useSubscriptionEligibility] error', error); |
| 410 | + const getSubscriptionEligibility = useCallback(async (): Promise< |
| 411 | + SubscriptionEligibility | undefined |
| 412 | + > => { |
| 413 | + try { |
| 414 | + // if user is not signed in or unlocked, return undefined |
| 415 | + if (!isSignedIn || !isUnlocked) { |
429 | 416 | return undefined; |
430 | 417 | } |
431 | | - }, |
432 | | - [isSignedIn, isUnlocked, dispatch, product], |
433 | | - ); |
| 418 | + |
| 419 | + const balanceCategory = getUserBalanceCategory(Number(totalFiatBalance)); |
| 420 | + |
| 421 | + // get the subscriptions before making the eligibility request |
| 422 | + // here, we cannot `useUserSubscriptions` hook as the hook's initial state has empty subscriptions array and loading state is false |
| 423 | + // that mistakenly makes `user does not have a subscription` and triggers the eligibility request |
| 424 | + const subscriptions = await dispatch(getSubscriptions()); |
| 425 | + const isShieldSubscriptionActive = |
| 426 | + getIsShieldSubscriptionActive(subscriptions); |
| 427 | + |
| 428 | + if (!isShieldSubscriptionActive) { |
| 429 | + // only if shield subscription is not active, get the eligibility |
| 430 | + const eligibilities = await dispatch( |
| 431 | + getSubscriptionsEligibilities({ balanceCategory }), |
| 432 | + ); |
| 433 | + return eligibilities.find( |
| 434 | + (eligibility) => eligibility.product === product, |
| 435 | + ); |
| 436 | + } |
| 437 | + return undefined; |
| 438 | + } catch (error) { |
| 439 | + log.warn('[useSubscriptionEligibility] error', error); |
| 440 | + return undefined; |
| 441 | + } |
| 442 | + }, [isSignedIn, isUnlocked, dispatch, product, totalFiatBalance]); |
434 | 443 |
|
435 | 444 | return { |
436 | 445 | getSubscriptionEligibility, |
|
0 commit comments