forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usePreferredCurrency.ts
33 lines (26 loc) · 1.25 KB
/
usePreferredCurrency.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import {useMemo} from 'react';
import {useOnyx} from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
type PreferredCurrency = ValueOf<typeof CONST.PAYMENT_CARD_CURRENCY>;
/**
* Get user's preferred currency in the following order:
*
* 1. Payment card currency
* 2. User's local currency (if it's a valid payment card currency)
* 3. USD (default currency)
*
*/
function usePreferredCurrency(): PreferredCurrency {
const [personalDetails] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST);
const [session] = useOnyx(ONYXKEYS.SESSION);
const [fundList] = useOnyx(ONYXKEYS.FUND_LIST);
const paymentCardCurrency = useMemo(() => Object.values(fundList ?? {}).find((card) => card.accountData?.additionalData?.isBillingCard)?.accountData?.currency, [fundList]);
if (paymentCardCurrency) {
return paymentCardCurrency;
}
const currentUserLocalCurrency = (personalDetails?.[session?.accountID ?? '-1']?.localCurrencyCode ?? CONST.PAYMENT_CARD_CURRENCY.USD) as PreferredCurrency;
return Object.values(CONST.PAYMENT_CARD_CURRENCY).includes(currentUserLocalCurrency) ? currentUserLocalCurrency : CONST.PAYMENT_CARD_CURRENCY.USD;
}
export default usePreferredCurrency;