Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / wallet / store / hooks / useWalletAccountExchangeRate.ts
bloba68cb95b68bd497bbb3d179a9765258ae54d17e5
1 import { useEffect } from 'react';
3 import { createSelector } from '@reduxjs/toolkit';
5 import type { WasmApiExchangeRate, WasmApiWalletAccount } from '@proton/andromeda';
6 import { baseUseSelector } from '@proton/react-redux-store';
8 import { exchangeRateHooks, selectExchangeRate, useGetExchangeRate } from '../';
10 export const useWalletAccountExchangeRate = (walletAccount?: WasmApiWalletAccount) => {
11     const getExchangeRate = useGetExchangeRate();
12     const [, loading] = exchangeRateHooks.useValue();
14     const exchangeRateSimpleSelector = createSelector(
15         selectExchangeRate,
16         (result): [WasmApiExchangeRate | undefined, boolean] => {
17             const { value } = result;
19             const rate = walletAccount?.FiatCurrency && value?.[walletAccount.FiatCurrency];
21             return [rate, loading];
22         }
23     );
25     useEffect(() => {
26         if (walletAccount?.FiatCurrency) {
27             void getExchangeRate(walletAccount.FiatCurrency);
28         }
29     }, [walletAccount?.FiatCurrency, getExchangeRate]);
31     return baseUseSelector(exchangeRateSimpleSelector);