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(
16 (result): [WasmApiExchangeRate | undefined, boolean] => {
17 const { value } = result;
19 const rate = walletAccount?.FiatCurrency && value?.[walletAccount.FiatCurrency];
21 return [rate, loading];
26 if (walletAccount?.FiatCurrency) {
27 void getExchangeRate(walletAccount.FiatCurrency);
29 }, [walletAccount?.FiatCurrency, getExchangeRate]);
31 return baseUseSelector(exchangeRateSimpleSelector);