Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / components / hooks / usePreferredPlansMap.ts
blob243339865780982b0711d08eab128f52d0d18838
1 import { usePaymentStatus } from '@proton/account/paymentStatus/hooks';
2 import { usePlans } from '@proton/account/plans/hooks';
3 import { useSubscription } from '@proton/account/subscription/hooks';
4 import { useUser } from '@proton/account/user/hooks';
5 import { type Currency, type FullPlansMap, getPlansMap as getPlansMapInner } from '@proton/payments';
7 import { type GetPreferredCurrencyParamsHook, useCurrencies } from '../payments/client-extensions/useCurrencies';
9 type PreferredPlansMapHook = {
10     plansMapLoading: boolean;
11     plansMap: FullPlansMap;
12     getPlansMap: (overrides?: GetPreferredCurrencyParamsHook) => {
13         plansMap: FullPlansMap;
14         preferredCurrency: Currency;
15     };
16     preferredCurrency: Currency;
19 export const usePreferredPlansMap = (currencyFallback?: boolean): PreferredPlansMapHook => {
20     const [plans, plansLoading] = usePlans();
21     const [status, statusLoading] = usePaymentStatus();
22     const [subscription, subscriptionLoading] = useSubscription();
23     const [user] = useUser();
24     const { getPreferredCurrency } = useCurrencies();
26     const getPlansMap = (overrides: GetPreferredCurrencyParamsHook = {}) => {
27         const preferredCurrency = getPreferredCurrency({
28             ...overrides,
29             user,
30             subscription,
31             plans: plans?.plans,
32             status,
33         });
35         return {
36             preferredCurrency,
37             plansMap: getPlansMapInner(plans?.plans ?? [], preferredCurrency, currencyFallback),
38         };
39     };
41     return {
42         ...getPlansMap(),
43         getPlansMap,
44         plansMapLoading: plansLoading || statusLoading || subscriptionLoading,
45     };